|
| 1 | +from flask import Flask, jsonify |
| 2 | +from flask_smorest import Api |
| 3 | +from flask_jwt_extended import JWTManager |
| 4 | + |
| 5 | +from db import db |
| 6 | +from blocklist import BLOCKLIST |
| 7 | + |
| 8 | +from resources.user import blp as UserBlueprint |
| 9 | +from resources.item import blp as ItemBlueprint |
| 10 | +from resources.store import blp as StoreBlueprint |
| 11 | +from resources.tag import blp as TagBlueprint |
| 12 | + |
| 13 | + |
| 14 | +def create_app(db_url=None): |
| 15 | + app = Flask(__name__) |
| 16 | + app.config["API_TITLE"] = "Stores REST API" |
| 17 | + app.config["API_VERSION"] = "v1" |
| 18 | + app.config["OPENAPI_VERSION"] = "3.0.3" |
| 19 | + app.config["OPENAPI_URL_PREFIX"] = "/" |
| 20 | + app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui" |
| 21 | + app.config[ |
| 22 | + "OPENAPI_SWAGGER_UI_URL" |
| 23 | + ] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" |
| 24 | + app.config["SQLALCHEMY_DATABASE_URI"] = db_url or "sqlite:///data.db" |
| 25 | + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False |
| 26 | + app.config["PROPAGATE_EXCEPTIONS"] = True |
| 27 | + db.init_app(app) |
| 28 | + api = Api(app) |
| 29 | + |
| 30 | + app.config["JWT_SECRET_KEY"] = "jose" |
| 31 | + jwt = JWTManager(app) |
| 32 | + |
| 33 | + # @jwt.additional_claims_loader |
| 34 | + # def add_claims_to_jwt(identity): |
| 35 | + # # TODO: Read from a config file instead of hard-coding |
| 36 | + # if identity == 1: |
| 37 | + # return {"is_admin": True} |
| 38 | + # return {"is_admin": False} |
| 39 | + |
| 40 | + @jwt.token_in_blocklist_loader |
| 41 | + def check_if_token_in_blocklist(jwt_header, jwt_payload): |
| 42 | + return jwt_payload["jti"] in BLOCKLIST |
| 43 | + |
| 44 | + @jwt.expired_token_loader |
| 45 | + def expired_token_callback(jwt_header, jwt_payload): |
| 46 | + return ( |
| 47 | + jsonify({"message": "The token has expired.", "error": "token_expired"}), |
| 48 | + 401, |
| 49 | + ) |
| 50 | + |
| 51 | + @jwt.invalid_token_loader |
| 52 | + def invalid_token_callback(error): |
| 53 | + return ( |
| 54 | + jsonify( |
| 55 | + {"message": "Signature verification failed.", "error": "invalid_token"} |
| 56 | + ), |
| 57 | + 401, |
| 58 | + ) |
| 59 | + |
| 60 | + @jwt.unauthorized_loader |
| 61 | + def missing_token_callback(error): |
| 62 | + return ( |
| 63 | + jsonify( |
| 64 | + { |
| 65 | + "description": "Request does not contain an access token.", |
| 66 | + "error": "authorization_required", |
| 67 | + } |
| 68 | + ), |
| 69 | + 401, |
| 70 | + ) |
| 71 | + |
| 72 | + @jwt.needs_fresh_token_loader |
| 73 | + def token_not_fresh_callback(jwt_header, jwt_payload): |
| 74 | + return ( |
| 75 | + jsonify( |
| 76 | + { |
| 77 | + "description": "The token is not fresh.", |
| 78 | + "error": "fresh_token_required", |
| 79 | + } |
| 80 | + ), |
| 81 | + 401, |
| 82 | + ) |
| 83 | + |
| 84 | + @jwt.revoked_token_loader |
| 85 | + def revoked_token_callback(jwt_header, jwt_payload): |
| 86 | + return ( |
| 87 | + jsonify( |
| 88 | + {"description": "The token has been revoked.", "error": "token_revoked"} |
| 89 | + ), |
| 90 | + 401, |
| 91 | + ) |
| 92 | + |
| 93 | + # JWT configuration ends |
| 94 | + |
| 95 | + with app.app_context(): |
| 96 | + import models # noqa: F401 |
| 97 | + |
| 98 | + db.create_all() |
| 99 | + |
| 100 | + api.register_blueprint(UserBlueprint) |
| 101 | + api.register_blueprint(ItemBlueprint) |
| 102 | + api.register_blueprint(StoreBlueprint) |
| 103 | + api.register_blueprint(TagBlueprint) |
| 104 | + |
| 105 | + return app |
0 commit comments