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.
17 require 'compat/multi_json'
23 # Manages the persistence of client configuration data and secrets. Format
24 # inspired by the Google API Python client.
26 # @see https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
31 # "client_id": "asdfjasdljfasdkjf",
32 # "client_secret": "1912308409123890",
33 # "redirect_uris": ["https://www.example.com/oauth2callback"],
34 # "auth_uri": "https://accounts.google.com/o/oauth2/auth",
35 # "token_uri": "https://accounts.google.com/o/oauth2/token"
42 # "client_id": "837647042410-75ifg...usercontent.com",
43 # "client_secret":"asdlkfjaskd",
44 # "redirect_uris": ["http://localhost", "urn:ietf:oauth:2.0:oob"],
45 # "auth_uri": "https://accounts.google.com/o/oauth2/auth",
46 # "token_uri": "https://accounts.google.com/o/oauth2/token"
52 # Reads client configuration from a file
54 # @param [String] filename
55 # Path to file to load
57 # @return [Google::APIClient::ClientSecrets]
58 # OAuth client settings
59 def self.load(filename=nil)
60 if filename && File.directory?(filename)
61 search_path = File.expand_path(filename)
65 search_path ||= File.expand_path('.')
67 if File.exist?(File.join(search_path, 'client_secrets.json'))
68 filename = File.join(search_path, 'client_secrets.json')
69 elsif search_path == '/' || search_path =~ /[a-zA-Z]:[\/\\]/
71 'No client_secrets.json filename supplied ' +
72 'and/or could not be found in search path.'
74 search_path = File.expand_path(File.join(search_path, '..'))
77 data = File.open(filename, 'r') { |file| MultiJson.load(file.read) }
82 # Intialize OAuth client settings.
84 # @param [Hash] options
85 # Parsed client secrets files
86 def initialize(options={})
87 # Client auth configuration
88 @flow = options[:flow] || options.keys.first.to_s || 'web'
89 fdata = options[@flow]
90 @client_id = fdata[:client_id] || fdata["client_id"]
91 @client_secret = fdata[:client_secret] || fdata["client_secret"]
92 @redirect_uris = fdata[:redirect_uris] || fdata["redirect_uris"]
93 @redirect_uris ||= [fdata[:redirect_uri]]
94 @javascript_origins = (
95 fdata[:javascript_origins] ||
96 fdata["javascript_origins"]
98 @javascript_origins ||= [fdata[:javascript_origin]]
99 @authorization_uri = fdata[:auth_uri] || fdata["auth_uri"]
100 @authorization_uri ||= fdata[:authorization_uri]
101 @token_credential_uri = fdata[:token_uri] || fdata["token_uri"]
102 @token_credential_uri ||= fdata[:token_credential_uri]
104 # Associated token info
105 @access_token = fdata[:access_token] || fdata["access_token"]
106 @refresh_token = fdata[:refresh_token] || fdata["refresh_token"]
107 @id_token = fdata[:id_token] || fdata["id_token"]
108 @expires_in = fdata[:expires_in] || fdata["expires_in"]
109 @expires_at = fdata[:expires_at] || fdata["expires_at"]
110 @issued_at = fdata[:issued_at] || fdata["issued_at"]
114 :flow, :client_id, :client_secret, :redirect_uris, :javascript_origins,
115 :authorization_uri, :token_credential_uri, :access_token,
116 :refresh_token, :id_token, :expires_in, :expires_at, :issued_at
120 # Serialize back to the original JSON form
125 return MultiJson.dump({
127 'client_id' => self.client_id,
128 'client_secret' => self.client_secret,
129 'redirect_uris' => self.redirect_uris,
130 'javascript_origins' => self.javascript_origins,
131 'auth_uri' => self.authorization_uri,
132 'token_uri' => self.token_credential_uri,
133 'access_token' => self.access_token,
134 'refresh_token' => self.refresh_token,
135 'id_token' => self.id_token,
136 'expires_in' => self.expires_in,
137 'expires_at' => self.expires_at,
138 'issued_at' => self.issued_at
139 }).inject({}) do |accu, (k, v)|
140 # Prunes empty values from JSON output.
141 unless v == nil || (v.respond_to?(:empty?) && v.empty?)