Class: OpenIDConnect::ResponseObject::IdToken

Inherits:
OpenIDConnect::ResponseObject show all
Defined in:
lib/openid_connect/response_object/id_token.rb

Defined Under Namespace

Classes: InvalidToken

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenIDConnect::ResponseObject

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

Constructor Details

#initialize(attributes = {}) ⇒ IdToken

Returns a new instance of IdToken.



11
12
13
14
15
16
17
# File 'lib/openid_connect/response_object/id_token.rb', line 11

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

Class Method Details

.decode(jwt_string, key_or_client) ⇒ Object



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

def decode(jwt_string, key_or_client)
  attributes = case key_or_client
  when Client
    OpenIDConnect::AccessToken.new(
      :client => key_or_client,
      :access_token => jwt_string
    ).id_token!
  else
    new JSON::JWT.decode(jwt_string, key_or_client)
  end
end

Instance Method Details

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



24
25
26
27
28
29
30
# File 'lib/openid_connect/response_object/id_token.rb', line 24

def to_jwt(key, algorithm = :RS256)
  token = JSON::JWT.new as_json
  if algorithm != :none
    token = token.sign key, algorithm
  end
  token.to_s
end

#verify!(client_id) ⇒ Object



19
20
21
22
# File 'lib/openid_connect/response_object/id_token.rb', line 19

def verify!(client_id)
  exp.to_i >= Time.now.to_i && aud == client_id or
  raise InvalidToken.new('Invalid audience or expired')
end