Skip to content

Commit b14d593

Browse files
authored
Merge pull request #16 from schoolofcode-me/add_user_logout_using_blacklist
merged
2 parents 5fdb340 + 508c7dd commit b14d593

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

section11/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from flask_jwt_extended import JWTManager
44

55
from db import db
6-
from resources.user import UserRegister, UserLogin, User, TokenRefresh
6+
from blacklist import BLACKLIST
7+
from resources.user import UserRegister, UserLogin, User, TokenRefresh, UserLogout
78
from resources.item import Item, ItemList
89
from resources.store import Store, StoreList
910

@@ -35,13 +36,10 @@ def add_claims_to_jwt(identity):
3536
return {'is_admin': False}
3637

3738

38-
black_list = [4, 6] # user.id that are black listed (can be read from a file or db too)
39-
40-
4139
# This method will check if a token is blacklisted, and will be called automatically when blacklist is enabled
4240
@jwt.token_in_blacklist_loader
4341
def check_if_token_in_blacklist(decrypted_token):
44-
return decrypted_token['identity'] in black_list
42+
return decrypted_token['jti'] in BLACKLIST
4543

4644

4745
# The following callbacks are used for customizing jwt response/error messages.
@@ -101,6 +99,7 @@ def create_tables():
10199
api.add_resource(UserLogin, '/login')
102100
api.add_resource(User, '/user/<int:user_id>')
103101
api.add_resource(TokenRefresh, '/refresh')
102+
api.add_resource(UserLogout, '/logout')
104103

105104
if __name__ == '__main__':
106105
db.init_app(app)

section11/blacklist.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
blacklist.py
3+
4+
This file just contains the blacklist of the JWT tokens–it will be imported by
5+
app and the logout resource so that tokens can be added to the blacklist when the
6+
user logs out.
7+
"""
8+
9+
BLACKLIST = set()

section11/resources/user.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
from flask_restful import Resource, reqparse
22
from werkzeug.security import safe_str_cmp
3-
from flask_jwt_extended import create_access_token, create_refresh_token, jwt_refresh_token_required, get_jwt_identity
3+
from flask_jwt_extended import (
4+
create_access_token,
5+
create_refresh_token,
6+
jwt_refresh_token_required,
7+
get_jwt_identity,
8+
get_raw_jwt,
9+
jwt_required
10+
)
411
from models.user import UserModel
12+
from blacklist import BLACKLIST
513

14+
_user_parser = reqparse.RequestParser()
15+
_user_parser.add_argument('username',
16+
type=str,
17+
required=True,
18+
help="This field cannot be blank."
19+
)
20+
_user_parser.add_argument('password',
21+
type=str,
22+
required=True,
23+
help="This field cannot be blank."
24+
)
625

7-
class UserRegister(Resource):
8-
parser = reqparse.RequestParser()
9-
parser.add_argument('username',
10-
type=str,
11-
required=True,
12-
help="This field cannot be blank."
13-
)
14-
parser.add_argument('password',
15-
type=str,
16-
required=True,
17-
help="This field cannot be blank."
18-
)
1926

27+
class UserRegister(Resource):
2028
def post(self):
21-
data = self.parser.parse_args()
29+
data = _user_parser.parse_args()
2230

2331
if UserModel.find_by_username(data['username']):
2432
return {"message": "A user with that username already exists"}, 400
@@ -30,20 +38,8 @@ def post(self):
3038

3139

3240
class UserLogin(Resource):
33-
parser = reqparse.RequestParser()
34-
parser.add_argument('username',
35-
type=str,
36-
required=True,
37-
help="This field cannot be blank."
38-
)
39-
parser.add_argument('password',
40-
type=str,
41-
required=True,
42-
help="This field cannot be blank."
43-
)
44-
4541
def post(self):
46-
data = self.parser.parse_args()
42+
data = _user_parser.parse_args()
4743

4844
user = UserModel.find_by_username(data['username'])
4945

@@ -58,6 +54,14 @@ def post(self):
5854
return {"message": "Invalid Credentials!"}, 401
5955

6056

57+
class UserLogout(Resource):
58+
@jwt_required
59+
def post(self):
60+
jti = get_raw_jwt()['jti']
61+
BLACKLIST.add(jti)
62+
return {"message": "Successfully logged out"}, 200
63+
64+
6165
class User(Resource):
6266
"""
6367
This resource can be useful when testing our Flask app. We may not want to expose it to public users, but for the

0 commit comments

Comments
 (0)