Class: Himari::IdToken

Inherits:
Object
  • Object
show all
Defined in:
lib/himari/id_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(claims:, client_id:, nonce:, signing_key:, issuer:, access_token: nil, time: Time.now, lifetime: 3600) ⇒ IdToken

Returns a new instance of IdToken.



20
21
22
23
24
25
26
27
28
29
# File 'lib/himari/id_token.rb', line 20

def initialize(claims:, client_id:, nonce:, signing_key:, issuer:, access_token: nil, time: Time.now, lifetime: 3600)
  @claims = claims
  @client_id = client_id
  @nonce = nonce
  @signing_key = signing_key
  @issuer = issuer
  @access_token = access_token
  @time = time
  @lifetime = lifetime
end

Instance Attribute Details

#claimsObject (readonly)

Returns the value of attribute claims.



31
32
33
# File 'lib/himari/id_token.rb', line 31

def claims
  @claims
end

#nonceObject (readonly)

Returns the value of attribute nonce.



31
32
33
# File 'lib/himari/id_token.rb', line 31

def nonce
  @nonce
end

#signing_keyObject (readonly)

Returns the value of attribute signing_key.



31
32
33
# File 'lib/himari/id_token.rb', line 31

def signing_key
  @signing_key
end

Class Method Details

.from_authz(authz, **kwargs) ⇒ Object

Parameters:



9
10
11
12
13
14
15
16
17
18
# File 'lib/himari/id_token.rb', line 9

def self.from_authz(authz, **kwargs)
  
  new(
    claims: authz.claims,
    client_id: authz.client_id,
    nonce: authz.nonce,
    lifetime: authz.lifetime.is_a?(Integer) ? authz.lifetime : authz.lifetime.id_token, # compat
    **kwargs
  )
end

Instance Method Details

#at_hashObject



48
49
50
51
52
# File 'lib/himari/id_token.rb', line 48

def at_hash
  return nil unless @access_token
  dgst = @signing_key.hash_function.digest(@access_token)
  Base64.urlsafe_encode64(dgst[0, dgst.size/2], padding: false)
end

#final_claimsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/himari/id_token.rb', line 33

def final_claims
  # https://openid.net/specs/openid-connect-core-1_0.html#IDToken
  claims.merge(
    iss: @issuer,
    aud: @client_id,
    iat: @time.to_i,
    nbf: @time.to_i,
    exp: (@time + @lifetime).to_i,
  ).merge(
    @nonce ? { nonce: @nonce } : {}
  ).merge(
    @access_token ? { at_hash: at_hash } : {}
  )
end

#to_jwtObject



54
55
56
57
58
# File 'lib/himari/id_token.rb', line 54

def to_jwt
  jwt = JSON::JWT.new(final_claims)
  jwt.kid = @signing_key.id
  jwt.sign(@signing_key.pkey, @signing_key.alg.to_sym).to_s
end