Class: OpenIDConnect::ResponseObject::IdToken

Inherits:
ConnectObject show all
Includes:
JWTnizable
Defined in:
lib/openid_connect/response_object/id_token.rb

Defined Under Namespace

Classes: InvalidToken

Instance Attribute Summary collapse

Attributes inherited from ConnectObject

#raw_attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JWTnizable

#as_jwt

Methods inherited from ConnectObject

all_attributes, #all_attributes, #as_json, #require_at_least_one_attributes, #validate!

Constructor Details

#initialize(attributes = {}) ⇒ IdToken

Returns a new instance of IdToken.



14
15
16
17
18
19
# File 'lib/openid_connect/response_object/id_token.rb', line 14

def initialize(attributes = {})
  super
  (all_attributes - [:aud, :exp, :iat, :auth_time, :sub_jwk]).each do |key|
    self.send "#{key}=", self.send(key).try(:to_s)
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/openid_connect/response_object/id_token.rb', line 10

def access_token
  @access_token
end

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/openid_connect/response_object/id_token.rb', line 10

def code
  @code
end

Class Method Details

.decode(jwt_string, key) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/openid_connect/response_object/id_token.rb', line 55

def decode(jwt_string, key)
  if key == :self_issued
    decode_self_issued jwt_string
  else
    new JSON::JWT.decode jwt_string, key
  end
end

.decode_self_issued(jwt_string) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
# File 'lib/openid_connect/response_object/id_token.rb', line 63

def decode_self_issued(jwt_string)
  jwt = JSON::JWT.decode jwt_string, :skip_verification
  jwk = JSON::JWK.new jwt[:sub_jwk]
  raise InvalidToken.new('Missing sub_jwk') if jwk.blank?
  raise InvalidToken.new('Invalid subject') unless jwt[:sub] == jwk.thumbprint
  jwt.verify! jwk
  new jwt
end

.self_issued(attributes = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/openid_connect/response_object/id_token.rb', line 72

def self_issued(attributes = {})
  attributes[:sub_jwk] ||= JSON::JWK.new attributes.delete(:public_key)
  _attributes_ = {
    iss: 'https://self-issued.me',
    sub: JSON::JWK.new(attributes[:sub_jwk]).thumbprint
  }.merge(attributes)
  new _attributes_
end

Instance Method Details

#to_jwt(key, algorithm = :RS256, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openid_connect/response_object/id_token.rb', line 30

def to_jwt(key, algorithm = :RS256, &block)
  hash_length = algorithm.to_s[2, 3].to_i
  if access_token
    token = case access_token
    when Rack::OAuth2::AccessToken
      access_token.access_token
    else
      access_token
    end
    self.at_hash = left_half_hash_of token, hash_length
  end
  if code
    self.c_hash = left_half_hash_of code, hash_length
  end
  super
end

#verify!(expected = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/openid_connect/response_object/id_token.rb', line 21

def verify!(expected = {})
  exp.to_i > Time.now.to_i &&
  iss == expected[:issuer] &&
  Array(aud).include?(expected[:client_id]) && # aud(ience) can be a string or an array of strings
  nonce == expected[:nonce] or
  raise InvalidToken.new('Invalid ID Token')
end