Replace deprecated method that cause ruby warnings File.exit?
[arvados.git] / lib / google / api_client / auth / file_storage.rb
1 # Copyright 2013 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'json'
16 require 'signet/oauth_2/client'
17
18 module Google
19   class APIClient
20     ##
21     # Represents cached OAuth 2 tokens stored on local disk in a
22     # JSON serialized file. Meant to resemble the serialized format
23     # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
24     #
25     class FileStorage
26       # @return [String] Path to the credentials file.
27       attr_accessor :path
28
29       # @return [Signet::OAuth2::Client] Path to the credentials file.
30       attr_reader :authorization
31
32       ##
33       # Initializes the FileStorage object.
34       #
35       # @param [String] path
36       #    Path to the credentials file.
37       def initialize(path)
38         @path = path
39         self.load_credentials
40       end
41
42       ##
43       # Attempt to read in credentials from the specified file.
44       def load_credentials
45         if File.exists? self.path
46           File.open(self.path, 'r') do |file|
47             cached_credentials = JSON.load(file)
48             @authorization = Signet::OAuth2::Client.new(cached_credentials)
49             @authorization.issued_at = Time.at(cached_credentials['issued_at'])
50             if @authorization.expired?
51               @authorization.fetch_access_token!
52               self.write_credentials
53             end
54           end
55         end
56       end
57
58       ##
59       # Write the credentials to the specified file.
60       #
61       # @param [Signet::OAuth2::Client] authorization
62       #    Optional authorization instance. If not provided, the authorization
63       #    already associated with this instance will be written.
64       def write_credentials(authorization=nil)
65         @authorization = authorization unless authorization.nil?
66
67         unless @authorization.refresh_token.nil?
68           hash = {}
69           %w'access_token
70            authorization_uri
71            client_id
72            client_secret
73            expires_in
74            refresh_token
75            token_credential_uri'.each do |var|
76             hash[var] = @authorization.instance_variable_get("@#{var}")
77           end
78           hash['issued_at'] = @authorization.issued_at.to_i
79
80           File.open(self.path, 'w', 0600) do |file|
81             file.write(hash.to_json)
82           end
83         end
84       end
85     end
86   end
87 end