Class: Google::Auth::Extras::ServiceAccountJWTCredential
- Inherits:
-
Signet::OAuth2::Client
- Object
- Signet::OAuth2::Client
- Google::Auth::Extras::ServiceAccountJWTCredential
- Defined in:
- lib/google/auth/extras/service_account_jwt_credential.rb
Overview
This credential issues JWTs signed a service account.
Instance Method Summary collapse
- #fetch_access_token ⇒ Object
-
#initialize(email_address:, target_audience:, base_credentials: nil, delegate_email_addresses: nil, issuer: nil, lifetime: 3600, subject: nil, universe_domain: 'googleapis.com') ⇒ ServiceAccountJWTCredential
constructor
A credential that obtains a signed JWT from Google for a service account.
- #inspect ⇒ Object
Constructor Details
#initialize(email_address:, target_audience:, base_credentials: nil, delegate_email_addresses: nil, issuer: nil, lifetime: 3600, subject: nil, universe_domain: 'googleapis.com') ⇒ ServiceAccountJWTCredential
A credential that obtains a signed JWT from Google for a service account.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/google/auth/extras/service_account_jwt_credential.rb', line 42 def initialize( email_address:, target_audience:, base_credentials: nil, delegate_email_addresses: nil, issuer: nil, lifetime: 3600, subject: nil, universe_domain: 'googleapis.com' ) super( client_id: target_audience, target_audience: target_audience, universe_domain: base_credentials&.universe_domain || universe_domain, ) @iam_credentials_service = Google::Apis::IamcredentialsV1::IAMCredentialsService.new.tap do |ics| ics. = base_credentials if base_credentials end @jwt_issuer = issuer || email_address @jwt_lifetime = lifetime @jwt_subject = subject || email_address @sa_delegates = Array(delegate_email_addresses).map do |email| transform_email_to_name(email) end @sa_name = transform_email_to_name(email_address) end |
Instance Method Details
#fetch_access_token ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/google/auth/extras/service_account_jwt_credential.rb', line 73 def fetch_access_token(*) now = Time.now.to_i request = Google::Apis::IamcredentialsV1::SignJwtRequest.new( payload: JSON.dump( aud: target_audience, exp: now + @jwt_lifetime, iat: now, iss: @jwt_issuer, sub: @jwt_subject, ), ) # The Google SDK doesn't like nil repeated values, but be careful with others as well. request.delegates = @sa_delegates unless @sa_delegates.empty? response = @iam_credentials_service.sign_service_account_jwt(@sa_name, request) { id_token: response.signed_jwt, } end |
#inspect ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/google/auth/extras/service_account_jwt_credential.rb', line 96 def inspect "#<#{self.class.name}" \ " @expires_at=#{expires_at.inspect}" \ " @id_token=#{@id_token ? '[REDACTED]' : 'nil'}" \ " @jwt_issuer=#{@jwt_issuer.inspect}" \ " @jwt_lifetime=#{@jwt_lifetime.inspect}" \ " @jwt_subject=#{@jwt_subject.inspect}" \ " @sa_delegates=#{@sa_delegates.inspect}" \ " @sa_name=#{@sa_name.inspect}" \ " @target_audience=#{@target_audience.inspect}" \ '>' end |