class Shmac::Authentication

Attributes

header_namespace[R]
options[R]
secret[R]

Public Class Methods

generate_authorization_header(request, secret:, access_key:, organization:, header_namespace: nil) click to toggle source
# File lib/shmac/authentication.rb, line 11
def self.generate_authorization_header 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) click to toggle source
# 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
new(secret, request, header_namespace: nil, request_adapter: nil, options: {}) click to toggle source
# 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.options = options
end

Public Instance Methods

==(other) click to toggle source
# 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?() click to toggle source
# File lib/shmac/authentication.rb, line 46
def authentic?
  return false if request.authorization.to_s.strip.empty?
  return false if request.tampered_body?

  given_signature = AuthorizationHeader.new(request.authorization).signature

  Security.secure_compare given_signature, self.signature
end
request() click to toggle source
# File lib/shmac/authentication.rb, line 55
def request
  request_adapter.call @request
end
request_adapter() click to toggle source
# File lib/shmac/authentication.rb, line 59
def request_adapter
  @request_adapter ||= ->(r) { r }
end
signature() click to toggle source
# 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.options[:skip_path] }
  ).to_s
end

Private Instance Methods

default_options() click to toggle source
# File lib/shmac/authentication.rb, line 72
def default_options
  { skip_path: false, validate_body_contents: true }
end
options=(opts = {}) click to toggle source
# File lib/shmac/authentication.rb, line 65
def options= opts = {}
  unknown_keys = opts.keys - default_options.keys
  raise ArgumentError.new("Unknown options: #{unknown_keys.join(", ")}") if unknown_keys.any?

  @options = default_options.merge(opts)
end