Class: OAuth2::MACToken
- Inherits:
-
AccessToken
- Object
- AccessToken
- OAuth2::MACToken
- Defined in:
- lib/oauth2/mac_token.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Attributes inherited from AccessToken
#client, #expires_at, #expires_in, #options, #params, #refresh_token, #token
Class Method Summary collapse
-
.from_access_token(token, secret, options = {}) ⇒ Object
Generates a MACToken from an AccessToken and secret.
Instance Method Summary collapse
-
#header(verb, url) ⇒ Object
Generate the MAC header.
-
#headers ⇒ Object
Get the headers hash (always an empty hash).
-
#initialize(client, token, secret, opts = {}) ⇒ MACToken
constructor
Initalize a MACToken.
-
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the MAC Token.
-
#signature(timestamp, nonce, verb, uri) ⇒ Object
Generate the Base64-encoded HMAC digest signature.
Methods inherited from AccessToken
#[], #delete, #expired?, #expires?, from_hash, from_kvform, #get, #patch, #post, #put, #refresh!, #to_hash
Constructor Details
#initialize(client, token, secret, opts = {}) ⇒ MACToken
Initalize a MACToken
32 33 34 35 36 37 |
# File 'lib/oauth2/mac_token.rb', line 32 def initialize(client, token, secret, opts = {}) @secret = secret self.algorithm = opts.delete(:algorithm) || 'hmac-sha-256' super(client, token, opts) end |
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
20 21 22 |
# File 'lib/oauth2/mac_token.rb', line 20 def algorithm @algorithm end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
20 21 22 |
# File 'lib/oauth2/mac_token.rb', line 20 def secret @secret end |
Class Method Details
.from_access_token(token, secret, options = {}) ⇒ Object
Generates a MACToken from an AccessToken and secret
16 17 18 |
# File 'lib/oauth2/mac_token.rb', line 16 def self.from_access_token(token, secret, = {}) new(token.client, token.token, secret, token.params.merge(:refresh_token => token.refresh_token, :expires_in => token.expires_in, :expires_at => token.expires_at).merge()) end |
Instance Method Details
#header(verb, url) ⇒ Object
Generate the MAC header
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/oauth2/mac_token.rb', line 63 def header(verb, url) = Time.now.utc.to_i nonce = Digest::SHA256.hexdigest([, SecureRandom.hex].join(':')) uri = URI.parse(url) raise(ArgumentError, "could not parse \"#{url}\" into URI") unless uri.is_a?(URI::HTTP) mac = signature(, nonce, verb, uri) "MAC id=\"#{token}\", ts=\"#{timestamp}\", nonce=\"#{nonce}\", mac=\"#{mac}\"" end |
#headers ⇒ Object
Get the headers hash (always an empty hash)
55 56 57 |
# File 'lib/oauth2/mac_token.rb', line 55 def headers {} end |
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the MAC Token
45 46 47 48 49 50 51 52 |
# File 'lib/oauth2/mac_token.rb', line 45 def request(verb, path, opts = {}, &block) url = client.connection.build_url(path, opts[:params]).to_s opts[:headers] ||= {} opts[:headers]['Authorization'] = header(verb, url) @client.request(verb, path, opts, &block) end |
#signature(timestamp, nonce, verb, uri) ⇒ Object
Generate the Base64-encoded HMAC digest signature
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/oauth2/mac_token.rb', line 82 def signature(, nonce, verb, uri) signature = [ , nonce, verb.to_s.upcase, uri.request_uri, uri.host, uri.port, '', nil ].join("\n") strict_encode64(OpenSSL::HMAC.digest(@algorithm, secret, signature)) end |