Class: Oauth2MacClient::Token
- Inherits:
-
Object
- Object
- Oauth2MacClient::Token
- Defined in:
- lib/oauth2_mac_client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#body_hash(body) ⇒ Object
Returns the value of attribute body_hash.
-
#host ⇒ Object
Returns the value of attribute host.
-
#issued_at ⇒ Object
Returns the value of attribute issued_at.
-
#mac_algorithm ⇒ Object
Returns the value of attribute mac_algorithm.
-
#mac_key ⇒ Object
Returns the value of attribute mac_key.
-
#method ⇒ Object
Returns the value of attribute method.
- #nonce ⇒ Object
-
#port ⇒ Object
Returns the value of attribute port.
-
#request_uri ⇒ Object
Returns the value of attribute request_uri.
Instance Method Summary collapse
- #age ⇒ Object
- #authorization_header ⇒ Object
- #base64_encode(text) ⇒ Object
- #calculate_hmac ⇒ Object
- #construct_authorization_header(url, method, body = "") ⇒ Object
-
#initialize(attributes = {}) ⇒ Token
constructor
A new instance of Token.
- #openssl_digest ⇒ Object
- #request_string ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Token
Returns a new instance of Token.
11 12 13 14 15 16 |
# File 'lib/oauth2_mac_client.rb', line 11 def initialize(attributes={}) @access_token = attributes[:access_token] @mac_key = attributes[:mac_key] @mac_algorithm = attributes[:mac_algorithm] @issued_at = attributes[:issued_at] || Time.now.utc end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
7 8 9 |
# File 'lib/oauth2_mac_client.rb', line 7 def access_token @access_token end |
#body_hash(body) ⇒ Object
Returns the value of attribute body_hash.
8 9 10 |
# File 'lib/oauth2_mac_client.rb', line 8 def body_hash @body_hash end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/oauth2_mac_client.rb', line 8 def host @host end |
#issued_at ⇒ Object
Returns the value of attribute issued_at.
7 8 9 |
# File 'lib/oauth2_mac_client.rb', line 7 def issued_at @issued_at end |
#mac_algorithm ⇒ Object
Returns the value of attribute mac_algorithm.
7 8 9 |
# File 'lib/oauth2_mac_client.rb', line 7 def mac_algorithm @mac_algorithm end |
#mac_key ⇒ Object
Returns the value of attribute mac_key.
7 8 9 |
# File 'lib/oauth2_mac_client.rb', line 7 def mac_key @mac_key end |
#method ⇒ Object
Returns the value of attribute method.
8 9 10 |
# File 'lib/oauth2_mac_client.rb', line 8 def method @method end |
#nonce ⇒ Object
23 24 25 26 27 28 |
# File 'lib/oauth2_mac_client.rb', line 23 def nonce @nonce ||= [ age, SecureRandom.hex ].join(':') end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/oauth2_mac_client.rb', line 8 def port @port end |
#request_uri ⇒ Object
Returns the value of attribute request_uri.
8 9 10 |
# File 'lib/oauth2_mac_client.rb', line 8 def request_uri @request_uri end |
Instance Method Details
#age ⇒ Object
18 19 20 21 |
# File 'lib/oauth2_mac_client.rb', line 18 def age age = Time.now.utc - @issued_at age.to_i end |
#authorization_header ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/oauth2_mac_client.rb', line 68 def header = "MAC" header << " id=\"#{@access_token}\"," header << " nonce=\"#{nonce}\"," header << " bodyhash=\"#{@body_hash}\"," header << " mac=\"#{@hmac}\"," end |
#base64_encode(text) ⇒ Object
41 42 43 |
# File 'lib/oauth2_mac_client.rb', line 41 def base64_encode(text) Base64.encode64(text).gsub(/\n/,'') end |
#calculate_hmac ⇒ Object
52 53 54 55 |
# File 'lib/oauth2_mac_client.rb', line 52 def calculate_hmac result = OpenSSL::HMAC.digest(openssl_digest,@mac_key,request_string) base64_encode result end |
#construct_authorization_header(url, method, body = "") ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/oauth2_mac_client.rb', line 57 def (url,method,body="") @body_hash = body.empty? ? "" : body_hash(body) @method=method.upcase uri = URI.parse(url) @host=uri.host @port=uri.port @request_uri=uri.request_uri @hmac=calculate_hmac end |
#openssl_digest ⇒ Object
45 46 47 48 49 50 |
# File 'lib/oauth2_mac_client.rb', line 45 def openssl_digest @openssl_digest ||= case @mac_algorithm when 'hmac-sha-256' then OpenSSL::Digest::SHA256.new when 'hmac-sha-1' then OpenSSL::Digest::SHA1.new end end |
#request_string ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/oauth2_mac_client.rb', line 30 def request_string [nonce, @method, @request_uri, @host, @port, @body_hash, '', # ext nil].join("\n") end |