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.
16 require 'signet/oauth_2/client'
22 # Generates access tokens using the JWT assertion profile. Requires a
23 # service account & access to the private key.
27 # client = Google::APIClient.new
28 # key = Google::APIClient::PKCS12.load_key('client.p12', 'notasecret')
29 # service_account = Google::APIClient::JWTAsserter.new(
30 # '123456-abcdef@developer.gserviceaccount.com',
31 # 'https://www.googleapis.com/auth/prediction',
33 # client.authorization = service_account.authorize
36 # @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
38 # @return [String] ID/email of the issuing party
40 # @return [Fixnum] How long, in seconds, the assertion is valid for
42 # @return [Fixnum] Seconds to expand the issued at/expiry window to account for clock skew
44 # @return [String] Scopes to authorize
46 # @return [OpenSSL::PKey] key for signing assertions
50 # Initializes the asserter for a service account.
52 # @param [String] issuer
53 # Name/ID of the client issuing the assertion
54 # @param [String, Array] scope
55 # Scopes to authorize. May be a space delimited string or array of strings
56 # @param [OpenSSL::PKey] key
57 # RSA private key for signing assertions
58 def initialize(issuer, scope, key)
61 self.expiry = 60 # 1 min default
67 # Set the scopes to authorize
69 # @param [String, Array] new_scope
70 # Scopes to authorize. May be a space delimited string or array of strings
74 @scope = new_scope.join(' ')
80 raise TypeError, "Expected Array or String, got #{new_scope.class}"
85 # Builds & signs the assertion.
87 # @param [String] person
88 # Email address of a user, if requesting a token to act on their behalf
89 # @return [String] Encoded JWT
90 def to_jwt(person=nil)
94 "scope" => self.scope,
95 "aud" => "https://accounts.google.com/o/oauth2/token",
96 "exp" => (now + expiry).to_i,
97 "iat" => (now - skew).to_i
99 assertion['prn'] = person unless person.nil?
100 return JWT.encode(assertion, @key, "RS256")
104 # Request a new access token.
106 # @param [String] person
107 # Email address of a user, if requesting a token to act on their behalf
108 # @param [Hash] options
109 # Pass through to Signet::OAuth2::Client.fetch_access_token
110 # @return [Signet::OAuth2::Client] Access token
112 # @see Signet::OAuth2::Client.fetch_access_token!
113 def authorize(person = nil, options={})
114 assertion = self.to_jwt(person)
115 authorization = Signet::OAuth2::Client.new(
116 :token_credential_uri => 'https://accounts.google.com/o/oauth2/token'
118 authorization.grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
119 authorization.extension_parameters = { :assertion => assertion }
120 authorization.fetch_access_token!(options)
121 return JWTAuthorization.new(authorization, self, person)
126 # Proxy for authorization that allows refreshing the access token
127 # using assertions instead of refresh tokens.
128 class JWTAuthorization < DelegateClass(Signet::OAuth2::Client)
130 # Initialize the proxy
132 # @param [Signet::OAuth2::Client] authorization
133 # Original authorization instance
134 # @param [Google::APIClient::JWTAsserter]
135 # Asserter for generating new access tokens
136 # @param [String] person
137 # Optional target user if impersonating.
138 def initialize(authorization, asserter, person = nil)
145 # @see Signet::OAuth2::Client#fetch_access_token!
146 def fetch_access_token!(options={})
147 new_authorization = @asserter.authorize(@person, options)
148 __setobj__(new_authorization)