0.0
Low commit activity in last 3 years
No release in over a year
Encodes ruby hashes as encrypted and URL-safe tokens.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 10.0
~> 3.0

Runtime

>= 6.0.0, < 8.0
 Project Readme

Gem Version Build Status

Base64Token

This gem allows you to take a ruby hash and turn it into an encrypted token that you can later convert back to your original hash.

The contents of the tokenized hash are encrypted, so the bearer of the token will not be able to see or modify the contents of the hash.

Installation

Add this line to your application's Gemfile:

gem 'base64_token', '~> 1.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install base64_token

Usage

# Set the encryption key used for your token. You should store that somewhere
# if you want to recognize your own tokens at a later time.
Base64Token.encryption_key = Base64Token.generate_key
=> "BgPrrt4Ltd7rYlsloSEs+cVuxcaLdjkTRFAjKWViIWo="

token = Base64Token.generate(user_id: 42, valid_to: '2017-01-19T13:37:00')
=> "fTsJg-2iOA5F3YC2i5tlGcWUE-npnZwSEezA-yRfhLL8aV_KE6AuGIZH5YAdgE-lLhiNUmuWCFkxlgUJy7TjdmJFscxzeS-l3CTD1or6nwR0-zHA7B-Q"

Base64Token.parse(token)
=> {:user_id=>42, :valid_to=>"2017-01-19T13:37:00"}

Note that:

  • your hash is converted to JSON intermediately, so you can (re)store anything that you can serialize to JSON
  • to ensure consistency before and after deserialization, all hash keys have to be symbols

Development

You can run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kaeuferportal/base64_token.