Class: Sworn::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sworn/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
# File 'lib/sworn/middleware.rb', line 7

def initialize(app, options = {})
  @app = app
  @verifier_class = options.fetch(:verifier_class) { Verifier }
end

Instance Attribute Details

#verifier_classObject (readonly)

Returns the value of attribute verifier_class.



5
6
7
# File 'lib/sworn/middleware.rb', line 5

def verifier_class
  @verifier_class
end

Instance Method Details

#bad_requestObject



24
25
26
# File 'lib/sworn/middleware.rb', line 24

def bad_request
  [400, {}, ["Bad request"]]
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sworn/middleware.rb', line 12

def call(env)
  request = Rack::Request.new(env)
  verifier = verifier_class.new(request)

  return bad_request    if verifier.unsigned?
  return not_authorized if verifier.expired?
  return not_authorized if verifier.replayed?
  return not_authorized unless verifier.valid?

  return @app.call(env)
end

#not_authorizedObject



28
29
30
# File 'lib/sworn/middleware.rb', line 28

def not_authorized
  [401, {}, ["Not authorized"]]
end