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

.from_jwt(jwt_string, key_or_client) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openid_connect/response_object/id_token.rb', line 29

def from_jwt(jwt_string, key_or_client)
  attributes = case key_or_client
  when Client
    resource_request do
      http_client.post key_or_client.check_session_uri, :id_token => jwt_string
    end
  else
    JWT.decode(jwt_string, key_or_client).with_indifferent_access
  end
  new attributes
end

.resource_requestObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/openid_connect/response_object/id_token.rb', line 41

def resource_request
  res = yield
  case res.status
  when 200
    JSON.parse(res.body).with_indifferent_access
  when 400
    raise BadRequest.new('Check Session Faild', res)
  else
    raise HttpError.new(res.status, 'Unknown HttpError', res)
  end
end

Instance Method Details

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



24
25
26
# File 'lib/openid_connect/response_object/id_token.rb', line 24

def to_jwt(key, algorithm = 'RS256')
  JWT.encode as_json, key, algorithm
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