Class: IapAuthenticator::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/iap_authenticator/token.rb

Constant Summary collapse

TokenURI =
"https://www.googleapis.com/oauth2/v4/token"
JWTBearerType =
"urn:ietf:params:oauth:grant-type:jwt-bearer"

Class Method Summary collapse

Class Method Details

.generate_bearer_token(assertion) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/iap_authenticator/token.rb', line 6

def self.generate_bearer_token( assertion )
  uri = URI(TokenURI)
  begin
    res = Net::HTTP.post_form(uri, 'grant_type' => JWTBearerType, 'assertion' => assertion)
  rescue => e
    raise e
  end
  if res.code.to_i != 200
    error_description = JSON.parse(res.body)["error_description"]
    error = JSON.parse(res.body)["error"]
     raise("Request failed with error: #{error} and description: #{error_description}")
  end

  response_jwt = JSON.parse(res.body)["id_token"]
  return response_jwt
end