sigmund 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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +45 -0
- data/Rakefile +12 -0
- data/lib/sigmund/base.rb +75 -0
- data/lib/sigmund/version.rb +3 -0
- data/lib/sigmund.rb +24 -0
- data/sigmund.gemspec +23 -0
- data/test/lib/sigmund/base_test.rb +100 -0
- data/test/lib/sigmund/version_test.rb +10 -0
- data/test/lib/sigmund_test.rb +57 -0
- data/test/test_helper.rb +4 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04869d6df5edfc8be15b695c2efa7c48e1ae7b5e
|
4
|
+
data.tar.gz: 0dadab9b98741b7e755efbfb843672f3dee47a25
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7fcf52f1a82477889fae5e4f0f4bf7873e94c8035ea08e802edf69d48f416d87fbb6bdaf0d6ef57fe7bbccdae7b2140873e85dff4811991d5ac616cb797dac7
|
7
|
+
data.tar.gz: 23b0e3ecf935a1f3cca0603b973fe889e19da34dd4de1519e2043da5a598556c3919ba4a108c08b87a566c14c91b5175e29199aa8fcb5e4f42db8a7ebddd0fce
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Leonard van Driel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Sigmund
|
2
|
+
|
3
|
+
*Sign urls and verify signatures*
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sigmund'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sigmund
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To sign a url, use:
|
22
|
+
|
23
|
+
Sigmund.sign("http://www.test.com/path", 'secret_key')
|
24
|
+
|
25
|
+
This will append a `sig` parameter to the url containing a HMAC SHA1 signature.
|
26
|
+
|
27
|
+
Now to verify a signature, use:
|
28
|
+
|
29
|
+
Sigmund.verify("http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s", 'secret_key')
|
30
|
+
|
31
|
+
This will return true only if a valid signature is present.
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
40
|
+
|
41
|
+
## License
|
42
|
+
Sigmund is licensed under the terms of the MIT License, see the included LICENSE file.
|
43
|
+
|
44
|
+
## Authors
|
45
|
+
- [Leonard van Driel](http://www.leonardvandriel.nl/)
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << 'lib/sigmund'
|
8
|
+
t.test_files = FileList['test/lib/*_test.rb', 'test/lib/sigmund/*_test.rb']
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :test
|
data/lib/sigmund/base.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'digest/hmac'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Sigmund
|
5
|
+
|
6
|
+
class Base
|
7
|
+
|
8
|
+
def self.hmac_procedure(key, digester)
|
9
|
+
Proc.new do |data|
|
10
|
+
hmac = Digest::HMAC.new(key, digester)
|
11
|
+
hmac.update(data)
|
12
|
+
hmac.digest
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.sign_uri(uri, proc, exclude, param)
|
17
|
+
normalized = normalize_uri(uri, exclude, param)
|
18
|
+
sig = compute_signature(normalized, proc)
|
19
|
+
append_signature(uri, sig, param)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.verify_uri(uri, proc, exclude, param)
|
23
|
+
normalized = normalize_uri(uri, exclude, param)
|
24
|
+
sig = compute_signature(normalized, proc)
|
25
|
+
(extract_signature(uri, param) == sig)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.normalize_uri(uri, exclude, param)
|
29
|
+
u = uri.dup
|
30
|
+
if u.query
|
31
|
+
components = u.query.split('&')
|
32
|
+
components.reject! { |c| c.start_with?(param + '=') }
|
33
|
+
u.query = components.join('&')
|
34
|
+
end
|
35
|
+
u.scheme = nil if u.scheme == '' || exclude.include?(:scheme)
|
36
|
+
u.host = nil if u.host == '' || exclude.include?(:host)
|
37
|
+
u.path = '' if u.path == nil || exclude.include?(:path)
|
38
|
+
u.query = nil if u.query == '' || exclude.include?(:query)
|
39
|
+
u.fragment = nil if u.fragment == '' || exclude.include?(:fragment)
|
40
|
+
u.normalize!
|
41
|
+
u
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.compute_signature(uri, proc)
|
45
|
+
data = proc.call(uri.to_s)
|
46
|
+
encode_base64(data)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.append_signature(uri, signature, param)
|
50
|
+
u = uri.dup
|
51
|
+
u.query ||= ''
|
52
|
+
u.query += (u.query.size > 0 ? '&' : '')
|
53
|
+
u.query += param + '=' + signature
|
54
|
+
u
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.extract_signature(uri, param)
|
58
|
+
return unless uri.query
|
59
|
+
components = uri.query.split('&').select{ |c| c.start_with?(param + '=') }
|
60
|
+
return unless components.size == 1
|
61
|
+
components.first[4..-1]
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.encode_base64(data)
|
65
|
+
Base64.urlsafe_encode64(data).gsub('=','')
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.decode_base64(data)
|
69
|
+
padded = data + ('=' * (-data.size % 4))
|
70
|
+
Base64.urlsafe_decode64(padded)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/lib/sigmund.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "sigmund/base"
|
2
|
+
|
3
|
+
module Sigmund
|
4
|
+
|
5
|
+
class SignatureBrokenError < RuntimeError; end
|
6
|
+
|
7
|
+
@digester = Digest::SHA1
|
8
|
+
@exclude = [:scheme, :host]
|
9
|
+
@param = 'sig'
|
10
|
+
|
11
|
+
def self.sign(string, key, digester: @digester, exclude: @exclude, param: @param)
|
12
|
+
proc = Base.hmac_procedure(key, digester)
|
13
|
+
Base.sign_uri(URI(string), proc, exclude, param)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.verify(string, key, digester: @digester, exclude: @exclude, param: @param)
|
17
|
+
proc = Base.hmac_procedure(key, digester)
|
18
|
+
Base.verify_uri(URI(string), proc, exclude, param)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.verify!(string, key, options = {})
|
22
|
+
raise SignatureBrokenError.new unless verify(string, key, *options)
|
23
|
+
end
|
24
|
+
end
|
data/sigmund.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sigmund/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sigmund"
|
8
|
+
spec.version = Sigmund::VERSION
|
9
|
+
spec.authors = ["Leonard van Driel"]
|
10
|
+
spec.email = ["leonardvandriel@gmail.com"]
|
11
|
+
spec.description = %q{Sign urls and verify signatures}
|
12
|
+
spec.summary = %q{Sign urls and verify signatures, easy!}
|
13
|
+
spec.homepage = "https://github.com/leonardvandriel/sigmund"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
describe Sigmund::Base do
|
4
|
+
|
5
|
+
let(:uri) { URI('http://www.test.com/path?query#fragment') }
|
6
|
+
|
7
|
+
describe "hmac_procedure" do
|
8
|
+
it "returns sha1 procedure" do
|
9
|
+
digest = Sigmund::Base.hmac_procedure('test', Digest::SHA1).call('x')
|
10
|
+
Sigmund::Base.encode_base64(digest).must_equal("9nX6BQQtf-lOAkme0pXTWm7b2zo")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "sign_uri" do
|
15
|
+
it "signs a uri" do
|
16
|
+
u = Sigmund::Base.sign_uri(uri, Proc.new{ |d| d + 'x' }, [], 'sig')
|
17
|
+
u.to_s.must_equal('http://www.test.com/path?query&sig=aHR0cDovL3d3dy50ZXN0LmNvbS9wYXRoP3F1ZXJ5I2ZyYWdtZW50eA#fragment')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "normalize_uri" do
|
22
|
+
it "downcases scheme and host" do
|
23
|
+
u = Sigmund::Base.normalize_uri(URI('hTTp://www.TEST.com/'), [], 'sig')
|
24
|
+
u.to_s.must_equal('http://www.test.com/')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "appends /" do
|
28
|
+
u = Sigmund::Base.normalize_uri(URI('http://www.test.com'), [], 'sig')
|
29
|
+
u.to_s.must_equal('http://www.test.com/')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "removes empty query" do
|
33
|
+
u = Sigmund::Base.normalize_uri(URI('http://www.test.com/?'), [], 'sig')
|
34
|
+
u.to_s.must_equal('http://www.test.com/')
|
35
|
+
end
|
36
|
+
|
37
|
+
it "removes empty fragment" do
|
38
|
+
u = Sigmund::Base.normalize_uri(URI('http://www.test.com/#'), [], 'sig')
|
39
|
+
u.to_s.must_equal('http://www.test.com/')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "removes existing signature" do
|
43
|
+
u = Sigmund::Base.normalize_uri(URI('http://www.test.com/?sig=test'), [], 'sig')
|
44
|
+
u.to_s.must_equal('http://www.test.com/')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "compute_signature" do
|
49
|
+
it "invokes proc" do
|
50
|
+
data = nil
|
51
|
+
Sigmund::Base.compute_signature(uri, Proc.new{ |d| data = d })
|
52
|
+
data.must_equal(uri.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns proc result" do
|
56
|
+
sig = Sigmund::Base.compute_signature(uri, Proc.new{ |d| 'test' })
|
57
|
+
sig.must_equal('dGVzdA')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "append_signature" do
|
62
|
+
it "appends test signature" do
|
63
|
+
u = Sigmund::Base.append_signature(uri, 'test', 'sig')
|
64
|
+
u.to_s.must_equal('http://www.test.com/path?query&sig=test#fragment')
|
65
|
+
end
|
66
|
+
|
67
|
+
it "handles empty signature" do
|
68
|
+
u = Sigmund::Base.append_signature(uri, '', 'sig')
|
69
|
+
u.to_s.must_equal('http://www.test.com/path?query&sig=#fragment')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "extract_signature" do
|
74
|
+
it "extracts test signature" do
|
75
|
+
sig = Sigmund::Base.extract_signature(URI("http://www.test.com/path?query&sig=test#fragment"), 'sig')
|
76
|
+
sig.must_equal('test')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "encode_base64" do
|
81
|
+
it "encodes test string" do
|
82
|
+
Sigmund::Base.encode_base64('test').must_equal('dGVzdA')
|
83
|
+
end
|
84
|
+
|
85
|
+
it "handles empty string" do
|
86
|
+
Sigmund::Base.encode_base64('').must_equal('')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "encode_base64" do
|
91
|
+
it "encodes test string" do
|
92
|
+
Sigmund::Base.decode_base64('dGVzdA').must_equal('test')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "handles empty string" do
|
96
|
+
Sigmund::Base.decode_base64('').must_equal('')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
describe Sigmund do
|
4
|
+
|
5
|
+
let(:uri) { URI('http://www.test.com/path?query#fragment') }
|
6
|
+
|
7
|
+
describe "sign" do
|
8
|
+
it "signs with sha1" do
|
9
|
+
u = Sigmund.sign("http://www.test.com/path", 'test')
|
10
|
+
u.to_s.must_equal('http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "signs with other key" do
|
14
|
+
u = Sigmund.sign("http://www.test.com/path", 'test' + 'Z')
|
15
|
+
u.to_s.must_equal('http://www.test.com/path?sig=TVQPBRB9DUagk9N7mf6z3YIIHaE')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "signs with other url" do
|
19
|
+
u = Sigmund.sign("http://www.test.com/path?query#fragment", 'test')
|
20
|
+
u.to_s.must_equal('http://www.test.com/path?query&sig=fEpucXVmFSzkC6V4MMcXDmYEhk4#fragment')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "verify" do
|
25
|
+
it "verifies with sha1" do
|
26
|
+
v = Sigmund.verify("http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s", 'test')
|
27
|
+
v.must_equal(true)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "fails if sig incorrect" do
|
31
|
+
v = Sigmund.verify("http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s" + 'Z', 'test')
|
32
|
+
v.must_equal(false)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "fails if key incorrect" do
|
36
|
+
v = Sigmund.verify("http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s", 'test' + 'Z')
|
37
|
+
v.must_equal(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "fails if query incorrect" do
|
41
|
+
v = Sigmund.verify("http://www.test.com/path?sigg=ZCqYKJiLS7WMZX7l5wEU016Mv1s", 'test')
|
42
|
+
v.must_equal(false)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "fails if sig missing" do
|
46
|
+
v = Sigmund.verify("http://www.test.com/path", 'test')
|
47
|
+
v.must_equal(false)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "verify" do
|
52
|
+
it "raises if sig incorrect" do
|
53
|
+
proc { Sigmund.verify!("http://www.test.com/path?sig=ZCqYKJiLS7WMZX7l5wEU016Mv1s" + 'Z', 'test') }.must_raise Sigmund::SignatureBrokenError
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sigmund
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leonard van Driel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Sign urls and verify signatures
|
42
|
+
email:
|
43
|
+
- leonardvandriel@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/sigmund.rb
|
54
|
+
- lib/sigmund/base.rb
|
55
|
+
- lib/sigmund/version.rb
|
56
|
+
- sigmund.gemspec
|
57
|
+
- test/lib/sigmund/base_test.rb
|
58
|
+
- test/lib/sigmund/version_test.rb
|
59
|
+
- test/lib/sigmund_test.rb
|
60
|
+
- test/test_helper.rb
|
61
|
+
homepage: https://github.com/leonardvandriel/sigmund
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.1.10
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Sign urls and verify signatures, easy!
|
85
|
+
test_files:
|
86
|
+
- test/lib/sigmund/base_test.rb
|
87
|
+
- test/lib/sigmund/version_test.rb
|
88
|
+
- test/lib/sigmund_test.rb
|
89
|
+
- test/test_helper.rb
|
90
|
+
has_rdoc:
|