-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (38 loc) · 1.46 KB
/
Copy pathapp.py
File metadata and controls
51 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Flask, jsonify
from flask_restful import Api
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from marshmallow import ValidationError
from db import db
from ma import ma
from blacklist import BLACKLIST
from resources.user import UserRegister, UserLogin, TokenRefresh, UserLogout
app = Flask(__name__)
CORS(app, resources=r'/api/*')
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["PROPAGATE_EXCEPTIONS"] = True
app.config["JWT_BLACKLIST_ENABLED"] = True # enable blacklist feature
app.config["JWT_BLACKLIST_TOKEN_CHECKS"] = [
"access",
"refresh",
] # allow blacklisting for access and refresh tokens
app.secret_key = "jose" # could do app.config['JWT_SECRET_KEY'] if we prefer
api = Api(app, prefix='/api')
@app.before_first_request
def create_tables():
db.create_all()
@app.errorhandler(ValidationError)
def handle_marshmallow_validation(err):
return jsonify({"errors": err.messages}), 400
jwt = JWTManager(app)
# This method will check if a token is blacklisted, and will be called automatically when blacklist is enabled
@jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
return decrypted_token["jti"] in BLACKLIST
api.add_resource(UserRegister, "/users")
api.add_resource(UserLogin, "/auth")
api.add_resource(TokenRefresh, "/refresh")
api.add_resource(UserLogout, "/logout")
db.init_app(app)
ma.init_app(app)