Updated to avoid deprecation of encode and decode methods in multi_json gem.
[arvados.git] / lib / google / api_client / client_secrets.rb
1 # Copyright 2010 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
16 require 'multi_json'
17
18
19 module Google
20   class APIClient
21     ##
22     # Manages the persistence of client configuration data and secrets.
23     class ClientSecrets
24       def self.load(filename=nil)
25         if filename && File.directory?(filename)
26           search_path = File.expand_path(filename)
27           filename = nil
28         end
29         while filename == nil
30           search_path ||= File.expand_path('.')
31           puts search_path
32           if File.exist?(File.join(search_path, 'client_secrets.json'))
33             filename = File.join(search_path, 'client_secrets.json')
34           elsif search_path == '/' || search_path =~ /[a-zA-Z]:[\/\\]/
35             raise ArgumentError,
36               'No client_secrets.json filename supplied ' +
37               'and/or could not be found in search path.'
38           else
39             search_path = File.expand_path(File.join(search_path, '..'))
40           end
41         end
42         data = File.open(filename, 'r') { |file| MultiJson.load(file.read) }
43         return self.new(data)
44       end
45
46       def initialize(options={})
47         # Client auth configuration
48         @flow = options[:flow] || options.keys.first.to_s || 'web'
49         fdata = options[@flow]
50         @client_id = fdata[:client_id] || fdata["client_id"]
51         @client_secret = fdata[:client_secret] || fdata["client_secret"]
52         @redirect_uris = fdata[:redirect_uris] || fdata["redirect_uris"]
53         @redirect_uris ||= [fdata[:redirect_uri]]
54         @javascript_origins = (
55           fdata[:javascript_origins] ||
56           fdata["javascript_origins"]
57         )
58         @javascript_origins ||= [fdata[:javascript_origin]]
59         @authorization_uri = fdata[:auth_uri] || fdata["auth_uri"]
60         @authorization_uri ||= fdata[:authorization_uri]
61         @token_credential_uri = fdata[:token_uri] || fdata["token_uri"]
62         @token_credential_uri ||= fdata[:token_credential_uri]
63
64         # Associated token info
65         @access_token = fdata[:access_token] || fdata["access_token"]
66         @refresh_token = fdata[:refresh_token] || fdata["refresh_token"]
67         @id_token = fdata[:id_token] || fdata["id_token"]
68         @expires_in = fdata[:expires_in] || fdata["expires_in"]
69         @expires_at = fdata[:expires_at] || fdata["expires_at"]
70         @issued_at = fdata[:issued_at] || fdata["issued_at"]
71       end
72
73       attr_reader(
74         :flow, :client_id, :client_secret, :redirect_uris, :javascript_origins,
75         :authorization_uri, :token_credential_uri, :access_token,
76         :refresh_token, :id_token, :expires_in, :expires_at, :issued_at
77       )
78
79       def to_json
80         return MultiJson.dump({
81           self.flow => ({
82             'client_id' => self.client_id,
83             'client_secret' => self.client_secret,
84             'redirect_uris' => self.redirect_uris,
85             'javascript_origins' => self.javascript_origins,
86             'auth_uri' => self.authorization_uri,
87             'token_uri' => self.token_credential_uri,
88             'access_token' => self.access_token,
89             'refresh_token' => self.refresh_token,
90             'id_token' => self.id_token,
91             'expires_in' => self.expires_in,
92             'expires_at' => self.expires_at,
93             'issued_at' => self.issued_at
94           }).inject({}) do |accu, (k, v)|
95             # Prunes empty values from JSON output.
96             unless v == nil || (v.respond_to?(:empty?) && v.empty?)
97               accu[k] = v
98             end
99             accu
100           end
101         })
102       end
103     end
104   end
105 end