-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (102 loc) · 3.91 KB
/
Copy pathapp.py
File metadata and controls
141 lines (102 loc) · 3.91 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, jsonify
from flask_smorest import abort,Api
from flask_jwt_extended import JWTManager
from blocklist import BLOCKLIST
from db import db, check_if_database_exists
import models
from flask_migrate import Migrate
import os
from resources.user import blp as UserBlueprint
from resources.group import blp as GroupBlueprint
from resources.action import blp as ActionBlueprint
from resources.resource import blp as ResourceBlueprint
from resources.company import blp as CompanyBlueprint
from resources.application import blp as ApplicationpBlueprint
from resources.permissions import blp as PermissionsBlueprint
from resources.settings import blp as SettingsBlueprint
from dotenv import load_dotenv
from schemas import UserTypeSchema
def create_app(db_url=None):
usertype =UserTypeSchema()
usertype.name = "Admin"
author_result = UserTypeSchema().dump(usertype)
print(author_result)
app=Flask(__name__)
load_dotenv()
app.config["PROPAGATE_EXCEPTIONS"] = True
app.config["API_TITLE"] = "Security REST API"
app.config["API_VERSION"] = "v1"
app.config["OPENAPI_VERSION"] = "3.0.3"
app.config["OPENAPI_URL_PREFIX"] = "/"
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
app.config["OPENAPI_SWAGGER_UI_URL"] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
app.config["SQLALCHEMY_DATABASE_URI"] = db_url or os.getenv("DATABASE_URL")
#app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#app.config['SQLALCHEMY_RECORD_QUERIES'] = True
check_if_database_exists(os.getenv("DATABASE_URL"))
db.init_app(app)
with app.app_context():
db.create_all()
migrate = Migrate(app, db)
api = Api(app)
#This has to be one time in the app
app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY")
jwt = JWTManager(app)
@jwt.additional_claims_loader
def add_claims_to_jwt(identity):
# TODO: Read from a config file instead of hard-coding
return {"identity": identity}
@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({"message": "The token has expired.", "status": "token_expired","code":401}), 401
@jwt.invalid_token_loader
def invalid_token_callback(error):
return (
jsonify(
{"message": "Signature verification failedc.", "status": "invalid_token","code":401}
),
401,
)
@jwt.unauthorized_loader
def missing_token_callback(error):
return (
jsonify(
{
"message": "Request does not contain an access token.",
"status": "authorization_required",
"code":401
}
),
401,
)
@jwt.needs_fresh_token_loader
def token_not_fresh_callback(jwt_header, jwt_payload):
return (
jsonify(
{"message": "The token is not fresh.", "status": "fresh_token_required","code":401}
),
401,
)
@jwt.revoked_token_loader
def revoked_token_callback(jwt_header, jwt_payload):
return (
jsonify(
{"message": "The token has been revoked.", "status": "token_revoked","code":401}
),
401,
)
@app.get("/")
def HelloName():
return "Welcome to the API"
api.register_blueprint(UserBlueprint)
api.register_blueprint(GroupBlueprint)
api.register_blueprint(ActionBlueprint)
api.register_blueprint(ResourceBlueprint)
api.register_blueprint(CompanyBlueprint)
api.register_blueprint(ApplicationpBlueprint)
api.register_blueprint(PermissionsBlueprint)
api.register_blueprint(SettingsBlueprint)
return app