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, = 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
|