dbalatero-signed_request 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Evri, Inc
2
+ Authored by David Balatero
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ = signed_request
2
+
3
+ A simple 25-line gem for signing/verifying an HTTP request that has gone over the wire.
4
+
5
+ The sender and receiver must share the same secret key between them.
6
+
7
+ This gem should be used in conjunction with SSL certificates to defend against
8
+ replay attacks.
9
+
10
+ Any key can be used to sign the request -- choosing a secure key is the choice of the
11
+ user of this library.
12
+
13
+ == Signing a request
14
+
15
+ require 'signed_request'
16
+
17
+ def send_over_the_wire
18
+ key = 'mykey' # insecure, use something else :)
19
+ params = { 'param1' => 'foo',
20
+ 'param2' => 'bar' }
21
+ signature = SignedRequest.sign(params, key)
22
+
23
+ params['signature'] = key
24
+
25
+ # post it to the receiver.
26
+ req = Net::HTTP::Get.new('/secret/url')
27
+ req.form_data = params
28
+ http = Net::HTTP.new('mydomain.com', 443)
29
+ http.use_ssl = true
30
+ response = http.start do |session|
31
+ session.request(req)
32
+ end
33
+
34
+ # do something w/ response
35
+ end
36
+
37
+ == Verifying a signed request
38
+
39
+ require 'signed_request'
40
+
41
+ class SecretController < ApplicationController
42
+ def verify_request
43
+ key = 'mykey'
44
+
45
+ if SignedRequest.verified(params, key)
46
+ # we are good
47
+ else
48
+ abort "get the hell out of here!"
49
+ end
50
+ end
51
+ end
52
+
53
+ == Copyright
54
+
55
+ Copyright (c) 2009 Evri, Inc
56
+ Authored by David Balatero <dbalatero AT no-spam evri DOT com>. See LICENSE for details.
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "signed_request"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "dbalatero@evri.com"
10
+ gem.homepage = "http://github.com/dbalatero/signed_request"
11
+ gem.authors = ["David Balatero"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "signed_request #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,32 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'openssl/digest'
4
+
5
+ module SignedRequest
6
+ STRIP_PARAMS = ['action', 'controller']
7
+
8
+ # Sign a request on the sending end.
9
+ def self.sign(params, secret_key)
10
+ query = params.sort_by { |k,v| k.to_s.downcase }
11
+ digest = OpenSSL::Digest::Digest.new('sha1')
12
+ hmac = OpenSSL::HMAC.digest(digest, secret_key, query.to_s)
13
+ encoded = Base64.encode64(hmac).chomp
14
+ end
15
+
16
+ # Validate an incoming request on the receiving end.
17
+ def self.validate(params, secret_key)
18
+ signature = params.delete('signature')
19
+ return false if !signature
20
+
21
+ strip_keys_from!(params, *STRIP_PARAMS)
22
+ actual_signature = sign(params, secret_key)
23
+ actual_signature == signature
24
+ end
25
+
26
+ private
27
+ def self.strip_keys_from!(hash, *keys)
28
+ keys.each do |key|
29
+ hash.delete(key)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SignedRequest do
4
+ before(:each) do
5
+ @test_key = 'mykey'
6
+ end
7
+
8
+ describe "sign" do
9
+ it "should sign the request and return the correct signed key as base64" do
10
+ params = {
11
+ "tokenID" => "N1CHGCG13NNB4JMVJN1Q1JXIKBQDO4DQ595NRSCTILAU47P7GA7JVQMMJNXRUJFM",
12
+ "callerReference" => "44441234567fdsa44",
13
+ "expiry" => "10/2014",
14
+ "status" => "SC"
15
+ }
16
+
17
+ result = SignedRequest.sign(params, @test_key)
18
+ result.should == "uoOmSftU4gnUMK6Q1ylyGnr5hEw="
19
+ end
20
+ end
21
+
22
+
23
+ describe "validate" do
24
+ it "should return true given a correct request" do
25
+ good_params = {
26
+ "tokenID" => "N1CHGCG13NNB4JMVJN1Q1JXIKBQDO4DQ595NRSCTILAU47P7GA7JVQMMJNXRUJFM",
27
+ "callerReference" => "44441234567fdsa44",
28
+ "action" => "amazon_return",
29
+ "signature" => "uoOmSftU4gnUMK6Q1ylyGnr5hEw=",
30
+ "controller" => "checkout",
31
+ "expiry" => "10/2014",
32
+ "status" => "SC"
33
+ }
34
+
35
+ SignedRequest.validate(good_params, @test_key).should be_true
36
+ end
37
+
38
+ it "should return false if there is no signature given" do
39
+ good_params_without_signature = {
40
+ "tokenID" => "N1CHGCG13NNB4JMVJN1Q1JXIKBQDO4DQ595NRSCTILAU47P7GA7JVQMMJNXRUJFM",
41
+ "callerReference" => "44441234567fdsa44",
42
+ "action" => "amazon_return",
43
+ "controller" => "checkout",
44
+ "expiry" => "10/2014",
45
+ "status" => "SC"
46
+ }
47
+
48
+ SignedRequest.validate(good_params_without_signature, @test_key).should be_false
49
+ end
50
+
51
+ it "should return false if there is a bad signature given" do
52
+ result = SignedRequest.validate({'signature' => 'bad', 'param1' => 'ok'}, @test_key)
53
+ result.should be_false
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'signed_request'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbalatero-signed_request
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Balatero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dbalatero@evri.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/signed_request.rb
33
+ - spec/signed_request_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/dbalatero/signed_request
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: TODO
61
+ test_files:
62
+ - spec/signed_request_spec.rb
63
+ - spec/spec_helper.rb