21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / lib / google / api_client / auth / jwt_asserter.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 require 'jwt'
16 require 'signet/oauth_2/client'
17 require 'delegate'
18
19 module Google
20   class APIClient
21     ##
22     # Generates access tokens using the JWT assertion profile. Requires a
23     # service account & access to the private key.
24     #
25     # @example Using Signet
26     #
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!
35     #   client.execute(...)
36     #
37     # @deprecated
38     #  Service accounts are now supported directly in Signet
39     # @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
40     class JWTAsserter
41       # @return [String] ID/email of the issuing party
42       attr_accessor :issuer
43       # @return [Fixnum] How long, in seconds, the assertion is valid for
44       attr_accessor :expiry
45       # @return [Fixnum] Seconds to expand the issued at/expiry window to account for clock skew
46       attr_accessor :skew
47       # @return [String] Scopes to authorize
48       attr_reader :scope
49       # @return [String,OpenSSL::PKey] key for signing assertions
50       attr_writer :key
51       # @return [String] Algorithm used for signing
52       attr_accessor :algorithm
53       
54       ##
55       # Initializes the asserter for a service account.
56       #
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")
67         self.issuer = issuer
68         self.scope = scope
69         self.expiry = 60 # 1 min default 
70         self.skew = 60      
71         self.key = key
72         self.algorithm = algorithm
73       end
74
75       ##
76       # Set the scopes to authorize
77       #
78       # @param [String, Array] new_scope
79       #   Scopes to authorize. May be a space delimited string or array of strings
80       def scope=(new_scope)
81         case new_scope
82         when Array
83           @scope = new_scope.join(' ')
84         when String
85           @scope = new_scope
86         when nil
87           @scope = ''
88         else
89           raise TypeError, "Expected Array or String, got #{new_scope.class}"
90         end
91       end
92       
93       ##
94       # Request a new access token.
95       # 
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 
101       #
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)
106         return authorization
107       end
108       
109       ##
110       # Builds a Signet OAuth2 client
111       #
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,
118           :issuer => @issuer,
119           :signing_key => @key,
120           :signing_algorithm => @algorithm,
121           :person => person
122         )
123       end      
124     end
125   end
126 end