has_secure_token 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +10 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +27 -0
- data/has_secure_token.gemspec +26 -0
- data/lib/has_secure_token.rb +29 -0
- data/lib/has_secure_token/version.rb +3 -0
- data/test/has_secure_password_test.rb +13 -0
- data/test/models/user.rb +14 -0
- data/test/test_helper.rb +17 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 779e9aa5c015cf598382638c8bf438f5765a4063
|
4
|
+
data.tar.gz: 708da57acbbbe47d93240d2edb3ca153f4f8c558
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38f2c9928827176cbc83d04620994a86de70ffcd894f2c3c53b43f8b35f5cb7f9cf8cdc87208bcb237f4774e08a1a03572a46bb4b2b867deb546188fd87796c6
|
7
|
+
data.tar.gz: 261107656b335bca54aa0089f0c1bec76e632c38d5323219f9d4f0396939774625aca41b7937947235dd478e57b5a2be13f1914c44455c31ed36b76aa759ad4c
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 robertomiranda
|
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,40 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/robertomiranda/has_secure_token.png)](https://travis-ci.org/robertomiranda/has_secure_token)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/has_secure_token.svg)](http://badge.fury.io/rb/has_secure_token)
|
3
|
+
[![Dependency Status](https://gemnasium.com/robertomiranda/has_secure_token.svg)](https://gemnasium.com/robertomiranda/has_secure_token)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/robertomiranda/has_secure_token/badges/gpa.svg)](https://codeclimate.com/github/robertomiranda/has_secure_token)
|
5
|
+
|
6
|
+
# HasSecureToken
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'has_secure_password'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install has_secure_password
|
21
|
+
|
22
|
+
##Setting your Model
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class User
|
26
|
+
has_secure_token :token1, :token2
|
27
|
+
end
|
28
|
+
|
29
|
+
user = User.create
|
30
|
+
user.token1 => "44539a6a59835a4ee9d7b112b48cd76e"
|
31
|
+
user.token2 => "226dd46af6be78953bde1641622497a8"
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( https://github.com/[my-github-username]/has_secure_password/fork )
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
task :console do
|
6
|
+
puts "Loading development console..."
|
7
|
+
system("irb -r active_model_otp")
|
8
|
+
end
|
9
|
+
|
10
|
+
task :help do
|
11
|
+
puts "Available rake tasks: "
|
12
|
+
puts "rake console - Run a IRB console with all enviroment loaded"
|
13
|
+
puts "rake test - Run tests"
|
14
|
+
end
|
15
|
+
|
16
|
+
task :test do
|
17
|
+
Dir.chdir('test')
|
18
|
+
end
|
19
|
+
|
20
|
+
Rake::TestTask.new(:test) do |t|
|
21
|
+
t.libs << '../lib'
|
22
|
+
t.libs << '../test'
|
23
|
+
t.test_files = FileList['*_test.rb']
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task :default => :test
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_secure_token/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "has_secure_token"
|
8
|
+
spec.version = HasSecureToken::VERSION
|
9
|
+
spec.authors = ["Roberto Miranda Altamar"]
|
10
|
+
spec.email = ["rjmaltamar@gmail.com"]
|
11
|
+
spec.summary = %q{Create uniques random tokens for any model in ruby on rails.}
|
12
|
+
spec.description = %q{Add to your models an easily way to generate tokens}
|
13
|
+
spec.homepage = "https://github.com/robertomiranda/has_secure_token"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.4.2"
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "active_model"
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module HasSecureToken
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
module ClassMethods
|
7
|
+
def has_secure_token(*attrs)
|
8
|
+
include InstanceMethodsOnActivation
|
9
|
+
cattr_accessor :token_columns
|
10
|
+
self.token_columns = attrs
|
11
|
+
before_create :_generate_token
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethodsOnActivation
|
16
|
+
def _generate_token
|
17
|
+
self.class.token_columns.each do |attribute|
|
18
|
+
self.send("#{attribute}=", loop do
|
19
|
+
random_token = SecureRandom.hex
|
20
|
+
break random_token unless self.class.exists?(attribute => random_token)
|
21
|
+
end)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveSupport.on_load(:active_record) do
|
28
|
+
include HasSecureToken
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class HasSecureTokenTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@user = User.new
|
6
|
+
@user.run_callbacks :create
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_assing_token_values
|
10
|
+
assert_not_nil @user.auth_token
|
11
|
+
assert_not_nil @user.invitation_token
|
12
|
+
end
|
13
|
+
end
|
data/test/models/user.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class User
|
2
|
+
extend ActiveModel::Callbacks
|
3
|
+
include ActiveModel::Validations
|
4
|
+
include HasSecureToken
|
5
|
+
|
6
|
+
define_model_callbacks :create
|
7
|
+
attr_accessor :auth_token, :invitation_token
|
8
|
+
|
9
|
+
has_secure_token :auth_token, :invitation_token
|
10
|
+
|
11
|
+
def self.exists?(attrs)
|
12
|
+
false
|
13
|
+
end
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
testdir = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
|
3
|
+
|
4
|
+
libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
|
5
|
+
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "has_secure_token"
|
9
|
+
require "minitest/autorun"
|
10
|
+
require "minitest/unit"
|
11
|
+
|
12
|
+
Dir["models/*.rb"].each {|file| require file }
|
13
|
+
|
14
|
+
def assert_not_nil(exp, msg=nil)
|
15
|
+
msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
|
16
|
+
assert(!exp.nil?, msg)
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_secure_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberto Miranda Altamar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.4.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.4.2
|
69
|
+
description: Add to your models an easily way to generate tokens
|
70
|
+
email:
|
71
|
+
- rjmaltamar@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- has_secure_token.gemspec
|
83
|
+
- lib/has_secure_token.rb
|
84
|
+
- lib/has_secure_token/version.rb
|
85
|
+
- test/has_secure_password_test.rb
|
86
|
+
- test/models/user.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
homepage: https://github.com/robertomiranda/has_secure_token
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Create uniques random tokens for any model in ruby on rails.
|
112
|
+
test_files:
|
113
|
+
- test/has_secure_password_test.rb
|
114
|
+
- test/models/user.rb
|
115
|
+
- test/test_helper.rb
|