|
| 1 | +Basic Usage |
| 2 | +=========== |
| 3 | + |
| 4 | + |
| 5 | +In its simplest form, there is not much to using flask_jwt_extended. |
| 6 | + |
| 7 | + |
| 8 | +.. code-block :: python |
| 9 | +
|
| 10 | + from flask import Flask, jsonify, request |
| 11 | + from flask_jwt_extended import JWTManager, jwt_required, create_access_token |
| 12 | +
|
| 13 | + app = Flask(__name__) |
| 14 | + app.secret_key = 'super-secret' # Change this! |
| 15 | +
|
| 16 | + # Setup the Flask-JWT-Extended extension |
| 17 | + jwt = JWTManager(app) |
| 18 | +
|
| 19 | +
|
| 20 | + # Provide a method to create access tokens. The create_access_token() function |
| 21 | + # is used to actually generate the token |
| 22 | + @app.route('/login', methods=['POST']) |
| 23 | + def login(): |
| 24 | + username = request.json.get('username', None) |
| 25 | + password = request.json.get('password', None) |
| 26 | + if username != 'test' and password != 'test': |
| 27 | + return jsonify({"msg": "Bad username or password"}), 401 |
| 28 | +
|
| 29 | + ret = {'access_token': create_access_token(username)} |
| 30 | + return jsonify(ret), 200 |
| 31 | +
|
| 32 | +
|
| 33 | + # Protect a view with jwt_required, which requires a valid access token in the |
| 34 | + # request to access. |
| 35 | + @app.route('/protected', methods=['GET']) |
| 36 | + @jwt_required |
| 37 | + def protected(): |
| 38 | + return jsonify({'hello': 'world'}), 200 |
| 39 | +
|
| 40 | + if __name__ == '__main__': |
| 41 | + app.run() |
| 42 | +
|
| 43 | +
|
| 44 | +To access a jwt_required protected view, all we have to do is send an authorization head with the request that include the token. The header looks like this: |
| 45 | + |
| 46 | + |
| 47 | +.. code-block :: bash |
| 48 | +
|
| 49 | + Authorization: Bearer <access_token> |
| 50 | +
|
| 51 | +
|
| 52 | +We can see this in action using CURL: |
| 53 | + |
| 54 | +.. code-block :: bash |
| 55 | +
|
| 56 | + $ curl --write-out "%{http_code}\n" http://localhost:5000/protected |
| 57 | + { |
| 58 | + "msg": "Missing Authorization Header" |
| 59 | + } |
| 60 | + 401 |
| 61 | +
|
| 62 | + $ curl --write-out "%{http_code}\n" -H "Content-Type: application/json" -X POST -d '{"username":"test","password":"test"}' http://localhost:5000/login |
| 63 | + { |
| 64 | + "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc" |
| 65 | + } |
| 66 | + 200 |
| 67 | +
|
| 68 | + $ export ACCESS="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc" |
| 69 | +
|
| 70 | + $ curl --write-out "%{http_code}\n" -H "Authorization: Bearer $ACCESS" http://localhost:5000/protected |
| 71 | + { |
| 72 | + "hello": "world" |
| 73 | + } |
| 74 | + 200 |
0 commit comments