Use JWT support in signet, ignore broken PKCS12 tests on jruby
[arvados.git] / 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     # @example Deprecated version
38     #
39     #    client = Google::APIClient.new
40     #    key = Google::APIClient::PKCS12.load_key('client.p12', 'notasecret')
41     #    service_account = Google::APIClient::JWTAsserter.new(
42     #     '123456-abcdef@developer.gserviceaccount.com',
43     #     'https://www.googleapis.com/auth/prediction',
44     #     key)
45     #    client.authorization = service_account.authorize
46     #    client.execute(...)
47     #
48     # @deprecated
49     #  Service accounts are now supported directly in Signet
50     # @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
51     class JWTAsserter
52       # @return [String] ID/email of the issuing party
53       attr_accessor :issuer
54       # @return [Fixnum] How long, in seconds, the assertion is valid for
55       attr_accessor :expiry
56       # @return [Fixnum] Seconds to expand the issued at/expiry window to account for clock skew
57       attr_accessor :skew
58       # @return [String] Scopes to authorize
59       attr_reader :scope
60       # @return [String,OpenSSL::PKey] key for signing assertions
61       attr_writer :key
62       # @return [String] Algorithm used for signing
63       attr_accessor :algorithm
64       
65       ##
66       # Initializes the asserter for a service account.
67       #
68       # @param [String] issuer
69       #    Name/ID of the client issuing the assertion
70       # @param [String, Array] scope
71       #   Scopes to authorize. May be a space delimited string or array of strings
72       # @param [String,OpenSSL::PKey] key
73       #   Key for signing assertions
74       # @param [String] algorithm
75       #   Algorithm to use, either 'RS256' for RSA with SHA-256 
76       #   or 'HS256' for HMAC with SHA-256
77       def initialize(issuer, scope, key, algorithm = "RS256")
78         self.issuer = issuer
79         self.scope = scope
80         self.expiry = 60 # 1 min default 
81         self.skew = 60      
82         self.key = key
83         self.algorithm = algorithm
84       end
85
86       ##
87       # Set the scopes to authorize
88       #
89       # @param [String, Array] new_scope
90       #   Scopes to authorize. May be a space delimited string or array of strings
91       def scope=(new_scope)
92         case new_scope
93         when Array
94           @scope = new_scope.join(' ')
95         when String
96           @scope = new_scope
97         when nil
98           @scope = ''
99         else
100           raise TypeError, "Expected Array or String, got #{new_scope.class}"
101         end
102       end
103       
104       ##
105       # Request a new access token.
106       # 
107       # @param [String] person
108       #   Email address of a user, if requesting a token to act on their behalf
109       # @param [Hash] options
110       #   Pass through to Signet::OAuth2::Client.fetch_access_token
111       # @return [Signet::OAuth2::Client] Access token 
112       #
113       # @see Signet::OAuth2::Client.fetch_access_token!
114       def authorize(person = nil, options={})
115         authorization = self.to_authorization
116         authorization.fetch_access_token!(options)
117         return authorization
118       end
119       
120       ##
121       # Builds a Signet OAuth2 client
122       #
123       # @return [Signet::OAuth2::Client] Access token 
124       def to_authorization(person = nil)
125         return Signet::OAuth2::Client.new(
126           :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
127           :audience => 'https://accounts.google.com/o/oauth2/token',
128           :scope => self.scope,
129           :issuer => @issuer,
130           :signing_key => @key,
131           :signing_algorithm => @algorithm,
132           :person => person
133         )
134       end      
135     end
136   end
137 end