Created
August 10, 2015 16:52
-
-
Save nycdavid/cd6723a483f458b50fa4 to your computer and use it in GitHub Desktop.
Example jwt auth middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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