Code Monkey home page Code Monkey logo

super_token's Introduction

Build Status Gem Version Dependency Status Code Climate Test Coverage

SuperToken

SuperToken is a fork of HasSecureToken module which is provides an easy way to generate uniques random tokens for any model in ruby on rails. SuperToken gives you a way to prefix token with string and change token length.

Installation

Add this line to your application's Gemfile:

gem 'super_token'

And then run:

$ bundle

Or install it yourself as:

$ gem install super_token

Setting your Model

The first step is to generate a migration in order to add the token key field.

rails g migration AddTokenToAPIKey token:string secret_key:string public_key:string
=>
   invoke  active_record
   create    db/migrate/20150424010931_add_token_to_api_key_.rb

Then run rake db:migrate in order to update users table in the database. The next step is to add has_secure_token to the model:

# Schema: APIKey(token:string, secret_key:string, public_key:string)
class APIKey < ActiveRecord::Base
  has_secure_token
end

# test regular has_secure_token features
api_key = APIKey.new
api_key.save
api_key.token # => "pX27zsMN2ViQKta1bGfLmVJE"
api_key.regenerate_token # => true

To use a custom column to store the token key field you can specify the column_name option. See example above (e.g: auth_token):

# Schema: APIKey(token:string, secret_key:string, public_key:string)
class APIKey < ActiveRecord::Base
  # super_token add two options for has_secure_token (prefix and length)
  has_secure_token :secret_key, prefix: 'sk_', length: 48
  has_secure_token :public_key, prefix: 'pk_', length: 48
end


api_key = APIKey.new
api_key.save
api_key.secret_key # => "sk_pX27zsMN2ViQKta1bGfLmVJEiQKta1bGfLmVJE"
api_key.public_key # => "pk_pX27zsMN2ViQKta1bGfLmVJEiQKta1bGfLmVJE"
api_key.regenerate_secret_key # => true

Running tests

Running

$ rake test

Should return

9 runs, 17 assertions, 0 failures, 0 errors, 0 skips

Contributing

  1. Fork it ( https://github.com/ecleel/super_token/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

super_token's People

Contributors

robertomiranda avatar ecleel avatar maksimabramchuk avatar marlonmantilla avatar pusewicz avatar rahij avatar

Watchers

James Cloos avatar Bogdan Timofte avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.