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
30 31 32 33 34 35 |
# File 'lib/oauth2/mac_token.rb', line 30 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.
18 19 20 |
# File 'lib/oauth2/mac_token.rb', line 18 def algorithm @algorithm end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
18 19 20 |
# File 'lib/oauth2/mac_token.rb', line 18 def secret @secret end |
Class Method Details
.from_access_token(token, secret, options = {}) ⇒ Object
Generates a MACToken from an AccessToken and secret
14 15 16 |
# File 'lib/oauth2/mac_token.rb', line 14 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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/oauth2/mac_token.rb', line 61 def header(verb, url) = Time.now.utc.to_i nonce = Digest::MD5.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=\"#{}\", nonce=\"#{nonce}\", mac=\"#{mac}\"" end |
#headers ⇒ Object
Get the headers hash (always an empty hash)
53 54 55 |
# File 'lib/oauth2/mac_token.rb', line 53 def headers {} end |
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the MAC Token
43 44 45 46 47 48 49 50 |
# File 'lib/oauth2/mac_token.rb', line 43 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/oauth2/mac_token.rb', line 80 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 |