Class: Pili::Credentials
- Inherits:
-
Object
- Object
- Pili::Credentials
- Defined in:
- lib/pili/credentials.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.
Class Method Summary collapse
- .base64_url_encode(string) ⇒ Object
- .digest(secret, bytes) ⇒ Object
- .generate_signature(options = {}) ⇒ Object
- .sign(secret, bytes) ⇒ Object
Instance Method Summary collapse
-
#initialize(access_key, secret_key) ⇒ Credentials
constructor
A new instance of Credentials.
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_key ⇒ Object (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_key ⇒ Object (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( = {}) method = [:method] url = [:url] content_type = [:content_type] body = [: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 |