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, #options, #params, #refresh_token, #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

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • token (String)

    the Access Token value

  • opts (Hash) (defaults to: {})

    the options to create the Access Token with

  • [String] (Hash)

    a customizable set of options

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

Parameters:

  • token (AccessToken)

    the OAuth2::Token instance

  • opts (Hash)

    the options to create the Access Token with

  • [String] (Hash)

    a customizable set of options

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

Parameters:

  • verb (Symbol)

    the HTTP request method

  • url (String)

    the HTTP URL path of the request

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

Parameters:

  • verb (Symbol)

    the HTTP request method

  • path (String)

    the HTTP URL path of the request

  • opts (Hash) (defaults to: {})

    the options to make the request with

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

Parameters:

  • timestamp (Fixnum)

    the timestamp of the request in seconds since epoch

  • nonce (String)

    the MAC header nonce

  • verb (Symbol)

    the HTTP request method

  • url (String)

    the HTTP URL path of the request



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")

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