@@ -25,67 +25,22 @@ def wrapper(*args, **kwargs):
25
25
return wrapper
26
26
27
27
28
- # Provide a method to create access tokens. The create_access_token()
29
- # function is used to actually generate the token, and you can return
30
- # it to the caller however you choose.
31
- @app .route ("/login" , methods = ["POST" ])
32
- def login ():
33
- if not request .is_json :
34
- return jsonify ({"msg" : "Missing JSON in request" }), 400
35
-
36
- username = request .json .get ("username" , None )
37
- password = request .json .get ("password" , None )
38
- if not username :
39
- return jsonify ({"msg" : "Missing username parameter" }), 400
40
- if not password :
41
- return jsonify ({"msg" : "Missing password parameter" }), 400
42
-
43
- if username == "admin" and password == "admin" :
44
- accesslevel = "admin"
45
- elif username == "test" and password == "test" :
46
- accesslevel = "user"
47
- else :
48
- return jsonify ({"msg" : "Bad username or password" }), 401
49
-
50
- @jwt .user_claims_loader
51
- def add_claims_to_access_token (identity ):
52
- return {"role" : accesslevel }
53
-
54
- # Identity can be any data that is json serializable
55
- access_token = create_access_token (identity = username )
56
- return jsonify (access_token = access_token ), 200
57
-
58
-
59
28
@jwt .user_claims_loader
60
29
def add_claims_to_access_token (accesslevel ):
30
+ """ Adds role k/v to token """
61
31
return {"role" : accesslevel }
62
32
63
33
64
34
def create_token (username , accesslevel ):
65
-
35
+ """ Create a JWT *access* token for the specified user and role.
36
+ Role is magically added by the user_claims_loader decorator
37
+ """
66
38
# Identity can be any data that is json serializable
67
39
new_token = create_access_token (identity = username )
68
40
# add_claims_to_access_token(accesslevel)
69
41
return jsonify (access_token = new_token )
70
42
71
43
72
- # Protect a view with jwt_required, which requires a valid access token
73
- # in the request to access.
74
- @app .route ("/protected" , methods = ["GET" ])
75
- @jwt_required
76
- def protected ():
77
- # Access the identity of the current user with get_jwt_identity
78
- current_user = get_jwt_identity ()
79
- return jsonify (logged_in_as = current_user ), 200
80
-
81
-
82
- @app .route ("/admin" , methods = ["GET" ])
83
- @admin_required
84
- def admin_func ():
85
- # Access the identity of the current user with get_jwt_identity
86
- current_user = get_jwt_identity ()
87
- return jsonify (logged_in_as = current_user ), 200
88
-
89
-
90
44
def get_jwt_user ():
45
+ """ Read the JWT and return the associated username """
91
46
return get_jwt_identity ()
0 commit comments