Class: RailsJwtAuth::Jwt::Manager

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

Class Method Summary collapse

Class Method Details

.decode(token) ⇒ Object

Decodes the JWT with the signed secret

“exp”=>148…, “iss”=>“RJA”, “alg”=>“HS256”


18
19
20
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 18

def self.decode(token)
  JWT.decode(token, secret_key_base)
end

.encode(payload) ⇒ Object

Encodes and signs JWT Payload with expiration



11
12
13
14
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 11

def self.encode(payload)
  payload.reverse_merge!(meta)
  JWT.encode(payload, secret_key_base)
end

.expired?(payload) ⇒ Boolean

Validates if the token is expired by exp parameter

Returns:

  • (Boolean)


36
37
38
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 36

def self.expired?(payload)
  Time.at(payload['exp']) < Time.now
end

.metaObject

Default options to be encoded in the token



28
29
30
31
32
33
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 28

def self.meta
  {
    exp: RailsJwtAuth.jwt_expiration_time.from_now.to_i,
    iss: RailsJwtAuth.jwt_issuer
  }
end

.secret_key_baseObject



6
7
8
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 6

def self.secret_key_base
  Rails.application.secrets.secret_key_base || Rails.application.credentials.secret_key_base
end

.valid_payload?(payload) ⇒ Boolean

Validates the payload hash for expiration and meta claims

Returns:

  • (Boolean)


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

def self.valid_payload?(payload)
  payload && !expired?(payload) && payload['iss'] == meta[:iss]
end