Class: Shmac::Authentication
- Inherits:
-
Object
- Object
- Shmac::Authentication
- Includes:
- Comparable
- Defined in:
- lib/shmac/authentication.rb
Instance Attribute Summary collapse
-
#header_namespace ⇒ Object
readonly
Returns the value of attribute header_namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Class Method Summary collapse
- .generate_authorization_header(request, secret:, access_key:, organization:, header_namespace: nil) ⇒ Object
- .generate_signature(request, secret:, header_namespace: nil) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #authentic? ⇒ Boolean
-
#initialize(secret, request, header_namespace: nil, request_adapter: nil, options: {}) ⇒ Authentication
constructor
A new instance of Authentication.
- #request ⇒ Object
- #request_adapter ⇒ Object
- #signature ⇒ Object
Constructor Details
#initialize(secret, request, header_namespace: nil, request_adapter: nil, options: {}) ⇒ Authentication
Returns a new instance of Authentication.
23 24 25 26 27 28 29 |
# File 'lib/shmac/authentication.rb', line 23 def initialize secret, request, header_namespace: nil, request_adapter: nil, options: {} @secret = secret @request = request @request_adapter = request_adapter @header_namespace = header_namespace self. = end |
Instance Attribute Details
#header_namespace ⇒ Object (readonly)
Returns the value of attribute header_namespace.
9 10 11 |
# File 'lib/shmac/authentication.rb', line 9 def header_namespace @header_namespace end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/shmac/authentication.rb', line 9 def @options end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
9 10 11 |
# File 'lib/shmac/authentication.rb', line 9 def secret @secret end |
Class Method Details
.generate_authorization_header(request, secret:, access_key:, organization:, header_namespace: nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/shmac/authentication.rb', line 11 def self. request, secret:, access_key:, organization:, header_namespace: nil AuthorizationHeader.generate( organization: organization, access_key: access_key, signature: self.generate_signature(request, secret: secret, header_namespace: header_namespace) ).to_s end |
.generate_signature(request, secret:, header_namespace: nil) ⇒ Object
19 20 21 |
# File 'lib/shmac/authentication.rb', line 19 def self.generate_signature request, secret:, header_namespace: nil new(secret, request, header_namespace: header_namespace).signature end |
Instance Method Details
#==(other) ⇒ Object
31 32 33 34 35 |
# File 'lib/shmac/authentication.rb', line 31 def == other return false unless other.is_a?(self.class) Security.secure_compare self.signature, other.signature end |
#authentic? ⇒ Boolean
46 47 48 49 50 51 52 53 |
# File 'lib/shmac/authentication.rb', line 46 def authentic? return false if request..to_s.strip.empty? return false if request.tampered_body? given_signature = AuthorizationHeader.new(request.).signature Security.secure_compare given_signature, self.signature end |
#request ⇒ Object
55 56 57 |
# File 'lib/shmac/authentication.rb', line 55 def request request_adapter.call @request end |
#request_adapter ⇒ Object
59 60 61 |
# File 'lib/shmac/authentication.rb', line 59 def request_adapter @request_adapter ||= ->(r) { r } end |
#signature ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/shmac/authentication.rb', line 37 def signature SignatureCalculator.new( secret: self.secret, request: self.request, header_namespace: self.header_namespace, options: { skip_path: self.[:skip_path] } ).to_s end |