-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (51 loc) · 1.62 KB
/
app.py
File metadata and controls
66 lines (51 loc) · 1.62 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
52
53
54
55
56
57
58
59
60
61
62
63
import os
from flask import Flask, jsonify
from flask_smorest import Api
from flask_jwt_extended import JWTManager
from config import config_dict
from db import db
from blocklist import BLOCKLIST
from resources.store import blp as store_blp
from resources.item import blp as item_blp
from resources.user import blp as user_blp
from datetime import timedelta
def create_app(config=config_dict['dev']):
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
api = Api(app)
jwt = JWTManager(app)
@jwt.token_in_blocklist_loader
def check_if_token_in_blocklist(jwt_header, jwt_payload):
return jwt_payload['jti'] in BLOCKLIST
@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_payload):
return (
jsonify({
"description": "This token has expired",
"error": "token_expired"
}), 401
)
@jwt.invalid_token_loader
def invalid_token_callback(error):
return (
jsonify({
"description": "Signature verification failed",
"error": "invalid_token"
}), 401
)
@jwt.unauthorized_loader
def missing_token_callback(error):
return (
jsonify({
"desciption": "Request does not contain an access token",
"error": "authorization_required"
}), 401
)
@app.before_first_request
def create_tables():
db.create_all()
api.register_blueprint(store_blp)
api.register_blueprint(item_blp)
api.register_blueprint(user_blp)
return app