Class: Pili::Credentials

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key) ⇒ Credentials

Returns a new instance of Credentials.



12
13
14
15
# File 'lib/pili/credentials.rb', line 12

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.



9
10
11
# File 'lib/pili/credentials.rb', line 9

def access_key
  @access_key
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



9
10
11
# File 'lib/pili/credentials.rb', line 9

def secret_key
  @secret_key
end

Class Method Details

.base64_url_encode(string) ⇒ Object



20
21
22
# File 'lib/pili/credentials.rb', line 20

def base64_url_encode(string)
  Base64.encode64(string).tr("+/", "-_").gsub(/[\n\r]?/, "")
end

.digest(secret, bytes) ⇒ Object



24
25
26
# File 'lib/pili/credentials.rb', line 24

def digest(secret, bytes)
  OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, bytes)
end

.generate_signature(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pili/credentials.rb', line 32

def generate_signature(options = {})
  method        = options[:method]
  url           = options[:url]
  content_type  = options[:content_type]
  body          = options[:body]

  uri = URI.parse(url)

  signature = "#{method} #{uri.path}"

  query_string = uri.query

  if !query_string.nil? && !query_string.empty?
    signature += '?' + query_string
  end

  signature += "\nHost: #{uri.host}"

  if !content_type.nil? && !content_type.empty? && content_type != "application/octet-stream"
    signature += "\nContent-Type: #{content_type}"
  end

  signature += "\n\n"

  if body.is_a?(Hash)
    signature += body.to_json
  end

  return signature
end

.sign(secret, bytes) ⇒ Object



28
29
30
# File 'lib/pili/credentials.rb', line 28

def sign(secret, bytes)
  base64_url_encode(digest(secret, bytes))
end