Skip to content

Instantly share code, notes, and snippets.

@jkassemi
Last active January 4, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkassemi/8641384 to your computer and use it in GitHub Desktop.
Save jkassemi/8641384 to your computer and use it in GitHub Desktop.
JKSR: Signing url-form-encoded messages

Standardization On: Signing url-form-encoded messages

UPDATE: Please see http://self-issued.info/docs/draft-jones-json-web-token-06.html and the implementation here https://github.com/progrium/ruby-jwt for another method for JSON specific requests.

(if you run into this post and are about to use a different method that doesn't pose any particular benefit, use this one - post an alternative if there's a benefit!)

PROBLEM

When a third party is given the option of sending a url-form-encoded request containing a signature value, we need a method of signing all keys and values as well as a signature, without the inclusion of the signature itself in the hash.

    http://example.com/?a=1&c=3&b=2&signature=abcdefg

METHOD

Signature

  1. Parse query string:
    url = http://example.com/?a=1&b=2&c=3
    #1(url) = ["http://example.com", "a=1&c=3&b=2"]
  1. Order by keys and values in array with "#{k}#{v}"
    #2("a=1&c=3&b=2") = ["a1", "c3", "b4"]
  1. Sort lexicographically, join
    #3(["a1", "c3", "b4"]) =  "a1b4c3"
  1. Sign (algorithm not specified but likely HMAC-* or NACL Signature Verification)
    #4("a1b4c3") = SIGNATURE
  1. Use in original URL
    #5(SIGNATURE) = http://example.com/?a=1&c=3&b=2&signature=SIGNATURE

Verification

Perform above on incoming request but remove 'signature' parameter from the encoded string before calcuating the signature. Compare the two and ensure they are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment