Class: JwtTokenAuth::AuthToken

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Claims
Defined in:
lib/jwt_token_auth/auth_token.rb

Overview

JWT doesn’t encrypt the payload, it only sings it. That means that you should not store secret information in the payload but information that should be verified.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Claims

#claims_symbols, included

Constructor Details

#initialize(params = {}) ⇒ AuthToken

Returns a new instance of AuthToken.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jwt_token_auth/auth_token.rb', line 22

def initialize(params = {})
  params = {
    exp: Time.new.to_i + JwtTokenAuth.configuration.expiration_time,
    iat: Time.now.to_i
  }.merge(convert_keys_into_symbols(params))

  @iss = params[:iss]
  @sub = params[:sub]
  @aud = params[:aud]
  @exp = params[:exp]
  @nbf = params[:nbf]
  @iat = params[:iat]
  @jti = params[:jti]

  __setobj__(params)
end

Instance Attribute Details

#audString

The audience of the token

Returns:

  • (String)

    the current value of aud



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def aud
  @aud
end

#expUnixTimestamp

The expiration

Returns:

  • (UnixTimestamp)

    the current value of exp



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def exp
  @exp
end

#iatUnixTimestamp

The time the token was issued

Returns:

  • (UnixTimestamp)

    the current value of iat



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def iat
  @iat
end

#issString

The issue of the token

Returns:

  • (String)

    the current value of iss



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def iss
  @iss
end

#jtiString

Unique identifier for the token

Returns:

  • (String)

    the current value of jti



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def jti
  @jti
end

#nbfString

Time before which the token must not be accepted for processing

Returns:

  • (String)

    the current value of nbf



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def nbf
  @nbf
end

#subString

The subject of the token

Returns:

  • (String)

    the current value of sub



17
18
19
# File 'lib/jwt_token_auth/auth_token.rb', line 17

def sub
  @sub
end

Class Method Details

.decode(encoded_token) ⇒ Object



52
53
54
55
56
# File 'lib/jwt_token_auth/auth_token.rb', line 52

def decode(encoded_token)
  AuthToken.new(
    JWT.decode(encoded_token, JwtTokenAuth.configuration.secret)[0]
  )
end

Instance Method Details

#encodeObject



47
48
49
# File 'lib/jwt_token_auth/auth_token.rb', line 47

def encode
  JWT.encode(self, secret)
end

#payloadObject



43
44
45
# File 'lib/jwt_token_auth/auth_token.rb', line 43

def payload
  self.except *claims_symbols
end

#secretObject



39
40
41
# File 'lib/jwt_token_auth/auth_token.rb', line 39

def secret
  JwtTokenAuth.configuration.secret
end