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)



44
45
46
47
48
# File 'lib/googleauth/id_tokens/key_sources.rb', line 44

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)


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

def algorithm
  @algorithm
end

#idString (readonly)

The key ID.

Returns:

  • (String)


54
55
56
# File 'lib/googleauth/id_tokens/key_sources.rb', line 54

def id
  @id
end

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

The key itself.

Returns:

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


60
61
62
# File 'lib/googleauth/id_tokens/key_sources.rb', line 60

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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/googleauth/id_tokens/key_sources.rb', line 78

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.



102
103
104
105
106
107
# File 'lib/googleauth/id_tokens/key_sources.rb', line 102

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