Class: Gr1d99Auth::JWT

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJWT

Returns a new instance of JWT.



7
8
9
# File 'lib/gr1d99_auth/jwt.rb', line 7

def initialize
  @config = Gr1d99Auth.configuration
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.decode(token) ⇒ Object



15
16
17
# File 'lib/gr1d99_auth/jwt.rb', line 15

def self.decode(token)
  new.decode(token)
end

.encode(payload) ⇒ Object



11
12
13
# File 'lib/gr1d99_auth/jwt.rb', line 11

def self.encode(payload)
  new.encode(payload)
end

Instance Method Details

#decode(token) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/gr1d99_auth/jwt.rb', line 30

def decode(token)
  key    = config.jwt_key
  verify = config.jwt_verify
  opts   = { algorithm: config.jwt_algorithm }

  ::JWT.decode(token, key, verify, opts)
end

#encode(payload) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/gr1d99_auth/jwt.rb', line 19

def encode(payload)
  if config.jwt_exp
    payload[:exp] = (Time.zone.now + config.jwt_exp.to_i).to_i
  end

  key = config.jwt_key
  algorithm = config.jwt_algorithm

  ::JWT.encode(payload, key, algorithm)
end