Class: Pili::Mac

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key) ⇒ Mac

Returns a new instance of Mac.



10
11
12
13
# File 'lib/pili/auth.rb', line 10

def initialize(access_key, secret_key)
  @access_key = access_key
  @secret_key = secret_key
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



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

def access_key
  @access_key
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

Instance Method Details

#sign(data) ⇒ Object



15
16
17
18
# File 'lib/pili/auth.rb', line 15

def sign(data)
  signature = Base64.urlsafe_encode64(Digest::HMAC.digest(data, @secret_key, Digest::SHA1))
  "#{@access_key}:#{signature}"
end

#sign_request(req) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pili/auth.rb', line 26

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