class Rack::JWTAuthMiddleware

Public Class Methods

new(app, &decoder) click to toggle source
# File lib/rack-jwt-token-auth.rb, line 5
def initialize(app, &decoder)
  @app, @decoder = app, decoder
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack-jwt-token-auth.rb, line 9
def call(env)
  begin
    if env['HTTP_AUTHORIZATION']
      token = env['HTTP_AUTHORIZATION'].match(/JWT token="(.+)"/)[1]
      env['user'] = @decoder.call(token)
    end
  rescue JWT::DecodeError => error
    body = {message: error.message}.to_json

    headers = {
      'Content-Type' => 'application/json',
      'Content-Length' => body.bytesize.to_s
    }

    return [401, headers, [body]]
  end

  @app.call(env)
end