Class: Oauth2MacClient::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2_mac_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_tokenObject

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

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/oauth2_mac_client.rb', line 8

def host
  @host
end

#issued_atObject

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_algorithmObject

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_keyObject

Returns the value of attribute mac_key.



7
8
9
# File 'lib/oauth2_mac_client.rb', line 7

def mac_key
  @mac_key
end

#methodObject

Returns the value of attribute method.



8
9
10
# File 'lib/oauth2_mac_client.rb', line 8

def method
  @method
end

#nonceObject



23
24
25
26
27
28
# File 'lib/oauth2_mac_client.rb', line 23

def nonce
  @nonce ||= [
    age,
    SecureRandom.hex
  ].join(':')
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/oauth2_mac_client.rb', line 8

def port
  @port
end

#request_uriObject

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

#ageObject



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_headerObject



68
69
70
71
72
73
74
# File 'lib/oauth2_mac_client.rb', line 68

def authorization_header
  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_hmacObject



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 construct_authorization_header(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
  authorization_header
end

#openssl_digestObject



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_stringObject



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