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) ⇒ Authentication

Returns a new instance of Authentication.



22
23
24
25
26
27
# File 'lib/shmac/authentication.rb', line 22

def initialize secret, request, header_namespace: nil, request_adapter: nil
  @secret = secret
  @request = request
  @request_adapter = request_adapter
  @header_namespace = header_namespace
end

Instance Attribute Details

#header_namespaceObject (readonly)

Returns the value of attribute header_namespace.



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

def header_namespace
  @header_namespace
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

Class Method Details

.generate_authorization_header(request, secret:, access_key:, organization:, header_namespace: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/shmac/authentication.rb', line 10

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



18
19
20
# File 'lib/shmac/authentication.rb', line 18

def self.generate_signature request, secret:, header_namespace: nil
  new(secret, request, header_namespace: header_namespace).signature
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
# File 'lib/shmac/authentication.rb', line 29

def == other
  other.is_a?(self.class) && self.signature == other.signature
end

#authentic?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/shmac/authentication.rb', line 41

def authentic?
  return false if request.authorization.to_s.strip.empty?

  AuthorizationHeader.new(request.authorization).signature == self.signature
end

#requestObject



47
48
49
# File 'lib/shmac/authentication.rb', line 47

def request
  request_adapter.call @request
end

#request_adapterObject



51
52
53
# File 'lib/shmac/authentication.rb', line 51

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

#signatureObject



33
34
35
36
37
38
39
# File 'lib/shmac/authentication.rb', line 33

def signature
  SignatureCalculator.new(
    secret: self.secret,
    request: self.request,
    header_namespace: self.header_namespace
  ).to_s
end