| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  | # Copyright 2010 Google Inc. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'jwt' | 
					
						
							|  |  |  | require 'signet/oauth_2/client' | 
					
						
							| 
									
										
										
										
											2012-10-30 20:18:12 +00:00
										 |  |  | require 'delegate' | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Google | 
					
						
							|  |  |  |   class APIClient | 
					
						
							|  |  |  |     ## | 
					
						
							|  |  |  |     # Generates access tokens using the JWT assertion profile. Requires a | 
					
						
							|  |  |  |     # service account & access to the private key. | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |     # @example Using Signet | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret') | 
					
						
							|  |  |  |     #   client.authorization = Signet::OAuth2::Client.new( | 
					
						
							|  |  |  |     #     :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', | 
					
						
							|  |  |  |     #     :audience => 'https://accounts.google.com/o/oauth2/token', | 
					
						
							| 
									
										
										
										
											2013-01-03 00:12:47 +00:00
										 |  |  |     #     :scope => 'https://www.googleapis.com/auth/prediction', | 
					
						
							|  |  |  |     #     :issuer => '123456-abcdef@developer.gserviceaccount.com', | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |     #     :signing_key => key) | 
					
						
							|  |  |  |     #   client.authorization.fetch_access_token! | 
					
						
							|  |  |  |     #   client.execute(...) | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # @example Deprecated version | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     #    client = Google::APIClient.new | 
					
						
							|  |  |  |     #    key = Google::APIClient::PKCS12.load_key('client.p12', 'notasecret') | 
					
						
							| 
									
										
										
										
											2012-11-02 19:50:06 +00:00
										 |  |  |     #    service_account = Google::APIClient::JWTAsserter.new( | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |     #     '123456-abcdef@developer.gserviceaccount.com', | 
					
						
							|  |  |  |     #     'https://www.googleapis.com/auth/prediction', | 
					
						
							|  |  |  |     #     key) | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |     #    client.authorization = service_account.authorize | 
					
						
							|  |  |  |     #    client.execute(...) | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |     # @deprecated | 
					
						
							|  |  |  |     #  Service accounts are now supported directly in Signet | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |     # @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount | 
					
						
							|  |  |  |     class JWTAsserter | 
					
						
							|  |  |  |       # @return [String] ID/email of the issuing party | 
					
						
							|  |  |  |       attr_accessor :issuer | 
					
						
							|  |  |  |       # @return [Fixnum] How long, in seconds, the assertion is valid for | 
					
						
							|  |  |  |       attr_accessor :expiry | 
					
						
							|  |  |  |       # @return [Fixnum] Seconds to expand the issued at/expiry window to account for clock skew | 
					
						
							|  |  |  |       attr_accessor :skew | 
					
						
							|  |  |  |       # @return [String] Scopes to authorize | 
					
						
							|  |  |  |       attr_reader :scope | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |       # @return [String,OpenSSL::PKey] key for signing assertions | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |       attr_writer :key | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |       # @return [String] Algorithm used for signing | 
					
						
							|  |  |  |       attr_accessor :algorithm | 
					
						
							|  |  |  |        | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |       ## | 
					
						
							|  |  |  |       # Initializes the asserter for a service account. | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [String] issuer | 
					
						
							|  |  |  |       #    Name/ID of the client issuing the assertion | 
					
						
							|  |  |  |       # @param [String, Array] scope | 
					
						
							|  |  |  |       #   Scopes to authorize. May be a space delimited string or array of strings | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |       # @param [String,OpenSSL::PKey] key | 
					
						
							|  |  |  |       #   Key for signing assertions | 
					
						
							|  |  |  |       # @param [String] algorithm | 
					
						
							|  |  |  |       #   Algorithm to use, either 'RS256' for RSA with SHA-256  | 
					
						
							|  |  |  |       #   or 'HS256' for HMAC with SHA-256 | 
					
						
							|  |  |  |       def initialize(issuer, scope, key, algorithm = "RS256") | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |         self.issuer = issuer | 
					
						
							|  |  |  |         self.scope = scope | 
					
						
							|  |  |  |         self.expiry = 60 # 1 min default  | 
					
						
							|  |  |  |         self.skew = 60      
 | 
					
						
							|  |  |  |         self.key = key | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |         self.algorithm = algorithm | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ## | 
					
						
							|  |  |  |       # Set the scopes to authorize | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [String, Array] new_scope | 
					
						
							|  |  |  |       #   Scopes to authorize. May be a space delimited string or array of strings | 
					
						
							|  |  |  |       def scope=(new_scope) | 
					
						
							|  |  |  |         case new_scope | 
					
						
							|  |  |  |         when Array | 
					
						
							|  |  |  |           @scope = new_scope.join(' ') | 
					
						
							|  |  |  |         when String | 
					
						
							|  |  |  |           @scope = new_scope | 
					
						
							|  |  |  |         when nil | 
					
						
							|  |  |  |           @scope = '' | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise TypeError, "Expected Array or String, got #{new_scope.class}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |       ## | 
					
						
							|  |  |  |       # Request a new access token. | 
					
						
							|  |  |  |       #  | 
					
						
							|  |  |  |       # @param [String] person | 
					
						
							|  |  |  |       #   Email address of a user, if requesting a token to act on their behalf | 
					
						
							|  |  |  |       # @param [Hash] options | 
					
						
							|  |  |  |       #   Pass through to Signet::OAuth2::Client.fetch_access_token | 
					
						
							|  |  |  |       # @return [Signet::OAuth2::Client] Access token  | 
					
						
							|  |  |  |       # | 
					
						
							| 
									
										
										
										
											2012-11-02 19:50:06 +00:00
										 |  |  |       # @see Signet::OAuth2::Client.fetch_access_token! | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |       def authorize(person = nil, options={}) | 
					
						
							| 
									
										
										
										
											2013-01-12 15:29:47 +00:00
										 |  |  |         authorization = self.to_authorization(person) | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |         authorization.fetch_access_token!(options) | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |         return authorization | 
					
						
							| 
									
										
										
										
											2012-10-30 20:18:12 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2012-11-02 19:50:06 +00:00
										 |  |  |       ## | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |       # Builds a Signet OAuth2 client | 
					
						
							| 
									
										
										
										
											2012-11-02 19:50:06 +00:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2013-01-02 19:50:45 +00:00
										 |  |  |       # @return [Signet::OAuth2::Client] Access token  | 
					
						
							|  |  |  |       def to_authorization(person = nil) | 
					
						
							|  |  |  |         return Signet::OAuth2::Client.new( | 
					
						
							|  |  |  |           :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', | 
					
						
							|  |  |  |           :audience => 'https://accounts.google.com/o/oauth2/token', | 
					
						
							|  |  |  |           :scope => self.scope, | 
					
						
							|  |  |  |           :issuer => @issuer, | 
					
						
							|  |  |  |           :signing_key => @key, | 
					
						
							|  |  |  |           :signing_algorithm => @algorithm, | 
					
						
							|  |  |  |           :person => person | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       end       | 
					
						
							| 
									
										
										
										
											2012-10-10 20:18:33 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |