Skip to content

Commit 6ce4c12

Browse files
committed
Removed old test code from jwt_ops
1 parent 49eaa7f commit 6ce4c12

File tree

2 files changed

+6
-51
lines changed

2 files changed

+6
-51
lines changed

src/server/jwt_ops.py

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,22 @@ def wrapper(*args, **kwargs):
2525
return wrapper
2626

2727

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-
5928
@jwt.user_claims_loader
6029
def add_claims_to_access_token(accesslevel):
30+
""" Adds role k/v to token """
6131
return {"role": accesslevel}
6232

6333

6434
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+
"""
6638
# Identity can be any data that is json serializable
6739
new_token = create_access_token(identity=username)
6840
# add_claims_to_access_token(accesslevel)
6941
return jsonify(access_token=new_token)
7042

7143

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-
9044
def get_jwt_user():
45+
""" Read the JWT and return the associated username """
9146
return get_jwt_identity()

src/server/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sqlalchemy
66
sqlalchemy_utils
77
psycopg2-binary==2.8.4
88
python-Levenshtein-wheels
9-
xlrd==1.2.0 # currently used for xlsx, but we should consider adjusting code to openpyxl for xlsx
9+
xlrd==1.2.0 # currently used for xlsx, but we should consider adjusting code to openpyxl for xlsx
1010
openpyxl
1111
requests
1212
pytest

0 commit comments

Comments
 (0)