Class: Shmac::Authentication

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/shmac/authentication.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.options = options
end

Instance Attribute Details

#header_namespaceObject (readonly)

Returns the value of attribute header_namespace.



9
10
11
# File 'lib/shmac/authentication.rb', line 9

def header_namespace
  @header_namespace
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/shmac/authentication.rb', line 9

def options
  @options
end

#secretObject (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.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) ⇒ 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

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# 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

#requestObject



55
56
57
# File 'lib/shmac/authentication.rb', line 55

def request
  request_adapter.call @request
end

#request_adapterObject



59
60
61
# File 'lib/shmac/authentication.rb', line 59

def request_adapter
  @request_adapter ||= ->(r) { r }
end

#signatureObject



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.options[:skip_path] }
  ).to_s
end