Class: OAuth2::MACToken

Inherits:
AccessToken show all
Defined in:
lib/oauth2/mac_token.rb

Instance Attribute Summary collapse

Attributes inherited from AccessToken

#client, #expires_at, #expires_in, #expires_latency, #options, #params, #refresh_token, #response, #token

Class Method Summary collapse

Instance Method Summary collapse

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

Options Hash (opts):

  • :refresh_token (String) — default: nil

    the refresh_token value

  • :expires_in (FixNum, String) — default: nil

    the number of seconds in which the AccessToken will expire

  • :expires_at (FixNum, String) — default: nil

    the epoch time in seconds in which AccessToken will expire

  • :algorithm (FixNum, String) — default: hmac-sha-256

    the algorithm to use for the HMAC digest (one of 'hmac-sha-256', 'hmac-sha-1')


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

#algorithmObject

Returns the value of attribute algorithm


18
19
20
# File 'lib/oauth2/mac_token.rb', line 18

def algorithm
  @algorithm
end

#secretObject (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

See Also:


14
15
16
# File 'lib/oauth2/mac_token.rb', line 14

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

Instance Method Details

#header(verb, url) ⇒ Object

Generate the MAC header

Raises:

  • (ArgumentError)

61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/oauth2/mac_token.rb', line 61

def header(verb, url)
  timestamp = Time.now.utc.to_i
  nonce = Digest::MD5.hexdigest([timestamp, SecureRandom.hex].join(':'))

  uri = URI.parse(url)

  raise(ArgumentError, "could not parse \"#{url}\" into URI") unless uri.is_a?(URI::HTTP)

  mac = signature(timestamp, nonce, verb, uri)

  "MAC id=\"#{token}\", ts=\"#{timestamp}\", nonce=\"#{nonce}\", mac=\"#{mac}\""
end

#headersObject

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

See Also:


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(timestamp, nonce, verb, uri)
  signature = [
    timestamp,
    nonce,
    verb.to_s.upcase,
    uri.request_uri,
    uri.host,
    uri.port,
    '', nil
  ].join("\n")

  Base64.strict_encode64(OpenSSL::HMAC.digest(@algorithm, secret, signature))
end