Skip to content

Instantly share code, notes, and snippets.

@nycdavid
Created August 10, 2015 16:52
Show Gist options
  • Select an option

  • Save nycdavid/cd6723a483f458b50fa4 to your computer and use it in GitHub Desktop.

Select an option

Save nycdavid/cd6723a483f458b50fa4 to your computer and use it in GitHub Desktop.
Example jwt auth middleware
require "pry"
class JwtAuthentication
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
secret = ";Xsc2}M48w33xUvL7N>SG-V}OnU??BG&O&|!az,)yHpLgm/|g,kj^iVNwr<SFqy"
token = req.env["HTTP_TOKEN"]
status, headers, response = @app.call(env)
if JWT.decode token, secret
[200, headers, response.body]
end
rescue JWT::DecodeError
message = { message: "Invalid token" }.to_json
[403, { "Content-Type" => "application/json" }, [message]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment