class JWTEasy::Encoder

Encoder object for generating new tokens.

Attributes

configuration[R]
data[R]

Public Class Methods

new(data, configuration = nil) click to toggle source

Initializes a new encoder instance.

  • If no configuration object is passed or is nil, the value of JWTEasy.configuration is used as the configuration object

@param [Object] data the data to be encoded @param [Configuration] configuration the configuration object

# File lib/jwt_easy/encoder.rb, line 19
def initialize(data, configuration = nil)
  @data           = data
  @configuration  = configuration || JWTEasy.configuration
end

Public Instance Methods

encode() click to toggle source

Encodes the data with the configured options.

@return [String] the encoded token

# File lib/jwt_easy/encoder.rb, line 27
def encode
  JWT.encode(payload, configuration.secret, configuration.algorithm)
end
expiration_time() click to toggle source

Calculates the expiration time if configured.

@return [Integer] the expiration time

# File lib/jwt_easy/encoder.rb, line 48
def expiration_time
  Time.now.to_i + configuration.expiration_time
end
not_before_time() click to toggle source

Calculates the not before time if configured.

@return [Integer] the not before time

# File lib/jwt_easy/encoder.rb, line 55
def not_before_time
  Time.now.to_i - configuration.not_before_time
end
payload() click to toggle source

Determines the structure of the payload to be encoded.

@return [Object] the payload to be encoded

# File lib/jwt_easy/encoder.rb, line 34
def payload
  case configuration.claim
  when CLAIM_EXPIRATION_TIME
    { data: data, exp: expiration_time }
  when CLAIM_NOT_BEFORE_TIME
    { data: data, nbf: not_before_time }
  else
    data
  end
end