Class: Rack::Indicium

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/indicium.rb,
lib/rack/indicium/sentry.rb,
lib/rack/indicium/version.rb

Defined Under Namespace

Classes: Sentry

Constant Summary collapse

HTTP_AUTHORIZATION =
"HTTP_AUTHORIZATION".freeze
BEARER_REGEXP =
/\ABearer /i
VERSION =
"1.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(app, secret, decoder = nil) ⇒ Indicium

Returns a new instance of Indicium.



10
11
12
13
14
15
# File 'lib/rack/indicium.rb', line 10

def initialize(app, secret, decoder = nil)
  @app = app

  @secret = secret
  @decoder = decoder || lambda { |jwt, secret| JWT.decode(jwt, secret) }
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
# File 'lib/rack/indicium.rb', line 17

def call(env)
  look_for_authorization_header(env)

  @app.call(env)
end

#decode(jwt) ⇒ Object



40
41
42
43
# File 'lib/rack/indicium.rb', line 40

def decode(jwt)
  @decoder.call(jwt, @secret)
rescue
end

#look_for_authorization_header(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/indicium.rb', line 23

def look_for_authorization_header(env)
  authorization_header = env[HTTP_AUTHORIZATION]
  return unless authorization_header

  _, jwt = authorization_header.split(BEARER_REGEXP)
  return unless jwt

  jwt_payload, jwt_header = decode(jwt)

  return unless jwt_payload
  return unless jwt_header

  env["jwt.raw"]     = jwt
  env["jwt.payload"] = jwt_payload
  env["jwt.header"]  = jwt_header
end