-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoauth2_routes.py
More file actions
executable file
·318 lines (275 loc) · 13.3 KB
/
oauth2_routes.py
File metadata and controls
executable file
·318 lines (275 loc) · 13.3 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
# -*- coding: utf-8 -*
import sys
from random import randint
from datetime import date
from flask import request, session, current_app
from flask import render_template, redirect, jsonify, json, abort
from flask_restful import Resource
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from werkzeug.security import gen_salt
from authlib.flask.oauth2 import current_token
from authlib.specs.rfc6749 import OAuth2Error
from zenroom import zenroom
import requests
from apps.backend.api.v0.models import db, User, OAuth2Client, OAuth2Token, Community
from apps.backend.api.v0.oauth2 import authorization, require_oauth
from apps.backend.api.v0.iot_login import IoTWalletLoginManager
from werkzeug.datastructures import ImmutableMultiDict
from apps.backend.api.v0.token_manager import TokenManager
from config.config import Config
from base64 import b64encode
__author__ = 'Jordi Allué'
__version__ = (0, 0, 1)
cfg = Config().get()
class OAuthManager(Resource):
def get(self, source):
if source == 'user_info':
return self.current_user()
elif source == 'iot_login':
return self.login_with_iot()
elif source == 'check_login':
return self.check_login()
else:
return "Invalid!!"
def post(self, source):
if source == 'login':
return self.issue_token()
elif source == 'iot_login_callback':
return self.login_with_iot_callback()
else:
return "Invalid!!"
@require_oauth('profile')
def current_user(self):
user = self.api_me()
return user
@staticmethod
def home():
if request.method == 'POST':
data = request.json
username = data['username']
user = User.query.filter_by(username=username).first()
if not user:
user = User(username=username)
db.session.add(user)
db.session.commit()
session['id'] = user.id
return redirect('/')
user = OAuthManager.current_user()
if user:
clients = OAuth2Client.query.filter_by(user_id=user.id).all()
else:
clients = []
# response = authorization.create_token_response() #render_template('home.html', user=user, clients=clients)
return "Success"
@staticmethod
def logout():
del session['id']
return redirect('/')
@staticmethod
def create_client():
# user = OAuthManager.current_user()
# if not user:
# return redirect('/')
# if request.method == 'GET':
# return render_template('create_client.html')
client = OAuth2Client()
client.user_id = 1
client.client_id = gen_salt(24)
if client.token_endpoint_auth_method == 'none':
client.client_secret = ''
else:
client.client_secret = gen_salt(48)
db.session.add(client)
db.session.commit()
return redirect('/')
@staticmethod
def authorize():
user = OAuthManager.current_user()
if request.method == 'GET':
try:
grant = authorization.validate_consent_request(end_user=user)
except OAuth2Error as error:
return error.error
return render_template('authorize.html', user=user, grant=grant)
data = request.json
username = data['username']
user = User.query.filter_by(username=username).first()
grant_user = user
response = authorization.create_authorization_response(grant_user=grant_user)
return response
@staticmethod
def issue_token():
try:
return authorization.create_token_response(request)
except Exception as e:
current_app.logger.error("Unexpected error:" + str(sys.exc_info()[0]))
current_app.logger.error("Error description: " + str(e))
return
@staticmethod
def revoke_token():
return authorization.create_endpoint_response('revocation')
@staticmethod
def login_with_iot():
# get the info of the user and the link from iotlogin library
iot_login_info = IoTWalletLoginManager.get_qrimg()
# create user for oit session
User.crate_user(iot_login_info['session'], 'iot')
return {"session": iot_login_info['session'],
"dddc_qr": iot_login_info['dddc_qr'],
"dddc_url": iot_login_info['dddc_url'],
"iot_qr": iot_login_info['iot_qr'],
"iot_url": iot_login_info['iot_url']}
@staticmethod
def login_with_iot_callback():
# username: AzrWLH8xw1xGYoPBBt1lP4xl
# password: V2CQt67jOXTpeV4BrDMumQOcka1HEpQmDWp72l1mnutz52j8
data = request.json
session_token = data['sessionId']
districts = {
"1": "Ciutat Vella",
"2": "Eixample",
"3": "Sants-Montjuïc",
"4": "Les Corts",
"5": "Sarrià-Sant Gervasi",
"6": "Gràcia",
"7": "Horta-Guinardó",
"8": "Nou Barris",
"9": "Sant Andreu",
"10": "Sant Martí",
}
try:
contract = """Scenario coconut: verify proof
Given that I have a valid 'verifier' from 'issuer_identifier'
and I have a valid 'credential proof'
When I aggregate the verifiers
and I verify the credential proof
Then print 'Success' 'OK' as 'string'
"""
current_app.logger.info("starting callback")
authorizable_attribute_id = data['credential']['authorizable_attribute_id']
current_app.logger.info("authorizable_attribute_id: " + authorizable_attribute_id)
credential_issuer_endpoint_address = data['credential']['credential_issuer_endpoint_address']
current_app.logger.info("credential_issuer_endpoint_address: " + credential_issuer_endpoint_address)
# read the public key from endpoint
bcn_community_obj = Community.get_from_authorizable_attribute_id(authorizable_attribute_id)
# print("bcn_community_obj: " + bcn_community_obj)
current_app.logger.info("URL: " + credential_issuer_endpoint_address + "/authorizable_attribute/{}".format(
authorizable_attribute_id))
res = requests.get(
credential_issuer_endpoint_address + "/authorizable_attribute/{}".format(authorizable_attribute_id))
if res.ok:
credential_key = json.dumps(res.json()["verification_key"])
value = json.dumps(data['credential']['value'])
## check with zenroom if login is valid
verify_response_msg = "OK"
current_app.logger.info("\tvalue: {}".format(value))
current_app.logger.info("\tAll good, got this result: {}".format(res.json()))
if cfg['iotconfig']['bypass'] == 'no':
with open('verifyer.zencode') as file:
verify_credential_script = file.read()
try:
verify_response = zenroom.zencode_exec(contract, data=credential_key, keys=value.replace('"proof"','"credential_proof"'))
verify_response_stdout = verify_response.stdout
print("response: " + verify_response_stdout)
if(verify_response_stdout.find("OK")!=-1):
verify_response_msg="OK"
else:
verify_response_msg="not OK"
except Exception as e:
print("Error in zenroom")
if verify_response_msg == "OK":
tkn_manager = TokenManager()
tkn_status = tkn_manager.validate_token(session_token)
print("token status "+tkn_status)
if tkn_status == '1':
# login
print("valid token creating auth")
request.headers.environ['HTTP_AUTHORIZATION'] = \
'Basic ' + b64encode(bytes(cfg['oauth']['client_username'] + ':'
+ cfg['oauth']['client_password'], 'utf-8')).decode('utf-8')
data2 = ImmutableMultiDict([('grant_type', 'password'), ('username', session_token),
('scope', 'profile'), ('password', 'dummy')])
request.form = data2
print("request data created.")
# Get personal data
name = ""
city = "Barcelona"
age = ""
area = ""
profile_data_array = data['optionalAttributes']
for profile_data in profile_data_array:
if profile_data['attribute_id'] == "schema:dateOfBirth":
# process age dd/mm/yyyy
day, month, year = profile_data['value'].split('/')
today = date.today()
age = today.year - int(year) - ((today.month, today.day) < (int(month), int(day)))
if profile_data['attribute_id'] == "schema:name":
name = profile_data['value']
if profile_data['attribute_id'] == "schema:city":
city = profile_data['value']
if profile_data['attribute_id'] == "schema:district":
if profile_data['value'] in districts:
area = districts[profile_data['value']]
User.update_user(session_token, name, city, age, area)
print("User updated")
User.user_add_community(session_token, bcn_community_obj.id)
print("User added to community")
headers = {'Authorization': 'Basic QXpyV0xIOHh3MXhHWW9QQkJ0MWxQNHhsOlYyQ1F0NjdqT1hUcGVWNEJyRE11bVFPY2thMUhFcFFtRFdwNzJsMW1udXR6NTJqOA=='} # + b64encode(bytes(cfg['oauth']['client_username'] + ':' + cfg['oauth']['client_password'], 'utf-8')).decode('utf-8')}
PARAMS = {'grant_type': 'password', 'username': session_token, 'scope': 'profile', 'password': 'dummy'}
r = requests.post(url='http://84.88.76.45:887/oauth/login', params=PARAMS, headers=headers)
data = r.json()
response = jsonify(message="Logged OK")
response.status_code = 200
return response
# token = authorization.create_token_response(request)
# print("token created")
# return token
else:
response = jsonify(message="Invalid Token")
response.status_code = 401
return response
else:
response = jsonify(message="Invalid Credentials")
response.status_code = 401
return response
else:
current_app.logger.info("\tCalls not getting back, got this error: {}".format(res.json()))
response = jsonify(message="Could not get public key data from credential_issuer_endpoint_address")
response.status_code = 412
return response
except Exception as e:
current_app.logger.error("Unexpected error:" + sys.exc_info()[0])
current_app.logger.error("Error description: " + e)
response = jsonify(message="Unexpected Error in Validation")
response.status_code = 412
return response
@staticmethod
def check_login():
data = request.args
username = data['session']
user = User.query.filter_by(username=username).one()
# given a username check if it has a token created for him
try:
token = OAuth2Token.query.filter_by(user_id=user.id).one()
return {"status": True,
"access_token": token.access_token,
"expires_in": token.expires_in,
"refresh_token": token.refresh_token,
"scope": token.scope,
"token_type": token.token_type}
except MultipleResultsFound as e:
message = format(e)
current_app.logger.error("Error description: " + message)
return {"status": False, "message": message}
except NoResultFound as e:
message = format(e)
current_app.logger.error("Error description: " + message)
return {"status": False, "message": message}
@staticmethod
def get_current_user():
return current_token.user
def api_me(self):
user = self.get_current_user()
return jsonify(id=user.id, username=user.username, name=user.profile_name, city=user.profile_city,
age=user.profile_age, area=user.profile_area, community=user.profile_community)