module JWTEasy

JWTEasy is a simple wrapper for the JWT gem that hopes to make generating and consuming various types of JSON web tokens a little easier.

Usage

Generating a plain token without encryption might look something like:

token = JWTEasy.encode(id: 'some-identifying-information')

You'd likely want to configure things before though:

# config/initializers/jwt_easy.rb
JWTEasy.configure do |config|
  config.not_before_time  = 3_600
  config.secret           = ENV['JWT_EASY_SECRET']
  config.algorithm        = JWTEasy::ALGORITHM_HMAC_HS256
end

Of course you're able to consume tokens just as easily:

JWTEasy.decode(token).id #=> 'some-identifying-information'

@see JWTEasy.encode @see JWTEasy.decode @see JWTEasy.configure

Constants

ALGORITHM_HMAC_HS256

Algorithm identifiers for supported algorithms.

CLAIM_EXPIRATION_TIME

Short-names for various supported claim types.

CLAIM_NOT_BEFORE_TIME
VERSION

Public Instance Methods

configuration() click to toggle source

Gets the configuration object.

If none was set, a new configuration object is instantiated and returned.

@return [Configuration] the configuration object

@see Configuration

# File lib/jwt_easy.rb, line 47
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Allows for configuring the library using a block.

@example Configuration using a block

JWTEasy.configure do |config|
  # ...
end

@see Configuration

# File lib/jwt_easy.rb, line 59
def configure
  yield(configuration) if block_given?
end
decode(token) click to toggle source

Instantiates a new decoder with the provided data and the global configuration object.

@example Decode a token

JWTEasy.decode(token)

@param [String] token the token to be decoded

@return [Result] Result of the decode

@see Encoder

# File lib/jwt_easy.rb, line 87
def decode(token)
  Decoder.new(token, configuration).decode
end
encode(data) click to toggle source

Instantiates a new encoder and encodes the provided data and the global configuration object.

@example Generate a token from some data

JWTEasy.encode(id: 'some-identifying-information')

@param [Object] data the data to be encoded in the token

@return [String] JSON web token

@see Encoder

# File lib/jwt_easy.rb, line 73
def encode(data)
  Encoder.new(data, configuration).encode
end