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”


14
15
16
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 14

def self.decode(token)
  JWT.decode(token, Rails.application.secrets.secret_key_base)
end

.encode(payload) ⇒ Object

Encodes and signs JWT Payload with expiration



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

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

.expired?(payload) ⇒ Boolean

Validates if the token is expired by exp parameter

Returns:

  • (Boolean)


32
33
34
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 32

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

.metaObject

Default options to be encoded in the token



24
25
26
27
28
29
# File 'lib/rails_jwt_auth/jwt/manager.rb', line 24

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

.valid_payload?(payload) ⇒ Boolean

Validates the payload hash for expiration and meta claims

Returns:

  • (Boolean)


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

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