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
38
# File 'lib/jwt_token_auth/auth_token.rb', line 22

def initialize(params = {})
  params = convert_keys_into_symbols(params)

  @iss = params[:iss]
  @sub = params[:sub]
  @aud = params[:aud]
  @exp = params.fetch :exp do
    time = JwtTokenAuth.configuration.expiration_time.call().to_i
  end.tap { |time| params[:exp] = time }
  @nbf = params[:nbf]
  @iat = params.fetch :iat do
    Time.now.to_i
  end.tap { |time| params[:iat] = time }
  @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



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

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

Instance Method Details

#encodeObject



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

def encode
  JWT.encode(self, secret)
end

#payloadObject



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

def payload
  self.except *claims_symbols
end

#secretObject



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

def secret
  JwtTokenAuth.configuration.secret
end