Module: JWTEasy

Defined in:
lib/jwt_easy.rb,
lib/jwt_easy/result.rb,
lib/jwt_easy/decoder.rb,
lib/jwt_easy/encoder.rb,
lib/jwt_easy/version.rb,
lib/jwt_easy/constants.rb,
lib/jwt_easy/configuration.rb

Overview

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'

Defined Under Namespace

Classes: Configuration, Decoder, Encoder, Result

Constant Summary collapse

VERSION =
'1.0.0'
CLAIM_EXPIRATION_TIME =

Short-names for various supported claim types.

:exp
CLAIM_NOT_BEFORE_TIME =
:nbf
ALGORITHM_HMAC_HS256 =

Algorithm identifiers for supported algorithms.

'HS256'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Gets the configuration object.

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

Returns:

See Also:



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

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Allows for configuring the library using a block.

Examples:

Configuration using a block

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

Yields:

See Also:



59
60
61
# File 'lib/jwt_easy.rb', line 59

def configure
  yield(configuration) if block_given?
end

.decode(token) ⇒ Result

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

Examples:

Decode a token

JWTEasy.decode(token)

Parameters:

  • token (String)

    the token to be decoded

Returns:

  • (Result)

    Result of the decode

See Also:



87
88
89
# File 'lib/jwt_easy.rb', line 87

def decode(token)
  Decoder.new(token, configuration).decode
end

.encode(data) ⇒ String

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

Examples:

Generate a token from some data

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

Parameters:

  • data (Object)

    the data to be encoded in the token

Returns:

  • (String)

    JSON web token

See Also:



73
74
75
# File 'lib/jwt_easy.rb', line 73

def encode(data)
  Encoder.new(data, configuration).encode
end