-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
120 lines (93 loc) · 3.67 KB
/
app.py
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
from flask import Flask, jsonify
from flask_restful import Api
from flask_jwt_extended import JWTManager
from apipackage.db import db
from apipackage.blacklist import BLACKLIST
from apipackage.resources.user import UserRegister, UserLogin, User, TokenRefresh, UserLogout,UserList,UserMakeAdmin,UserRemoveAdmin
from utils import urls as utilsimports
from apipackage.models.user import UserModel
from apipackage.resources.imageanalysis import FaceRecognitionResource
from apipackage.resources.textanalysis import TextAnalysisResource
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = utilsimports.sqliteDatabse
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['PROPAGATE_EXCEPTIONS'] = True
api = Api(app)
"""
JWT related configuration. The following functions includes:
1) add claims to each jwt
2) customize the token expired error message
"""
app.config['JWT_SECRET_KEY'] = 'future_ai_systems'
app.config['JWT_BLACKLIST_ENABLED'] = True # enable blacklist feature
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh'] # allow blacklisting for access and refresh tokens
jwt = JWTManager(app)
"""
`claims` are data we choose to attach to each jwt payload
and for each jwt protected endpoint, we can retrieve these claims via `get_jwt_claims()`
one possible use case for claims are access level control, which is shown below
"""
@jwt.user_claims_loader
def add_claims_to_jwt(identity):
user = UserModel.check_if_admin(identity)
if user:
return {'is_admin': True}
return {'is_admin': False}
# 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
# The following callbacks are used for customizing jwt response/error messages.
# The original ones may not be in a very pretty format (opinionated)
@jwt.expired_token_loader
def expired_token_callback():
return jsonify({
'message': 'The token has expired.',
'error': 'token_expired'
}), 401
@jwt.invalid_token_loader
def invalid_token_callback(error): # we have to keep the argument here, since it's passed in by the caller internally
return jsonify({
'message': 'Signature verification failed.',
'error': 'invalid_token'
}), 401
@jwt.unauthorized_loader
def missing_token_callback(error):
return jsonify({
"description": "Request does not contain an access token.",
'error': 'authorization_required'
}), 401
@jwt.needs_fresh_token_loader
def token_not_fresh_callback():
return jsonify({
"description": "The token is not fresh.",
'error': 'fresh_token_required'
}), 401
@jwt.revoked_token_loader
def revoked_token_callback():
return jsonify({
"description": "The token has been revoked.",
'error': 'token_revoked'
}), 401
# JWT configuration ends
@app.before_first_request
def create_tables():
db.create_all()
# Api resources below
# Reg and Auth
api.add_resource(UserRegister, '/register')
api.add_resource(UserLogin, '/login')
api.add_resource(TokenRefresh, '/refresh')
api.add_resource(UserLogout, '/logout')
# User Manipulation Api's
api.add_resource(UserList, '/users')
api.add_resource(User, '/user/<string:email>')
api.add_resource(UserMakeAdmin, '/user_privilegedMakeAdmin/<string:email>')
api.add_resource(UserRemoveAdmin, '/user_privilegedRemoveAdmin/<string:email>')
# Image Analysis Api's
api.add_resource(FaceRecognitionResource, '/facerecognition') #Face Recognition Api
# Text Analysis Api's
api.add_resource(TextAnalysisResource, '/textanalysis')
db.init_app(app)
if __name__ == '__main__':
app.run()