Simple generator, parser and validator for Oauth v2 HTTP message authentication code(MAC) header. It simply generates, parse and verify signatures for Oauth v2 HTTP MAC authentication for 'SHA1' and 'SHA256' algorithms. Please visit https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01 for spec specifications.
Installation
Add this line to your application's Gemfile:
gem 'oauth2_hmac_header'
And then execute:
$ bundle
Or install it yourself as:
$ gem install oauth2_hmac_header
Usage
Generating from request vars
@client_id = 'client1'
@algorithm = 'hmac-sha-256'
@key = 'demo_key'
@method = 'post'
@uri = '/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q'
@host = 'example.com'
@port = 443
@ext = 'a,b,c'
@header = Oauth2HmacHeader::AuthorizationHeader.generate_with_new_signature(
@client_id, @algorithm, @key, @method, @uri, @host, @port, @ext
)
Generating with early signed signatures
@client_id = 'client1'
@algorithm = 'hmac-sha-256'
@key = 'demo_key'
@method = 'post'
@uri = '/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q'
@host = 'example.com'
@port = 443
@ext = 'a,b,c'
@ts, @nonce, @ext, @mac = Oauth2HmacSign::Signature.generate(
@algorithm, @key, @method, @uri, @host, @port, @ext
)
@header = Oauth2HmacHeader::AuthorizationHeader.generate(
@client_id, @ts, @nonce, @ext, @mac
)
Parsing
@header = "MAC id=\"client1\", ts=\"1438530720\", nonce=\"1438530720:ab4412bd\", ext=\"a,b,c\", mac=\"Sav0I-p1rAU29TlISoznME5xeOzJIPZEvG26ni_APNE=\""
@client_id, @ts, @nonce, @ext, @mac = Oauth2HmacHeader::AuthorizationHeader.parse(@header)
Verify
@method = 'post'
@uri = '/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q'
@host = 'example.com'
@port = 443
@header = "MAC id=\"client1\", ts=\"1438530720\", nonce=\"1438530720:ab4412bd\", ext=\"a,b,c\", mac=\"Sav0I-p1rAU29TlISoznME5xeOzJIPZEvG26ni_APNE=\""
@client_id, @ts, @nonce, @ext, @mac = Oauth2HmacHeader::AuthorizationHeader.parse(@header)
@key = 'demo_key'
@algorithm = 'hmac-sha-256'
Oauth2HmacHeader::AuthorizationHeader.is_valid?(
@mac,
@algorithm,
@key,
@ts,
@nonce,
@method,
@uri,
@host,
@port,
@ext
)
Contributing
- Fork it ( https://github.com/mustafaturan/oauth2_hmac_header/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request