Class: M2mKeygen::RackValidator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/m2m_keygen/rack_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret, algorithm: 'sha512', header_name: 'X-Signature') ⇒ RackValidator

Returns a new instance of RackValidator.



16
17
18
19
# File 'lib/m2m_keygen/rack_validator.rb', line 16

def initialize(secret, algorithm: 'sha512', header_name: 'X-Signature')
  @header_name = T.let("HTTP_#{header_name.tr('-', '_').upcase}", String)
  @signature = T.let(Signature.new(secret, algorithm: algorithm), Signature)
end

Instance Attribute Details

#header_nameObject (readonly)

Returns the value of attribute header_name.



13
14
15
# File 'lib/m2m_keygen/rack_validator.rb', line 13

def header_name
  @header_name
end

#signatureObject (readonly)

Returns the value of attribute signature.



10
11
12
# File 'lib/m2m_keygen/rack_validator.rb', line 10

def signature
  @signature
end

Instance Method Details

#validate(req) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/m2m_keygen/rack_validator.rb', line 22

def validate(req)
  # This will cover the case when Rails is used.
  req = Rack::Request.new(req.env)
  @signature.validate(
    params: req.params || {},
    verb: req.request_method || 'get',
    path: req.path || '/',
    signature: req.env['HTTP_X_SIGNATURE'] || '',
  ) && req.params['expiry'] && req.params['expiry'].to_i > Time.now.to_i &&
    req.params['expiry'].to_i < Time.now.to_i + 120
end