Skip to content

Commit b008758

Browse files
authored
Merge pull request #15 from schoolofcode-me/fix_requirements
merged
2 parents f91f64e + 40ce6aa commit b008758

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.vscode/
12
*.pyc
23
.idea/
34
__pycache__/

section11/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tips:
8383
Introduce the concept of `claims`, it's just the data we choose to attach to the JWT payload. Use the `Item.delete()` endpoint as example, we make it only accessible by authenticated admins. So we need to configure the claims in `app.py` and decide whether a user is an admin, then we add a boolean claim `is_admin` to the JWT payload.
8484

8585
tips:
86-
- `get_jwt_oidentity()` now as opposed to `current_identity`
86+
- `get_jwt_identity()` now as opposed to `current_identity`
8787
- The identity is just the user id now as opposed to a UserModel object.
8888

8989
### Half protected endpoints

section11/app.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@
1010
app = Flask(__name__)
1111
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
1212
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
13+
app.config['PROPAGATE_EXCEPTIONS'] = True
1314
api = Api(app)
14-
db.init_app(app)
1515

1616
"""
17-
JWT related configurations began. The following functions includes:
17+
JWT related configuration. The following functions includes:
1818
1) add claims to each jwt
1919
2) customize the token expired error message
2020
"""
21-
app.config['JWT_SECRET_KEY'] = 'jose' # we can also use app.secret like before, Flask-JWT-Extended can recognize both
21+
app.config['JWT_SECRET_KEY'] = 'jose' # we can also use app.secret like before, Flask-JWT-Extended can recognize both
2222
app.config['JWT_BLACKLIST_ENABLED'] = True # enable blacklist feature
2323
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh'] # allow blacklisting for access and refresh tokens
2424
jwt = JWTManager(app)
2525

2626
"""
27-
`claims` are data we choose to attached to each jwt payload
27+
`claims` are data we choose to attach to each jwt payload
2828
and for each jwt protected endpoint, we can retrieve these claims via `get_jwt_claims()`
2929
one possible use case for claims are access level control, which is shown below
3030
"""
31-
32-
3331
@jwt.user_claims_loader
3432
def add_claims_to_jwt(identity):
35-
if identity == 1: # instead of hard-coding, we can read from a config file to get a list of admins instead
33+
if identity == 1: # instead of hard-coding, we should read from a config file to get a list of admins instead
3634
return {'is_admin': True}
3735
return {'is_admin': False}
3836

@@ -104,4 +102,5 @@ def create_tables():
104102
api.add_resource(TokenRefresh, '/refresh')
105103

106104
if __name__ == '__main__':
105+
db.init_app(app)
107106
app.run(port=5000, debug=True)

section11/models/user.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ def __init__(self, username, password):
1212
self.username = username
1313
self.password = password
1414

15-
def json(self):
16-
return {
17-
'id': self.id,
18-
'username': self.username
19-
}
20-
2115
def save_to_db(self):
2216
db.session.add(self)
2317
db.session.commit()

section11/requirement.txt

-108 Bytes
Binary file not shown.

section11/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask-JWT-Extended
2+
Flask-RESTful
3+
Flask-SQLAlchemy

section11/resources/item.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@ def get(self):
8080
items = [item.json() for item in ItemModel.find_all()]
8181
if user_id:
8282
return {'items': items}, 200
83-
return {'items': [item['name'] for item in items]}, 401
83+
return {
84+
'items': [item['name'] for item in items],
85+
'message': 'More data available if you log in.'
86+
}, 200

0 commit comments

Comments
 (0)