Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add OAuthy::SignatureMethods::HMACSHA1 class and spec.

  • Loading branch information...
commit 7438a816dd33f8a8f25391ffa71a1ed96d1a84c0 1 parent 5d9c7ca
@adamtanner authored
View
29 lib/oauthy/signature_methods/hmac_sha1.rb
@@ -0,0 +1,29 @@
+require "base64"
+require "openssl"
+require "cgi"
+
+module OAuthy
+ module SignatureMethods
+ class HMACSHA1
+ def initialize(attributes = {})
+ @signature_base_string = attributes.fetch(:signature_base_string)
+ @consumer_key = attributes.fetch(:consumer_key)
+ @consumer_secret = attributes.fetch(:consumer_secret)
+ end
+
+ def generate
+ oauth_signature = OpenSSL::HMAC.digest("sha1", key, @signature_base_string)
+ oauth_signature = Base64.encode64(oauth_signature).chomp
+ oauth_signature = CGI.escape(oauth_signature)
+ end
+
+ private
+ def key
+ escaped_consumer_secret = CGI.escape(@consumer_secret)
+ escaped_consumer_key = CGI.escape(@consumer_key)
+
+ [escaped_consumer_secret, escaped_consumer_key].join("&")
+ end
+ end
+ end
+end
View
36 spec/oauthy/signature_methods/hmac_sha1_spec.rb
@@ -0,0 +1,36 @@
+require "oauthy"
+
+describe OAuthy::SignatureMethods::HMACSHA1 do
+ let(:oauth_signature) do
+ "tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"
+ end
+
+ let(:signature_base_string) do
+ "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"
+ end
+
+ let(:consumer_key) do
+ "pfkkdhi9sl3r4s00"
+ end
+
+ let(:consumer_secret) do
+ "kd94hf93k423kf44"
+ end
+
+ subject do
+ OAuthy::SignatureMethods::HMACSHA1.new(
+ :signature_base_string => signature_base_string,
+ :consumer_key => consumer_key,
+ :consumer_secret => consumer_secret
+ )
+ end
+
+ describe "#generate" do
+ it "generates a valid OAuth signature" do
+ subject.generate.should eq(oauth_signature)
+ end
+ end
+
+ describe "#verify" do
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.