class OAuth2::MACToken
Attributes
Public Class Methods
Generates a MACToken
from an AccessToken
and secret
@param [AccessToken] token the OAuth2::Token instance @option [String] secret the secret key value @param [Hash] opts the options to create the Access Token with @see MACToken#initialize
# File lib/oauth2/mac_token.rb, line 9 def self.from_access_token(token, secret, options = {}) 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(options)) end
Initalize a MACToken
@param [Client] client the OAuth2::Client
instance @param [String] token the Access Token value @option [String] secret the secret key value @param [Hash] opts the options to create the Access Token with @option opts [String] :refresh_token (nil) the refresh_token value @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken
will expire @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken
will expire @option opts [FixNum, String] :algorithm (hmac-sha-256) the algorithm to use for the HMAC digest (one of ‘hmac-sha-256’, ‘hmac-sha-1’)
# File lib/oauth2/mac_token.rb, line 29 def initialize(client, token, secret, opts = {}) @secret = secret self.algorithm = opts.delete(:algorithm) || "hmac-sha-256" super(client, token, opts) end
Public Instance Methods
Set the HMAC algorithm
@param [String] alg the algorithm to use (one of ‘hmac-sha-1’, ‘hmac-sha-256’)
# File lib/oauth2-cocoa/mac_token.rb, line 6 def algorithm=(alg) @algorithm = begin case alg.to_s when "hmac-sha-1" "hmacSha1:hmacKey" when "hmac-sha-256" "hmacSha256:hmacKey" else fail(ArgumentError, "Unsupported algorithm") end end end
Generate the MAC header
@param [Symbol] verb the HTTP request method @param [String] url the HTTP URL path of the request
# File lib/oauth2/mac_token.rb, line 60 def header(verb, url) timestamp = Time.now.utc.to_i nonce = generate_nonce mac = signature(timestamp, nonce, verb, url) "MAC id=\"#{token}\", ts=\"#{timestamp}\", nonce=\"#{nonce}\", mac=\"#{mac}\"" end
Get the headers hash (always an empty hash)
# File lib/oauth2/mac_token.rb, line 52 def headers {} end
Make a request with the MAC Token
@param [Symbol] verb the HTTP request method @param [String] path the HTTP URL path of the request @param [Hash] opts the options to make the request with @see Client#request
# File lib/oauth2/mac_token.rb, line 42 def request(verb, path, opts = {}, &block) url = client.connection.build_url(path, opts[:params]).to_s opts[:headers] ||= {} opts[:headers].merge!("Authorization" => header(verb, url)) @client.request(verb, path, opts, &block) end
Generate the Base64-encoded HMAC digest signature
@param [Fixnum] timestamp the timestamp of the request in seconds since epoch @param [String] nonce the MAC header nonce @param [Symbol] verb the HTTP request method @param [String] url the HTTP URL path of the request
# File lib/oauth2-cocoa/mac_token.rb, line 25 def signature(timestamp, nonce, verb, url) nsurl = NSURL.URLWithString(url.to_s) fail(ArgumentError, "could not parse \"#{url}\" into NSURL") unless nsurl.host path = nsurl.path path = "/" if path == "" port = nsurl.port port = nsurl.scheme == "https" ? 443 : 80 unless port signature = [ timestamp, nonce, verb.to_s.upcase, path, nsurl.host, port, "", nil ].join("\n") digest = CocoaSecurity.send(algorithm, signature, secret) digest.base64 end
Private Instance Methods
# File lib/oauth2-cocoa/mac_token.rb, line 51 def generate_nonce timestamp = Time.now.utc.to_i uuid = CFUUIDCreate(nil) string = CFUUIDCreateString(nil, uuid) CocoaSecurity.md5([timestamp, string].join(":")).hex end
No-op since we need the verb and path and the MAC always goes in a header
# File lib/oauth2/mac_token.rb, line 71 def token=(_) end