fernet 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 +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/fernet.gemspec +19 -0
- data/lib/fernet.rb +13 -0
- data/lib/fernet/generator.rb +24 -0
- data/lib/fernet/verifier.rb +41 -0
- data/lib/fernet/version.rb +3 -0
- data/spec/fernet_spec.rb +50 -0
- data/spec/spec_helper.rb +17 -0
- metadata +71 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Harold Giménez
|
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,34 @@
|
|
1
|
+
# Fernet
|
2
|
+
|
3
|
+
Fernet allows you to easily generate and verify HMAC based authentication tokens for issuing API requests between remote servers.
|
4
|
+
|
5
|
+
![Fernet](http://f.cl.ly/items/2d0P3d26271O3p2v253u/photo.JPG)
|
6
|
+
|
7
|
+
Fernet is usually served as a *digestif* after a meal but may also be served with coffee and espresso or mixed into coffee and espresso drinks.
|
8
|
+
|
9
|
+
Fernet about it!
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'fernet'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install fernet
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
For now, see [spec/fernet_spec.rb](https://github.com/hgimenez/fernet/blob/master/spec/fernet_spec.rb).
|
28
|
+
|
29
|
+
I'll flesh this out further soon. Sorry.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
Valcro is copyright (c) Harold Giménez and is released under the terms of the
|
34
|
+
MIT License found in the LICENSE file.
|
data/Rakefile
ADDED
data/fernet.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/fernet/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Harold Giménez"]
|
6
|
+
gem.email = ["harold.gimenez@gmail.com"]
|
7
|
+
gem.description = %q{Delicious HMAC Digest[if] authentication}
|
8
|
+
gem.summary = %q{Easily generate and verify HMAC based authentication tokens}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "fernet"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Fernet::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
data/lib/fernet.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'fernet/version'
|
2
|
+
require 'fernet/generator'
|
3
|
+
require 'fernet/verifier'
|
4
|
+
|
5
|
+
module Fernet
|
6
|
+
def self.generate(secret, &block)
|
7
|
+
Generator.new(secret).generate(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.verify(secret, token, &block)
|
11
|
+
Verifier.new(secret).verify_token(token, &block)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'json'
|
3
|
+
require 'openssl'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
module Fernet
|
7
|
+
class Generator
|
8
|
+
attr_reader :secret
|
9
|
+
attr_accessor :data
|
10
|
+
|
11
|
+
def initialize(secret)
|
12
|
+
@secret = secret
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate
|
16
|
+
yield self
|
17
|
+
data.merge!(issued_at: DateTime.now)
|
18
|
+
|
19
|
+
mac = OpenSSL::HMAC.hexdigest('sha256', JSON.dump(data), secret)
|
20
|
+
Base64.urlsafe_encode64(JSON.dump(data.merge(signature: mac)))
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'json'
|
3
|
+
require 'openssl'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
module Fernet
|
7
|
+
class Verifier
|
8
|
+
|
9
|
+
attr_reader :secret, :token, :data
|
10
|
+
attr_writer :seconds_valid
|
11
|
+
|
12
|
+
def initialize(secret)
|
13
|
+
@secret = secret
|
14
|
+
end
|
15
|
+
|
16
|
+
def verify_token(token)
|
17
|
+
@token = token
|
18
|
+
deconstruct
|
19
|
+
|
20
|
+
custom_verification = yield self
|
21
|
+
|
22
|
+
signatures_match? && token_recent_enough? && custom_verification
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def deconstruct
|
28
|
+
@data = JSON.parse(Base64.decode64(token))
|
29
|
+
@received_signature = @data.delete('signature')
|
30
|
+
@regenerated_mac = OpenSSL::HMAC.hexdigest('sha256', JSON.dump(@data), secret)
|
31
|
+
end
|
32
|
+
|
33
|
+
def token_recent_enough?
|
34
|
+
DateTime.parse(data['issued_at']) > (DateTime.now - 60)
|
35
|
+
end
|
36
|
+
|
37
|
+
def signatures_match?
|
38
|
+
@regenerated_mac == @received_signature
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/fernet_spec.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fernet'
|
3
|
+
|
4
|
+
describe Fernet do
|
5
|
+
let(:token_data) do
|
6
|
+
{ email: 'harold@heroku.com', id: '123', arbitrary: 'data' }
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:secret) { 'sekrit123' }
|
10
|
+
|
11
|
+
it 'can verify tokens it generates' do
|
12
|
+
token = Fernet.generate(secret) do |generator|
|
13
|
+
generator.data = token_data
|
14
|
+
end
|
15
|
+
|
16
|
+
Fernet.verify(secret, token) do |verifier|
|
17
|
+
verifier.data['email'] == 'harold@heroku.com'
|
18
|
+
end.should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'fails with a bad secret' do
|
22
|
+
token = Fernet.generate(secret) do |generator|
|
23
|
+
generator.data = token_data
|
24
|
+
end
|
25
|
+
|
26
|
+
Fernet.verify('bad', token) do |verifier|
|
27
|
+
verifier.data['email'] == 'harold@heroku.com'
|
28
|
+
end.should be_false
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'fails with a bad custom verification' do
|
32
|
+
token = Fernet.generate(secret) do |generator|
|
33
|
+
generator.data = token_data
|
34
|
+
end
|
35
|
+
|
36
|
+
Fernet.verify('bad', token) do |verifier|
|
37
|
+
verifier.data['email'] == 'harold@gmail.com'
|
38
|
+
end.should be_false
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'fails if the token is too old' do
|
42
|
+
token = Fernet.generate(secret) do |generator|
|
43
|
+
generator.data = token_data
|
44
|
+
end
|
45
|
+
|
46
|
+
Fernet.verify('bad', token) do |verifier|
|
47
|
+
verifier.seconds_valid = 0
|
48
|
+
end.should be_false
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fernet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Harold Giménez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70116561753260 !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: *70116561753260
|
25
|
+
description: Delicious HMAC Digest[if] authentication
|
26
|
+
email:
|
27
|
+
- harold.gimenez@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- fernet.gemspec
|
39
|
+
- lib/fernet.rb
|
40
|
+
- lib/fernet/generator.rb
|
41
|
+
- lib/fernet/verifier.rb
|
42
|
+
- lib/fernet/version.rb
|
43
|
+
- spec/fernet_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
homepage: ''
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.10
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Easily generate and verify HMAC based authentication tokens
|
69
|
+
test_files:
|
70
|
+
- spec/fernet_spec.rb
|
71
|
+
- spec/spec_helper.rb
|