hirameki 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/LICENSE.md +22 -0
- data/README.md +52 -0
- data/Rakefile +2 -0
- data/hirameki.gemspec +20 -0
- data/lib/hirameki.rb +40 -0
- data/lib/hirameki/config.rb +51 -0
- data/lib/hirameki/version.rb +3 -0
- data/spec/hirameki_spec.rb +81 -0
- data/spec/spec_helper.rb +12 -0
- metadata +81 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Yuki Nishijima
|
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,52 @@
|
|
1
|
+
# Hirameki 
|
2
|
+
|
3
|
+
Hirameki is a one-time token generator backed by Redis.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hirameki'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hirameki
|
18
|
+
|
19
|
+
If you use Hirameki with Rails, put the following in `config/initializers/`.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Hirameki.configure do |config|
|
23
|
+
config.redis = Redis.new(host: "localhost", port: 6379)
|
24
|
+
config.pepper = "put_a_long_pepper_in_here"
|
25
|
+
config.expiration_time = 10.minutes
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
it's super easy to use Hirameki. Give it a try on irb/pry!
|
32
|
+
|
33
|
+
```
|
34
|
+
> token = Hirameki.generate!(10)
|
35
|
+
=> "4dc6d4ca42e5204ab2d9cb5a0e0a8ec715c7d152"
|
36
|
+
> Hirameki.get(token)
|
37
|
+
=> 10
|
38
|
+
```
|
39
|
+
|
40
|
+
Thas't it!
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
49
|
+
|
50
|
+
## Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2011 Yuki Nishijima. See LICENSE.md for further details.
|
data/Rakefile
ADDED
data/hirameki.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hirameki/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Yuki Nishijima"]
|
6
|
+
gem.email = ["mail@yukinishijima.net"]
|
7
|
+
gem.description = %q{redis based one time token generator.}
|
8
|
+
gem.summary = %q{redis based one time token generator.}
|
9
|
+
gem.homepage = "https://github.com/yuki24/hirameki"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "hirameki"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Hirameki::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'redis', '~> 2.2.2'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
end
|
data/lib/hirameki.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "hirameki/config"
|
2
|
+
require "hirameki/version"
|
3
|
+
|
4
|
+
module Hirameki
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def configure
|
8
|
+
@config = Hirameki::Config.new
|
9
|
+
yield(@config)
|
10
|
+
end
|
11
|
+
|
12
|
+
def config
|
13
|
+
@config
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(key)
|
17
|
+
val = @config.redis.get(key)
|
18
|
+
val.nil? ? val : val.to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
def del(key)
|
22
|
+
@config.redis.del(key).to_i
|
23
|
+
end
|
24
|
+
|
25
|
+
def generate!(value)
|
26
|
+
gen_key(value).tap do |key|
|
27
|
+
if @config.expiration_time.nil?
|
28
|
+
@config.redis.set(key, value)
|
29
|
+
else
|
30
|
+
@config.redis.setex(key, @config.expiration_time, value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def gen_key(value)
|
38
|
+
Digest::SHA1.hexdigest("#{@config.pepper}#{Time.now.to_i}#{value}")
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
module Hirameki
|
4
|
+
class Config
|
5
|
+
attr_accessor :pepper, :expiration_time
|
6
|
+
|
7
|
+
# Accepts:
|
8
|
+
# 1. A 'hostname:port' String
|
9
|
+
# 2. A 'hostname:port:db' String (to select the Redis db)
|
10
|
+
# 3. A 'hostname:port/namespace' String (to set the Redis namespace)
|
11
|
+
# 4. A Redis URL String 'redis://host:port'
|
12
|
+
# 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
|
13
|
+
# or `Redis::Namespace`.
|
14
|
+
def redis=(server)
|
15
|
+
case server
|
16
|
+
when String
|
17
|
+
if server =~ /redis\:\/\//
|
18
|
+
redis = Redis.connect(url: server, thread_safe: true)
|
19
|
+
else
|
20
|
+
server, namespace = server.split('/', 2)
|
21
|
+
host, port, db = server.split(':')
|
22
|
+
redis = Redis.new(host: host, port: port, thread_safe: true, db: db)
|
23
|
+
end
|
24
|
+
@redis = redis
|
25
|
+
when Redis, Redis::Namespace
|
26
|
+
@redis = server
|
27
|
+
else
|
28
|
+
@redis = Redis.new(host: "localhost", port: 6379)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the current Redis connection. If none has been created, will
|
33
|
+
# create a new one.
|
34
|
+
def redis
|
35
|
+
return @redis if @redis
|
36
|
+
self.redis = Redis.respond_to?(:connect) ? Redis.connect : "localhost:6379"
|
37
|
+
end
|
38
|
+
|
39
|
+
def redis_id
|
40
|
+
# support 1.x versions of redis-rb
|
41
|
+
if redis.respond_to?(:server)
|
42
|
+
redis.server
|
43
|
+
elsif redis.respond_to?(:nodes) # distributed
|
44
|
+
redis.nodes.map { |n| n.id }.join(', ')
|
45
|
+
else
|
46
|
+
redis.client.id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hirameki do
|
4
|
+
describe "confgiruration setup" do
|
5
|
+
context "full configuration" do
|
6
|
+
before(:all) do
|
7
|
+
Hirameki.configure do |config|
|
8
|
+
config.redis = Redis.new(host: "localhost", port: 6379)
|
9
|
+
config.pepper = "tapyokGajivijabroirnajvilphOcyij"
|
10
|
+
config.expiration_time = 60 * 10
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { Hirameki.config }
|
15
|
+
its(:redis) { should be_a(Redis) }
|
16
|
+
its(:pepper) { should == "tapyokGajivijabroirnajvilphOcyij" }
|
17
|
+
its(:expiration_time) { should == 600 }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "generation of token" do
|
22
|
+
module Hirameki
|
23
|
+
extend self
|
24
|
+
|
25
|
+
def stub_setex!
|
26
|
+
@config.redis.should_not_receive(:setex)
|
27
|
+
end
|
28
|
+
|
29
|
+
def stub_set!
|
30
|
+
@config.redis.should_not_receive(:set)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "token generation with expiration time" do
|
35
|
+
before(:all) do
|
36
|
+
Hirameki.configure do |config|
|
37
|
+
config.redis = Redis.new(host: "localhost", port: 6379)
|
38
|
+
config.pepper = "tapyokGajivijabroirnajvilphOcyij"
|
39
|
+
end
|
40
|
+
Hirameki.config.redis.flushdb
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not expire any keys" do
|
44
|
+
Hirameki.stub_setex!
|
45
|
+
key = Hirameki.generate!(1)
|
46
|
+
Hirameki.get(key).should == 1
|
47
|
+
Hirameki.del(key).should == 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "token generation without expiration time" do
|
52
|
+
before(:all) do
|
53
|
+
Hirameki.configure do |config|
|
54
|
+
config.redis = Redis.new(host: "localhost", port: 6379)
|
55
|
+
config.pepper = "tapyokGajivijabroirnajvilphOcyij"
|
56
|
+
config.expiration_time = 1
|
57
|
+
end
|
58
|
+
Hirameki.config.redis.flushdb
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should expire any keys in 1 second" do
|
62
|
+
Hirameki.stub_set!
|
63
|
+
key = Hirameki.generate!(1)
|
64
|
+
sleep(2)
|
65
|
+
Hirameki.get(key).should be_nil
|
66
|
+
Hirameki.del(key).should == 0
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "invalid key" do
|
71
|
+
it "should add the given value to redis and return its key" do
|
72
|
+
key = Hirameki.generate!(1)
|
73
|
+
Hirameki.get(key).should == 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return 0 when there is no key in redis" do
|
77
|
+
Hirameki.get(10).should be_nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'hirameki'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
# Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hirameki
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yuki Nishijima
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis
|
16
|
+
requirement: &14647800 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.2.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *14647800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &14609140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *14609140
|
36
|
+
description: redis based one time token generator.
|
37
|
+
email:
|
38
|
+
- mail@yukinishijima.net
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- .travis.yml
|
46
|
+
- CHANGELOG.md
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE.md
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- hirameki.gemspec
|
52
|
+
- lib/hirameki.rb
|
53
|
+
- lib/hirameki/config.rb
|
54
|
+
- lib/hirameki/version.rb
|
55
|
+
- spec/hirameki_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: https://github.com/yuki24/hirameki
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.10
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: redis based one time token generator.
|
81
|
+
test_files: []
|