Class: Google::Auth::IDTokens::KeyInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/googleauth/id_tokens/key_sources.rb

Overview

A public key used for verifying ID tokens.

This includes the public key data, ID, and the algorithm used for signature verification. RSA and Elliptical Curve (EC) keys are supported.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, key: nil, algorithm: nil) ⇒ KeyInfo

Create a public key info structure.

Parameters:

  • id (String) (defaults to: nil)

    The key ID.

  • key (OpenSSL::PKey::RSA, OpenSSL::PKey::EC) (defaults to: nil)

    The key itself.

  • algorithm (String) (defaults to: nil)

    The algorithm (normally ‘RS256` or `ES256`)



57
58
59
60
61
# File 'lib/googleauth/id_tokens/key_sources.rb', line 57

def initialize id: nil, key: nil, algorithm: nil
  @id = id
  @key = key
  @algorithm = algorithm
end

Instance Attribute Details

#algorithmString (readonly)

The signature algorithm. (normally ‘RS256` or `ES256`)

Returns:

  • (String)


79
80
81
# File 'lib/googleauth/id_tokens/key_sources.rb', line 79

def algorithm
  @algorithm
end

#idString (readonly)

The key ID.

Returns:

  • (String)


67
68
69
# File 'lib/googleauth/id_tokens/key_sources.rb', line 67

def id
  @id
end

#keyOpenSSL::PKey::RSA, OpenSSL::PKey::EC (readonly)

The key itself.

Returns:

  • (OpenSSL::PKey::RSA, OpenSSL::PKey::EC)


73
74
75
# File 'lib/googleauth/id_tokens/key_sources.rb', line 73

def key
  @key
end

Class Method Details

.from_jwk(jwk) ⇒ KeyInfo

Create a KeyInfo from a single JWK, which may be given as either a hash or an unparsed JSON string.

Parameters:

  • jwk (Hash, String)

    The JWK specification.

Returns:

Raises:

  • (KeySourceError)

    If the key could not be extracted from the JWK.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/googleauth/id_tokens/key_sources.rb', line 91

def from_jwk jwk
  jwk = symbolize_keys ensure_json_parsed jwk
  key = case jwk[:kty]
        when "RSA"
          extract_rsa_key jwk
        when "EC"
          extract_ec_key jwk
        when nil
          raise KeySourceError, "Key type not found"
        else
          raise KeySourceError, "Cannot use key type #{jwk[:kty]}"
        end
  new id: jwk[:kid], key: key, algorithm: jwk[:alg]
end

.from_jwk_set(jwk_set) ⇒ Array<KeyInfo>

Create an array of KeyInfo from a JWK Set, which may be given as either a hash or an unparsed JSON string.

Parameters:

  • jwk (Hash, String)

    The JWK Set specification.

Returns:

Raises:

  • (KeySourceError)

    If a key could not be extracted from the JWK Set.



115
116
117
118
119
120
# File 'lib/googleauth/id_tokens/key_sources.rb', line 115

def from_jwk_set jwk_set
  jwk_set = symbolize_keys ensure_json_parsed jwk_set
  jwks = jwk_set[:keys]
  raise KeySourceError, "No keys found in jwk set" unless jwks
  jwks.map { |jwk| from_jwk jwk }
end