pallets-eco / flask-jwt

JWT (JSON Web Tokens) for Flask applications
MIT License
564 stars 177 forks source link

Custom authentication parameters / token based authentication? #91

Open mik-rom opened 8 years ago

mik-rom commented 8 years ago

In my app I want to be able to use just a custom login token (on app start) to authenticate and get a JWT token. Right now as far as I know a username and password is required, is there any way to change this?

stone7890 commented 8 years ago

you can use function jwt_encode_callback to get jwt token.

`def _default_jwt_encode_handler(identity): secret = current_app.config['JWT_SECRET_KEY'] algorithm = current_app.config['JWT_ALGORITHM'] required_claims = current_app.config['JWT_REQUIRED_CLAIMS']

payload = _jwt.jwt_payload_callback(identity)
missing_claims = list(set(required_claims) - set(payload.keys()))

if missing_claims:
    raise RuntimeError('Payload is missing required claims: %s' % ', '.join(missing_claims))

headers = _jwt.jwt_headers_callback(identity)

return jwt.encode(payload, secret, algorithm=algorithm, headers=headers)`