Skip to content

Validating a JWT token in Emmett #332

Answered by gi0baro
KellerKev asked this question in Q&A
Discussion options

You must be logged in to vote

I would use a Pipe (see https://emmett.sh/docs/2.2.x/pipeline) and put all the routes requiring such validation within a module (see https://emmett.sh/docs/2.2.x/app_and_modules#application-modules), like:

import jwt
from emmett import App, Pipe, request, response
from emmett.tools import ServicePipe

class JWTValidate(Pipe):
    def get_header_value(self):
        rv, err, header = None, None, request.headers.get("authorization", "")
        try:
            spec, rv = header.split()
            if spec.lower() != "bearer":
                err = "Authorization header must start with Bearer"
        except:
            err = "Authorization header must be Bearer + \s + token"
        return 

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by KellerKev
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants