-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
184 lines (143 loc) · 7.18 KB
/
authentication.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from flask import Flask, request, make_response, abort, jsonify
from authenticators.basic_authentication import BasicAuthentication
from authenticators.token_authentication import TokenAuthentication
from validators.token_validation import TokenValidator
from validators.external_token_validation import ExternalTokenValidator
from revocators.token_revocation import TokenRevocator
from user import User
from settings import Config
app = Flask(__name__)
@app.route('/auth', methods=['GET'])
def auth_get():
"""Method to validate tokens and get information"""
serv_token = request.headers.get('X-Service-Token')
subj_token = request.headers.get('X-Subject-Token')
ext_token = request.headers.get('X-External-Token')
if serv_token is None:
abort(400, description='service token must be provided')
service = TokenValidator().validate_token(serv_token)
if service is None or service.is_service is False:
abort(400, description='validation must be done by service user')
subj = TokenValidator().validate_token(subj_token)
if subj is None:
abort(404, description='subject token not found')
if Config.EXTERNAL_MAPPING_VERIFICATION is True and ext_token is not None:
ext = ExternalTokenValidator().validate_token(ext_token)
if ext is None or ext.is_authenticated is False:
abort(403, description='external token not authenticated')
return make_response(jsonify(subj.to_public_dict()), 200)
@app.route('/auth', methods=['POST'])
def auth_post():
"""Method to authenticate users (using credentials or tokens)"""
req_data = request.get_json()
auth_method = req_data.get('method')
subj_token = req_data.get('token')
ext_token = req_data.get('external_token')
auth_user = req_data.get('username')
auth_pass = req_data.get('password')
serv_token = request.headers.get('X-Service-Token')
# Output
resp_data = None
gen_token = None
if auth_method == 'password':
if auth_user is not None and auth_pass is not None:
user = BasicAuthentication().authenticate(username=auth_user, password=auth_pass)
if user is None:
abort(404, description='user not found')
if user.is_service is True:
gen_token = user.token
resp_data = user.to_public_dict()
elif serv_token is not None:
service = TokenValidator().validate_token(serv_token)
if service is None or service.is_service is False:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must be done by service user')
else:
if Config.EXTERNAL_MAPPING_VERIFICATION is True and ext_token is not None:
external = ExternalTokenValidator().validate_token(ext_token)
if external.is_authenticated is True:
gen_token = user.token
resp_data = user.to_public_dict()
else:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must match external service')
else:
gen_token = user.token
resp_data = user.to_public_dict()
else:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must be done by service user')
elif auth_user is None:
abort(400, description='user must be provided')
else:
abort(400, description='password must be provided')
elif auth_method == 'token':
if subj_token is not None:
user = TokenAuthentication().authenticate(token=subj_token)
if user is None:
abort(400, description='token not found')
if user.is_service is True:
gen_token = user.token
resp_data = user.to_public_dict()
elif serv_token is not None:
service = TokenValidator().validate_token(serv_token)
if service is None or service.is_service is False:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must be done by service user')
else:
if Config.EXTERNAL_MAPPING_VERIFICATION is True:
external = ExternalTokenValidator().validate_token(ext_token)
if external.is_authenticated is True:
gen_token = user.token
resp_data = user.to_public_dict()
else:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must match external service')
else:
gen_token = user.token
resp_data = user.to_public_dict()
else:
TokenRevocator().revoke_token(user.token)
abort(403, description='user authentication must be done by service user')
else:
abort(400, description='token must be provided')
else:
abort(400, description='method must be password or token')
resp = make_response(jsonify(resp_data), 201)
resp.headers['X-Subject-Token'] = gen_token
return resp
@app.route('/auth', methods=['HEAD'])
def auth_head():
"""Method to validate tokens"""
serv_token = request.headers.get('X-Service-Token')
subj_token = request.headers.get('X-Subject-Token')
ext_token = request.headers.get('X-External-Token')
if serv_token is None:
abort(400, description='service token must be provided')
service = TokenValidator().validate_token(serv_token)
if service is None or service.is_service is False:
abort(400, description='validation must be done by service user')
subj = TokenValidator().validate_token(subj_token)
if subj is None:
abort(404, description='subject token not found')
if Config.EXTERNAL_MAPPING_VERIFICATION is True and ext_token is not None:
ext = ExternalTokenValidator().validate_token(ext_token)
if ext is None or ext.is_authenticated is False:
abort(403, description='external token not authenticated')
return make_response('', 200)
@app.route('/auth', methods=['DELETE'])
def auth_delete():
"""Method to revoke tokens"""
serv_token = request.headers.get('X-Service-Token')
subj_token = request.headers.get('X-Subject-Token')
if serv_token is None:
abort(400, description='service token must be provided')
service = TokenValidator().validate_token(serv_token)
if service is None or service.is_service is False:
abort(400, description='revoking must be done by service user')
if subj_token is None:
abort(400, description='subject token must be provided')
if TokenRevocator().revoke_token(subj_token):
return make_response('token revoked', 204)
else:
abort(400, description='token could not be revoked')