API Documentation¶
In here you will find the API for everything exposed in this extension.
Configuring JWT Options¶
-
class
flask_jwt_simple.
JWTManager
(app=None)[source]¶ This object is used to hold the JWT settings and callback functions. Instances
JWTManager
are not bound to specific apps, so you can create one in the main body of your code and then bind it to your app in a factory function.-
__init__
(app=None)[source]¶ Create the JWTManager instance. You can either pass a flask application in directly here to register this extension with the flask app, or call init_app after creating this object
Parameters: app – A flask application
-
init_app
(app)[source]¶ Register this extension with the flask app
Parameters: app – A flask application
-
expired_token_loader
(callback)[source]¶ Sets the callback method to be called if an expired JWT is received
The default implementation will return json ‘{“msg”: “Token has expired”}’ with a 401 status code.
Callback must be a function that takes zero arguments.
-
invalid_token_loader
(callback)[source]¶ Sets the callback method to be called if an invalid JWT is received.
The default implementation will return json ‘{“msg”: <err>}’ with a 401 status code.
Callback must be a function that takes only one argument, which is the error message of why the token is invalid.
Sets the callback method to be called if no JWT is received
The default implementation will return ‘{“msg”: “Missing Authorization Header”}’ json with a 401 status code.
Callback must be a function that takes only one argument, which is the error message of why the token is invalid.
-
jwt_data_loader
(callback)[source]¶ Sets the callback method to be called for what data should be included in a JWT (with the create_jwt() function).
The default implementation will return the following data.
{ 'exp': now + current_app.config['JWT_EXPIRES'], 'iat': now, 'nbf': now, 'sub': identity }
Callback must be a function that takes only one argument, which is the identity of the user this JWT is for.
-
Protected endpoint decorators¶
-
flask_jwt_simple.
jwt_required
(fn)[source]¶ If you decorate a view with this, it will ensure that the requester has a valid JWT before calling the actual view.
Parameters: fn – The view function to decorate
-
flask_jwt_simple.
jwt_optional
(fn)[source]¶ If you decorate a view with this, it will check the request for a valid JWT and put it into the Flask application context before calling the view. If no authorization header is present, the view will be called without the application context being changed. Other authentication errors are not affected. For example, if an expired JWT is passed in, it will still not be able to access an endpoint protected by this decorator.
Parameters: fn – The view function to decorate
Utilities¶
-
flask_jwt_simple.
get_jwt
()[source]¶ Returns the python dictionary which has all of the data in this JWT. If no JWT is currently present, an empty dict is returned
-
flask_jwt_simple.
get_jwt_identity
()[source]¶ Returns the identity of the JWT in this context. If no JWT is present, None is returned.