Class: Pili::Mac
- Inherits:
-
Object
- Object
- Pili::Mac
- Defined in:
- lib/pili/auth.rb
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Instance Method Summary collapse
-
#initialize(access_key, secret_key) ⇒ Mac
constructor
A new instance of Mac.
- #sign(data) ⇒ Object
- #sign_request(req) ⇒ Object
Constructor Details
#initialize(access_key, secret_key) ⇒ Mac
Returns a new instance of Mac.
11 12 13 14 |
# File 'lib/pili/auth.rb', line 11 def initialize(access_key, secret_key) @access_key = access_key @secret_key = secret_key end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
9 10 11 |
# File 'lib/pili/auth.rb', line 9 def access_key @access_key end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
9 10 11 |
# File 'lib/pili/auth.rb', line 9 def secret_key @secret_key end |
Instance Method Details
#sign(data) ⇒ Object
16 17 18 19 20 |
# File 'lib/pili/auth.rb', line 16 def sign(data) digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha1"), @secret_key, data) signature = Base64.urlsafe_encode64(digest) "#{@access_key}:#{signature}" end |
#sign_request(req) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pili/auth.rb', line 28 def sign_request(req) data = "#{req.method} #{req.path}" # FIXME: how to deal with port, because default uri.port is 80 data += "\nHost: #{req.uri.host}" content_type = req["Content-Type"] if content_type != nil data += "\nContent-Type: #{content_type}" end data += "\n\n" if inc_body(req, content_type) data += "#{req.body}" end sign(data) end |