1 # Copyright 2010 Google Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
22 # Manages the persistence of client configuration data and secrets.
24 def self.load(filename=nil)
25 if filename && File.directory?(filename)
26 search_path = File.expand_path(filename)
30 search_path ||= File.expand_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]:[\/\\]/
36 'No client_secrets.json filename supplied ' +
37 'and/or could not be found in search path.'
39 search_path = File.expand_path(File.join(search_path, '..'))
42 data = File.open(filename, 'r') { |file| MultiJson.load(file.read) }
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"]
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]
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"]
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
80 return MultiJson.dump({
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?)