Class: Google::APIClient::JWTAsserter
- Inherits:
-
Object
- Object
- Google::APIClient::JWTAsserter
- Defined in:
- lib/google/api_client/service_account.rb
Overview
Generates access tokens using the JWT assertion profile. Requires a service account & access to the private key.
Instance Attribute Summary collapse
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#issuer ⇒ Object
Returns the value of attribute issuer.
-
#key ⇒ Object
writeonly
Sets the attribute key.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
-
#authorize(person = nil, options = {}) ⇒ Signet::OAuth2::Client
Request a new access token.
-
#initialize(issuer, scope, key) ⇒ JWTAsserter
constructor
Initializes the asserter for a service account.
-
#to_jwt(person = nil) ⇒ String
Builds & signs the assertion.
Constructor Details
#initialize(issuer, scope, key) ⇒ JWTAsserter
Initializes the asserter for a service account.
68 69 70 71 72 73 |
# File 'lib/google/api_client/service_account.rb', line 68 def initialize(issuer, scope, key) self.issuer = issuer self.scope = scope self.expiry = 60 # 1 min default self.key = key end |
Instance Attribute Details
#expiry ⇒ Object
Returns the value of attribute expiry.
55 56 57 |
# File 'lib/google/api_client/service_account.rb', line 55 def expiry @expiry end |
#issuer ⇒ Object
Returns the value of attribute issuer.
55 56 57 |
# File 'lib/google/api_client/service_account.rb', line 55 def issuer @issuer end |
#key=(value) ⇒ Object (writeonly)
Sets the attribute key
57 58 59 |
# File 'lib/google/api_client/service_account.rb', line 57 def key=(value) @key = value end |
#scope ⇒ Object
Returns the value of attribute scope.
56 57 58 |
# File 'lib/google/api_client/service_account.rb', line 56 def scope @scope end |
Instance Method Details
#authorize(person = nil, options = {}) ⇒ Signet::OAuth2::Client
Request a new access token.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/google/api_client/service_account.rb', line 122 def (person = nil, ={}) assertion = self.to_jwt(person) = Signet::OAuth2::Client.new( :token_credential_uri => 'https://accounts.google.com/o/oauth2/token' ) .grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer' .extension_parameters = { :assertion => assertion } .fetch_access_token!() return end |
#to_jwt(person = nil) ⇒ String
Builds & signs the assertion.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/google/api_client/service_account.rb', line 99 def to_jwt(person=nil) now = Time.new assertion = { "iss" => @issuer, "scope" => self.scope, "aud" => "https://accounts.google.com/o/oauth2/token", "exp" => (now + expiry).to_i, "iat" => now.to_i } assertion['prn'] = person unless person.nil? return JWT.encode(assertion, @key, "RS256") end |