From ae6715f9bc9ab3e5b627844b3fb9ba5fa5ddc1bc Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Sun, 29 Apr 2012 23:14:47 +0100 Subject: [PATCH 1/8] removing dependency on ruby-hmac library from gemspec, using latest rspec version --- Gemfile.lock | 25 ++++++++++--------------- signature.gemspec | 3 +-- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1d6c8dd..42ab508 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,28 +2,23 @@ PATH remote: . specs: signature (0.1.2) - ruby-hmac GEM remote: http://rubygems.org/ specs: - diff-lcs (1.1.2) - rspec (2.0.1) - rspec-core (~> 2.0.1) - rspec-expectations (~> 2.0.1) - rspec-mocks (~> 2.0.1) - rspec-core (2.0.1) - rspec-expectations (2.0.1) - diff-lcs (>= 1.1.2) - rspec-mocks (2.0.1) - rspec-core (~> 2.0.1) - rspec-expectations (~> 2.0.1) - ruby-hmac (0.4.0) + diff-lcs (1.1.3) + rspec (2.9.0) + rspec-core (~> 2.9.0) + rspec-expectations (~> 2.9.0) + rspec-mocks (~> 2.9.0) + rspec-core (2.9.0) + rspec-expectations (2.9.1) + diff-lcs (~> 1.1.3) + rspec-mocks (2.9.0) PLATFORMS ruby DEPENDENCIES - rspec (~> 2.0.0) - ruby-hmac + rspec (~> 2.9.0) signature! diff --git a/signature.gemspec b/signature.gemspec index accf999..48eb387 100644 --- a/signature.gemspec +++ b/signature.gemspec @@ -17,6 +17,5 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency "ruby-hmac" - s.add_development_dependency "rspec", "~> 2.0.0" + s.add_development_dependency "rspec", "~> 2.9.0" end From 7a0558b5c16198a113b3702e6978e5f9702d000e Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Sun, 29 Apr 2012 23:15:48 +0100 Subject: [PATCH 2/8] using OpenSSL instead of ruby-hmac gem --- lib/signature.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/signature.rb b/lib/signature.rb index ea62ba1..319aafc 100644 --- a/lib/signature.rb +++ b/lib/signature.rb @@ -1,4 +1,4 @@ -require 'hmac-sha2' +require 'openssl' module Signature class AuthenticationError < RuntimeError; end @@ -90,7 +90,11 @@ def auth_hash private def signature(token) - HMAC::SHA256.hexdigest(token.secret, string_to_sign) + OpenSSL::HMAC.hexdigest(digest, token.secret, string_to_sign) + end + + def digest + OpenSSL::Digest::Digest.new('sha256') end def string_to_sign From 2793ea950a362ba71440d9df7f96048f4ed75925 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Apr 2012 09:02:54 +0100 Subject: [PATCH 3/8] added travis.yml --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..44e453b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: ruby +rvm: + - 1.8.7 + - 1.9.2 + - 1.9.3 + - jruby-18mode # JRuby in 1.8 mode + - jruby-19mode # JRuby in 1.9 mode + - rbx-18mode + - rbx-19mode From 25b42b4d97c22f0c8f3cce7c9c0711bc82895104 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Apr 2012 09:08:18 +0100 Subject: [PATCH 4/8] added build status icon and command to travis.yml --- .travis.yml | 2 ++ README.md | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 44e453b..69b1fe0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,3 +7,5 @@ rvm: - jruby-19mode # JRuby in 1.9 mode - rbx-18mode - rbx-19mode + +script: bundle exec rspec spec diff --git a/README.md b/README.md index 50e285a..e442d61 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ signature ========= +[![Build Status](https://secure.travis-ci.org/markburns/signature.png?branch=master)](http://travis-ci.org/markburns/signature) + Examples -------- @@ -11,7 +13,7 @@ Client example request = Signature::Request.new('POST', '/api/thing', params) auth_hash = request.sign(token) query_params = params.merge(auth_hash) - + HTTParty.post('http://myservice/api/thing', { :query => query_params }) From 5cafec7a018d573b2ff268b06747c2527e5e153c Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Apr 2012 09:13:40 +0100 Subject: [PATCH 5/8] updated with note about jruby --- .travis.yml | 6 ++++-- README.md | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 69b1fe0..3b1f876 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,10 @@ rvm: - 1.8.7 - 1.9.2 - 1.9.3 - - jruby-18mode # JRuby in 1.8 mode - - jruby-19mode # JRuby in 1.9 mode +# JRuby fails with +# java.security.NoSuchAlgorithmException: sha256 MessageDigest not available +# - jruby-18mode # JRuby in 1.8 mode +# - jruby-19mode # JRuby in 1.9 mode - rbx-18mode - rbx-19mode diff --git a/README.md b/README.md index e442d61..3c4430e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,21 @@ Server example (sinatra) # Do whatever you need to do end +Pre-requisites +------------ +Tested against these Ruby versions + * 1.8.7 + * 1.9.2 + * 1.9.3 + * rbx-18mode + * rbx-19mode + +However JRuby currently fails with +java.security.NoSuchAlgorithmException: sha256 MessageDigest not available + * jruby-18mode # JRuby in 1.8 mode + * jruby-19mode # JRuby in 1.9 mode + + Developing ---------- From d3063442d632282a52cbb56c84045623e510f5a9 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Apr 2012 09:20:20 +0100 Subject: [PATCH 6/8] ruby code blocks for Github MD --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 3c4430e..da27150 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Examples Client example +```ruby params = {:some => 'parameters'} token = Signature::Token.new('my_key', 'my_secret') request = Signature::Request.new('POST', '/api/thing', params) @@ -17,9 +18,11 @@ Client example HTTParty.post('http://myservice/api/thing', { :query => query_params }) +``` `query_params` looks like: +```ruby { :some => "parameters", :auth_timestamp => 1273231888, @@ -28,8 +31,10 @@ Client example :auth_key => "my_key" } +``` Server example (sinatra) +```ruby error Signature::AuthenticationError do |controller| error = controller.env["sinatra.error"] halt 401, "401 UNAUTHORIZED: #{error.message}\n" @@ -44,6 +49,7 @@ Server example (sinatra) # Do whatever you need to do end +``` Pre-requisites ------------ From a7724fe9f03cd37541ef59c70c1586180e2c02b3 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Apr 2012 09:21:56 +0100 Subject: [PATCH 7/8] improve formatting of code snippets --- README.md | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index da27150..49acf21 100644 --- a/README.md +++ b/README.md @@ -9,46 +9,46 @@ Examples Client example ```ruby - params = {:some => 'parameters'} - token = Signature::Token.new('my_key', 'my_secret') - request = Signature::Request.new('POST', '/api/thing', params) - auth_hash = request.sign(token) - query_params = params.merge(auth_hash) - - HTTParty.post('http://myservice/api/thing', { - :query => query_params - }) +params = {:some => 'parameters'} +token = Signature::Token.new('my_key', 'my_secret') +request = Signature::Request.new('POST', '/api/thing', params) +auth_hash = request.sign(token) +query_params = params.merge(auth_hash) + +HTTParty.post('http://myservice/api/thing', { + :query => query_params +}) ``` `query_params` looks like: ```ruby - { - :some => "parameters", - :auth_timestamp => 1273231888, - :auth_signature => "28b6bb0f242f71064916fad6ae463fe91f5adc302222dfc02c348ae1941eaf80", - :auth_version => "1.0", - :auth_key => "my_key" - } +{ + :some => "parameters", + :auth_timestamp => 1273231888, + :auth_signature => "28b6bb0f242f71064916fad6ae463fe91f5adc302222dfc02c348ae1941eaf80", + :auth_version => "1.0", + :auth_key => "my_key" +} ``` Server example (sinatra) ```ruby - error Signature::AuthenticationError do |controller| - error = controller.env["sinatra.error"] - halt 401, "401 UNAUTHORIZED: #{error.message}\n" - end - - post '/api/thing' do - request = Signature::Request.new('POST', env["REQUEST_PATH"], params) - # This will raise a Signature::AuthenticationError if request does not authenticate - token = request.authenticate do |key| - Signature::Token.new(key, lookup_secret(key)) - end - - # Do whatever you need to do - end +error Signature::AuthenticationError do |controller| + error = controller.env["sinatra.error"] + halt 401, "401 UNAUTHORIZED: #{error.message}\n" +end + +post '/api/thing' do + request = Signature::Request.new('POST', env["REQUEST_PATH"], params) + # This will raise a Signature::AuthenticationError if request does not authenticate + token = request.authenticate do |key| + Signature::Token.new(key, lookup_secret(key)) + end + + # Do whatever you need to do +end ``` Pre-requisites From 5e114ac7278089add0b73c97b5d1cc28e9e2ae8e Mon Sep 17 00:00:00 2001 From: Martyn Loughran Date: Mon, 30 Apr 2012 15:43:54 +0100 Subject: [PATCH 8/8] Support jruby via the jruby-openssl gem --- .travis.yml | 6 ++---- signature.gemspec | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3b1f876..69b1fe0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,8 @@ rvm: - 1.8.7 - 1.9.2 - 1.9.3 -# JRuby fails with -# java.security.NoSuchAlgorithmException: sha256 MessageDigest not available -# - jruby-18mode # JRuby in 1.8 mode -# - jruby-19mode # JRuby in 1.9 mode + - jruby-18mode # JRuby in 1.8 mode + - jruby-19mode # JRuby in 1.9 mode - rbx-18mode - rbx-19mode diff --git a/signature.gemspec b/signature.gemspec index 48eb387..1f757da 100644 --- a/signature.gemspec +++ b/signature.gemspec @@ -17,5 +17,6 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] + s.add_dependency "jruby-openssl" if defined?(JRUBY_VERSION) s.add_development_dependency "rspec", "~> 2.9.0" end