Class: Userbin::JWT

Inherits:
Object
  • Object
show all
Defined in:
lib/userbin/jwt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt) ⇒ JWT

Returns a new instance of JWT.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/userbin/jwt.rb', line 7

def initialize(jwt)
  begin
    raise Userbin::SecurityError, 'Empty JWT' unless jwt
    @payload = ::JWT.decode(jwt, Userbin.config.api_secret, true) do |header|
      @header = header.with_indifferent_access
      Userbin.config.api_secret # used by the 'key finder' in the JWT gem
    end.with_indifferent_access
  rescue ::JWT::DecodeError => e
    raise Userbin::SecurityError.new(e)
  end
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



5
6
7
# File 'lib/userbin/jwt.rb', line 5

def header
  @header
end

#payloadObject

Returns the value of attribute payload.



5
6
7
# File 'lib/userbin/jwt.rb', line 5

def payload
  @payload
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/userbin/jwt.rb', line 19

def expired?
  Time.now.utc > Time.at(@header['exp']).utc
end

#merge!(payload = {}) ⇒ Object



23
24
25
# File 'lib/userbin/jwt.rb', line 23

def merge!(payload = {})
  @payload.merge!(payload)
end

#to_jsonObject



27
28
29
# File 'lib/userbin/jwt.rb', line 27

def to_json
  @payload
end

#to_tokenObject



31
32
33
# File 'lib/userbin/jwt.rb', line 31

def to_token
  ::JWT.encode(@payload, Userbin.config.api_secret, "HS256", @header)
end