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.
25 # @example Using Signet
27 # key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret')
28 # client.authorization = Signet::OAuth2::Client.new(
29 # :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
30 # :audience => 'https://accounts.google.com/o/oauth2/token',
31 # :scope => 'https://www.googleapis.com/auth/prediction',
32 # :issuer => '123456-abcdef@developer.gserviceaccount.com',
33 # :signing_key => key)
34 # client.authorization.fetch_access_token!
38 # Service accounts are now supported directly in Signet
39 # @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
41 # @return [String] ID/email of the issuing party
43 # @return [Fixnum] How long, in seconds, the assertion is valid for
45 # @return [Fixnum] Seconds to expand the issued at/expiry window to account for clock skew
47 # @return [String] Scopes to authorize
49 # @return [String,OpenSSL::PKey] key for signing assertions
51 # @return [String] Algorithm used for signing
52 attr_accessor :algorithm
55 # Initializes the asserter for a service account.
57 # @param [String] issuer
58 # Name/ID of the client issuing the assertion
59 # @param [String, Array] scope
60 # Scopes to authorize. May be a space delimited string or array of strings
61 # @param [String,OpenSSL::PKey] key
62 # Key for signing assertions
63 # @param [String] algorithm
64 # Algorithm to use, either 'RS256' for RSA with SHA-256
65 # or 'HS256' for HMAC with SHA-256
66 def initialize(issuer, scope, key, algorithm = "RS256")
69 self.expiry = 60 # 1 min default
72 self.algorithm = algorithm
76 # Set the scopes to authorize
78 # @param [String, Array] new_scope
79 # Scopes to authorize. May be a space delimited string or array of strings
83 @scope = new_scope.join(' ')
89 raise TypeError, "Expected Array or String, got #{new_scope.class}"
94 # Request a new access token.
96 # @param [String] person
97 # Email address of a user, if requesting a token to act on their behalf
98 # @param [Hash] options
99 # Pass through to Signet::OAuth2::Client.fetch_access_token
100 # @return [Signet::OAuth2::Client] Access token
102 # @see Signet::OAuth2::Client.fetch_access_token!
103 def authorize(person = nil, options={})
104 authorization = self.to_authorization(person)
105 authorization.fetch_access_token!(options)
110 # Builds a Signet OAuth2 client
112 # @return [Signet::OAuth2::Client] Access token
113 def to_authorization(person = nil)
114 return Signet::OAuth2::Client.new(
115 :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
116 :audience => 'https://accounts.google.com/o/oauth2/token',
117 :scope => self.scope,
119 :signing_key => @key,
120 :signing_algorithm => @algorithm,