signauth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/lib/signauth/credentials.rb +28 -0
- data/lib/signauth/errors.rb +7 -0
- data/lib/signauth/request.rb +18 -0
- data/lib/signauth/signature/version_1.rb +48 -0
- data/lib/signauth/signature/version_2.rb +40 -0
- data/lib/signauth/signature.rb +6 -0
- data/lib/signauth/signer.rb +17 -0
- data/lib/signauth/version.rb +3 -0
- data/lib/signauth.rb +9 -0
- data/signauth.gemspec +20 -0
- data/spec/signauth_spec.rb +5 -0
- data/spec/spec_helper.rb +15 -0
- metadata +103 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 arukoh
|
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,29 @@
|
|
1
|
+
# Signauth
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'signauth'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install signauth
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Signauth
|
4
|
+
class Credentials
|
5
|
+
attr_reader :access_key_id
|
6
|
+
attr_reader :secret_access_key
|
7
|
+
|
8
|
+
def initialize(key = random(20), secret = random(40))
|
9
|
+
raise ArgumentError, "invalid key" if key.nil? || key.empty?
|
10
|
+
raise ArgumentError, "invalid secret" if secret.nil? || secret.empty?
|
11
|
+
@access_key_id = key
|
12
|
+
@secret_access_key = secret
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_h
|
16
|
+
{
|
17
|
+
"access_key_id" => access_key_id,
|
18
|
+
"secret_access_key" => secret_access_key
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def random(size)
|
24
|
+
SecureRandom.base64(size)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Signauth
|
2
|
+
class Request
|
3
|
+
|
4
|
+
attr_accessor :method
|
5
|
+
attr_accessor :host
|
6
|
+
attr_accessor :path
|
7
|
+
attr_accessor :params
|
8
|
+
|
9
|
+
def initialize(signature_version = Signauth::Signature::Version1)
|
10
|
+
extend(signature_version)
|
11
|
+
@method = "GET"
|
12
|
+
@host = ""
|
13
|
+
@path = "/"
|
14
|
+
@params = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Signauth
|
2
|
+
module Signature
|
3
|
+
module Version1
|
4
|
+
|
5
|
+
def add_authorization!(credentials)
|
6
|
+
params['access_key_id'] = credentials.access_key_id
|
7
|
+
params['signature_version'] = '1'
|
8
|
+
params['signature_method'] = 'HmacSHA256'
|
9
|
+
|
10
|
+
params.delete('signature')
|
11
|
+
params['signature'] = signature(credentials)
|
12
|
+
end
|
13
|
+
|
14
|
+
def authenticate(credentials)
|
15
|
+
given = params.delete('signature')
|
16
|
+
computed = signature(credentials)
|
17
|
+
unless given == computed
|
18
|
+
raise Errors::SignatureDoesNotMatch,
|
19
|
+
"Invalid signature: should have sent Base64(HmacSHA256(secret, #{string_to_sign.inspect}))"\
|
20
|
+
", but given #{given}"
|
21
|
+
end
|
22
|
+
true
|
23
|
+
ensure
|
24
|
+
params['signature'] = given
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def signature(credentials)
|
30
|
+
Signer.sign(credentials.secret_access_key, string_to_sign)
|
31
|
+
end
|
32
|
+
|
33
|
+
def string_to_sign
|
34
|
+
[
|
35
|
+
method.to_s.upcase,
|
36
|
+
host.to_s.downcase,
|
37
|
+
path.to_s,
|
38
|
+
params.sort.collect { |n, v| encoded(n, v) }.join('&'),
|
39
|
+
].join("\n")
|
40
|
+
end
|
41
|
+
|
42
|
+
def encoded(name, value)
|
43
|
+
"#{URI.escape(name)}=#{URI.escape(value)}"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module Signauth
|
4
|
+
module Signature
|
5
|
+
module Version2
|
6
|
+
include Version1
|
7
|
+
|
8
|
+
#http://www.w3.org/TR/NOTE-datetime
|
9
|
+
ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
|
10
|
+
|
11
|
+
def add_authorization!(credentials)
|
12
|
+
params['timestamp'] = Time.now.utc.strftime(ISO8601)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def authenticate(credentials, skew = 15*60)
|
17
|
+
validate_timestamp(skew)
|
18
|
+
super(credentials)
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def validate_timestamp(skew)
|
24
|
+
begin
|
25
|
+
timestamp = Time.iso8601(params['timestamp'])
|
26
|
+
rescue => e
|
27
|
+
raise Errors::InvalidTimestamp, "#{e.class}-#{e.message}"
|
28
|
+
end
|
29
|
+
|
30
|
+
if (timestamp.to_i - Time.now.to_i).abs >= skew
|
31
|
+
raise Errors::RequestTimeTooSkewed,
|
32
|
+
"Timestamp expired: Given timestamp (#{timestamp.utc.strftime(ISO8601)}) "\
|
33
|
+
"not within #{skew}s of server time (#{Time.now.utc.strftime(ISO8601)})"
|
34
|
+
end
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Signauth
|
5
|
+
module Signer
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def sign(secret, string_to_sign, digest_method = 'sha256')
|
9
|
+
Base64.encode64(hmac(secret, string_to_sign, digest_method)).strip
|
10
|
+
end
|
11
|
+
|
12
|
+
def hmac(key, value, digest = 'sha256')
|
13
|
+
OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(digest), key, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/lib/signauth.rb
ADDED
data/signauth.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/signauth/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["arukoh"]
|
6
|
+
gem.email = ["arukoh10@gmail.com"]
|
7
|
+
gem.description = %q{Signature authentication}
|
8
|
+
gem.summary = %q{Signature authentication}
|
9
|
+
gem.homepage = "https://github.com/arukoh/signauth"
|
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 = "signauth"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Signauth::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake", ">= 0.8.7"
|
19
|
+
gem.add_development_dependency "rspec", ">= 2.4.0"
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- arukoh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.7
|
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.8.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.4.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: 2.4.0
|
46
|
+
description: Signature authentication
|
47
|
+
email:
|
48
|
+
- arukoh10@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/signauth.rb
|
60
|
+
- lib/signauth/credentials.rb
|
61
|
+
- lib/signauth/errors.rb
|
62
|
+
- lib/signauth/request.rb
|
63
|
+
- lib/signauth/signature.rb
|
64
|
+
- lib/signauth/signature/version_1.rb
|
65
|
+
- lib/signauth/signature/version_2.rb
|
66
|
+
- lib/signauth/signer.rb
|
67
|
+
- lib/signauth/version.rb
|
68
|
+
- signauth.gemspec
|
69
|
+
- spec/signauth_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: https://github.com/arukoh/signauth
|
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
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 566629137
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
hash: 566629137
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.24
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Signature authentication
|
101
|
+
test_files:
|
102
|
+
- spec/signauth_spec.rb
|
103
|
+
- spec/spec_helper.rb
|