simple_token 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +9 -0
- data/lib/simple_token/rails.rb +57 -0
- data/lib/simple_token/version.rb +3 -0
- data/lib/simple_token.rb +10 -0
- data/simple_token.gemspec +22 -0
- data/spec/app/models/post.rb +3 -0
- data/spec/app/models/user.rb +3 -0
- data/spec/config/database.yml +4 -0
- data/spec/db/schema.rb +17 -0
- data/spec/factories.rb +13 -0
- data/spec/simple_token_spec.rb +90 -0
- data/spec/spec_helper.rb +26 -0
- metadata +102 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rake'
|
4
|
+
gem 'sqlite3'
|
5
|
+
gem 'rspec', :group => [ :test, :development ]
|
6
|
+
gem 'nyan-cat-formatter', :group => [ :test, :development ]
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'simplecov', :require => false
|
10
|
+
gem 'factory_girl'
|
11
|
+
end
|
12
|
+
|
13
|
+
# Specify your gem's dependencies in simple_token.gemspec
|
14
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Enrique Vidal
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# SimpleToken
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/cloverinteractive/simple_token.png?branch=master)](http://travis-ci.org/cloverinteractive/simple_token)
|
4
|
+
|
5
|
+
Add an unique 64 chars sha2 or secure random token to your active record models.
|
6
|
+
|
7
|
+
**Note:** You should definately try `postgresql` native `UUID` support, if that's not a possibility then `simple_token` has your back.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'simple_token'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install simple_token
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
SimpleToken only has two dependencies, `activerecord` and `activesupport` and it's usage is the same as every `acts_as_` rails plugin out there.
|
26
|
+
|
27
|
+
By default SimpleToken expects you to have a `token` field in your table, but I'll show you how to customize this in a bit.
|
28
|
+
|
29
|
+
### Default settings
|
30
|
+
|
31
|
+
Just a simple call inside your model like this:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Post < ActiveRecord::Base
|
35
|
+
has_simple_token
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Custom settings
|
40
|
+
|
41
|
+
By default SimpleToken, uses sha2 as it's digest algorithm and expects a token field to exist in your table but we can change this:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class User < ActiveRecord::Base
|
45
|
+
has_simple_token :token_field => :secret_hash, :digest_type => :secure_random
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
This model will now use `secure_random` to generate the secret token and it will write it to the `secret_hash` field on the users table.
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module SimpleToken
|
2
|
+
module Rails
|
3
|
+
def has_simple_token( options={} )
|
4
|
+
cattr_accessor :token_field, :digest_type
|
5
|
+
|
6
|
+
return if included_modules.include? InstanceMethods
|
7
|
+
|
8
|
+
self.token_field = options[:token_field] || :token
|
9
|
+
self.digest_type = options[:digest_type] || :sha2
|
10
|
+
|
11
|
+
include InstanceMethods
|
12
|
+
|
13
|
+
define_token_getter
|
14
|
+
end
|
15
|
+
|
16
|
+
def define_token_getter
|
17
|
+
define_method( token_field ) do
|
18
|
+
return self[ token_field ] unless self[ token_field ].blank?
|
19
|
+
|
20
|
+
new_token = case digest_type
|
21
|
+
when :sha2 then create_sha2_token
|
22
|
+
when :secure_random then create_secure_random_token
|
23
|
+
end
|
24
|
+
|
25
|
+
self[ token_field ] = new_token
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
extend ActiveSupport::Concern
|
31
|
+
|
32
|
+
included do
|
33
|
+
private
|
34
|
+
|
35
|
+
def create_sha2_token
|
36
|
+
token_lookup do
|
37
|
+
salt = rand( 36**12 ).to_s 36
|
38
|
+
seed_content = "#{ salt }#{ Time.now.to_i }#{ self.hash }".scan( /./ ).shuffle.join
|
39
|
+
new_token = Digest::SHA2.hexdigest seed_content
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_secure_random_token
|
44
|
+
token_lookup { SecureRandom.hex 32 }
|
45
|
+
end
|
46
|
+
|
47
|
+
def token_lookup( &block )
|
48
|
+
begin
|
49
|
+
new_token = block.call
|
50
|
+
end while self.class.exists? token_field => new_token
|
51
|
+
|
52
|
+
new_token
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/simple_token.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "simple_token/version"
|
2
|
+
require "simple_token/rails"
|
3
|
+
|
4
|
+
module SimpleToken
|
5
|
+
def self.included( base )
|
6
|
+
base.extend Rails
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Base.send( :include, SimpleToken ) if defined?( ActiveRecord::Base ) and defined?( ActiveSupport::Concern )
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_token/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "simple_token"
|
8
|
+
gem.version = SimpleToken::VERSION
|
9
|
+
gem.authors = ["Enrique Vidal"]
|
10
|
+
gem.email = ["enrique@cloverinteractive.com"]
|
11
|
+
gem.description = %q{Easily create unique tokens in active record models}
|
12
|
+
gem.summary = %q{Simple token creation gem}
|
13
|
+
gem.homepage = "http://cloverinteractive.github.com/simple_token/"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'activerecord'
|
21
|
+
gem.add_development_dependency 'activesupport'
|
22
|
+
end
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
create_table "posts", :force => true do |t|
|
3
|
+
t.string "title"
|
4
|
+
t.string "token"
|
5
|
+
|
6
|
+
t.datetime "created_at"
|
7
|
+
t.datetime "updated_at"
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table "users", :force => true do |t|
|
11
|
+
t.string "username"
|
12
|
+
t.string "secret_hash"
|
13
|
+
|
14
|
+
t.datetime "created_at"
|
15
|
+
t.datetime "updated_at"
|
16
|
+
end
|
17
|
+
end
|
data/spec/factories.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module SimpleToken
|
4
|
+
describe Rails do
|
5
|
+
context 'Default settings' do
|
6
|
+
let( :post ) { build :post }
|
7
|
+
|
8
|
+
it 'should respond to token' do
|
9
|
+
post.should respond_to( :token )
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should generate an unique 64 chars long SHA2 digest' do
|
13
|
+
token = post.token
|
14
|
+
|
15
|
+
token.should_not be_blank
|
16
|
+
token.length.should eql( 64 )
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should generate token before being persisted' do
|
20
|
+
post.token.should_not be_blank
|
21
|
+
post.should_not be_persisted
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should generate new tokens each time' do
|
25
|
+
persisted_post = create :post
|
26
|
+
persisted_post.token.should_not eql( post.token )
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should have a create_sha2_token private method' do
|
30
|
+
post.private_methods.should be_include( :create_sha2_token )
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should have a create_secure_random_token private method' do
|
34
|
+
post.private_methods.should be_include( :create_secure_random_token )
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should have a token_lookup private method' do
|
38
|
+
post.private_methods.should be_include( :token_lookup )
|
39
|
+
end
|
40
|
+
|
41
|
+
context :create_secure_random_token do
|
42
|
+
it 'should generate a 64 chars string' do
|
43
|
+
secure_random = post.send( :create_secure_random_token )
|
44
|
+
secure_random.should_not be_blank
|
45
|
+
secure_random.length.should eql( 64 )
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context :create_sha2_token do
|
50
|
+
it 'should generate a 64 chars string' do
|
51
|
+
sha2 = post.send( :create_sha2_token )
|
52
|
+
sha2.should_not be_blank
|
53
|
+
sha2.length.should eql( 64 )
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context :token_lookup do
|
58
|
+
it 'works by receiving a block of code to generate a string and returning such string if unique' do
|
59
|
+
lookup = post.send( :token_lookup ) do
|
60
|
+
'foo'
|
61
|
+
end
|
62
|
+
|
63
|
+
lookup.should eql( 'foo' )
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'Custom settings' do
|
69
|
+
let( :user ) { build :user }
|
70
|
+
|
71
|
+
it 'should have a getter named secret_hash' do
|
72
|
+
user.should respond_to( :secret_hash )
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should have a 64 chars secret_hash' do
|
76
|
+
user.secret_hash.length.should eql( 64 )
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should generate a new token in secret_hash even if not persisted' do
|
80
|
+
user.secret_hash.should be_present
|
81
|
+
user.should_not be_persisted
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should generate a unique token each time' do
|
85
|
+
persisted_user = create :user
|
86
|
+
persisted_user.secret_hash.should_not eql( user.secret_hash )
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rspec'
|
3
|
+
require 'factory_girl'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_support'
|
6
|
+
require 'simple_token'
|
7
|
+
require 'sqlite3'
|
8
|
+
require 'factories'
|
9
|
+
|
10
|
+
SPEC_PATH = File.expand_path File.join( File.dirname __FILE__ )
|
11
|
+
config = YAML::load( IO.read( File.join SPEC_PATH, 'config', 'database.yml' ) )['test']['sqlite']
|
12
|
+
|
13
|
+
# Connect to sqlite3 and load models
|
14
|
+
ActiveRecord::Base.establish_connection config
|
15
|
+
ActiveSupport::Dependencies.autoload_paths.unshift File.join( SPEC_PATH, 'app', 'models' )
|
16
|
+
|
17
|
+
# Load schema.rb into sqlite3
|
18
|
+
ActiveRecord::Base.silence do
|
19
|
+
ActiveRecord::Migration.verbose = false
|
20
|
+
load File.join( SPEC_PATH, 'db', 'schema.rb' )
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.order = 'random'
|
25
|
+
config.include FactoryGirl::Syntax::Methods
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Enrique Vidal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Easily create unique tokens in active record models
|
47
|
+
email:
|
48
|
+
- enrique@cloverinteractive.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- .travis.yml
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/simple_token.rb
|
61
|
+
- lib/simple_token/rails.rb
|
62
|
+
- lib/simple_token/version.rb
|
63
|
+
- simple_token.gemspec
|
64
|
+
- spec/app/models/post.rb
|
65
|
+
- spec/app/models/user.rb
|
66
|
+
- spec/config/database.yml
|
67
|
+
- spec/db/schema.rb
|
68
|
+
- spec/factories.rb
|
69
|
+
- spec/simple_token_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: http://cloverinteractive.github.com/simple_token/
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.23
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Simple token creation gem
|
95
|
+
test_files:
|
96
|
+
- spec/app/models/post.rb
|
97
|
+
- spec/app/models/user.rb
|
98
|
+
- spec/config/database.yml
|
99
|
+
- spec/db/schema.rb
|
100
|
+
- spec/factories.rb
|
101
|
+
- spec/simple_token_spec.rb
|
102
|
+
- spec/spec_helper.rb
|