Skip to content

Commit

Permalink
Add Request#signed_params method
Browse files Browse the repository at this point in the history
  • Loading branch information
mloughran committed Jun 21, 2012
1 parent 52aae61 commit c1f8d5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(method, path, query)

@method = method.upcase
@path, @query_hash, @auth_hash = path, query_hash, auth_hash
@signed = false
end

# Sign the request with the given token, and return the computed
Expand All @@ -45,9 +46,10 @@ def sign(token)
:auth_key => token.key,
:auth_timestamp => Time.now.to_i.to_s
}

@auth_hash[:auth_signature] = signature(token)

@signed = true

return @auth_hash
end

Expand Down Expand Up @@ -111,10 +113,16 @@ def authenticate(timestamp_grace = 600)
# Expose the authentication parameters for a signed request
#
def auth_hash
raise "Request not signed" unless @auth_hash && @auth_hash[:auth_signature]
raise "Request not signed" unless @signed
@auth_hash
end

# Query parameters merged with the computed authentication parameters
#
def signed_params
@query_hash.merge(auth_hash)
end

private

def signature(token)
Expand Down
2 changes: 1 addition & 1 deletion spec/signature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
describe "verification" do
before :each do
@request.sign(@token)
@params = @request.query_hash.merge(@request.auth_hash)
@params = @request.signed_params
end

it "should verify requests" do
Expand Down

0 comments on commit c1f8d5a

Please sign in to comment.