Class: SimpleJwtAuth::Middleware::Grape::Jwt

Inherits:
Grape::Middleware::Auth::Base
  • Object
show all
Defined in:
lib/simple_jwt_auth/middleware/grape/jwt.rb

Constant Summary collapse

ENV_AUTH_KEY =
'HTTP_AUTHORIZATION'
ENV_PAYLOAD_KEY =
'grape_jwt.payload'

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_jwt_auth/middleware/grape/jwt.rb', line 10

def call(env)
  return app.call(env) if test_env?(env)

  token = env.fetch(ENV_AUTH_KEY, '').split.last

  begin
    payload, _header = SimpleJwtAuth::Decode.new(token).call
    env[ENV_PAYLOAD_KEY] = payload

    logger.debug "Authorized request, JWT payload: #{payload}"

    app.call(env)
  rescue JWT::DecodeError => e
    logger.warn "Unauthorized request, JWT error: #{e.message}"

    [401, { 'Content-Type' => 'application/json' }, [{ status: 401, error: e.message }.to_json]]
  end
end