Description

Token is a library that generates and verifies cryptographically secure, signed
string tokens.

Install

Manually:

gem install token

or with Bundler (add to your Gemfile):

gem'token'

Usage

Setup

The Token class defaults to using the AES-246-CFB cipher with a randomly
generated key and initialization vector, and the payload format as a single
integer value. These defaults can be overridden as follows:

Verify a token

Modify the Payload Format

By default, the payload of the token is a single integer value. This is useful
when you are handling login, as it can specify the ID of the user with the
token. However, sometimes applications require tokens with more intricate
payloads.

To change the format of the payload to contain multiple values or different
formats, modify the format parameter. This parameter should be in the same
format as expected by Array.pack. The default value is 'L'.

Note that if the format only contains a single field, then the payload
argument to Token.generate can be a scalar. Otherwise, the argument must be
an array. The array will be automatically flattened when generating the token,
and verify will always return a flat array or scalar.

Instances of the Token Class

You can create instances of the Token class and use the instances to generate
and verify tokens. This is useful if you use tokens for multiple purposes in
your application and each purpose uses different cryptographic parameters or
payload formats.