-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaborators_utils.py
More file actions
378 lines (300 loc) · 12.5 KB
/
collaborators_utils.py
File metadata and controls
378 lines (300 loc) · 12.5 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from flask import Flask, request, jsonify
import json
import boto3
import google.auth.transport.requests
from google.oauth2 import id_token
from functools import wraps
import os
import csv
import io
from flask_cors import CORS
import datetime
application = Flask(__name__)
CORS(application)
# Configuration variables
FIREBASE_PROJECT_ID = os.environ.get("FIREBASE_PROJECT_ID", "collaborator-dir")
S3_SECRET_KEY = os.getenv("AWS_SECRET_KEY", None)
S3_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY", None)
COLLABORATORS_BUCKET = os.environ.get("COLLABORATORS_BUCKET", "collaborators-dir")
COLLABORATORS_KEY = os.environ.get("COLLABORATORS_KEY", "collaborators.csv")
ADMINS_BUCKET = os.environ.get("ADMINS_BUCKET", "collaborators-dir")
ADMINS_KEY = os.environ.get("ADMINS_KEY", "admins.csv")
def authenticate(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if request.method == "OPTIONS":
return _build_cors_preflight_response()
id_token_str = None
auth_header = request.headers.get("Authorization", None)
if auth_header and auth_header.startswith("Bearer "):
id_token_str = auth_header.split("Bearer ")[1]
else:
return jsonify({"message": "Unauthorized"}), 401
try:
request_adapter = google.auth.transport.requests.Request()
decoded_token = id_token.verify_firebase_token(
id_token_str, request_adapter, audience=FIREBASE_PROJECT_ID
)
print(decoded_token)
request.decoded_token = decoded_token
return f(*args, **kwargs)
except Exception as e:
print(f"Token verification failed: {e}")
return jsonify({"message": "Unauthorized"}), 401
return decorated_function
# Helper functions
def get_collaborators_data():
s3 = boto3.client(
"s3",
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name="us-east-1",
)
obj = s3.get_object(Bucket=COLLABORATORS_BUCKET, Key=COLLABORATORS_KEY)
data = obj["Body"].read().decode("utf-8").splitlines()
reader = csv.DictReader(data)
collaborators = list(reader)
return collaborators
def get_admins_list():
s3 = boto3.client(
"s3",
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name="us-east-1",
)
obj = s3.get_object(Bucket=ADMINS_BUCKET, Key=ADMINS_KEY)
data = obj["Body"].read().decode("utf-8").splitlines()
reader = csv.DictReader(data)
admins = [row["email"] for row in reader]
return admins
def is_admin(decoded_token):
user_email = decoded_token.get("email")
admins = get_admins_list()
return user_email in admins
# Helper function to update S3 CSV
def update_s3_csv(bucket, key, data, fieldnames):
output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
csv_data = output.getvalue()
s3 = boto3.client(
"s3",
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name="us-east-1",
)
s3.put_object(Bucket=bucket, Key=key, Body=csv_data.encode("utf-8"))
def get_user_details(request):
try:
decoded_token = request.decoded_token
user_email = decoded_token.get("email")
if not user_email:
return jsonify({"message": "Email not found in token"}), 400
collaborators = get_collaborators_data()
user_details = None
for collaborator in collaborators:
if collaborator["email"] == user_email:
user_details = collaborator
break
if not user_details:
return jsonify({"message": "User not found"}), 404
return jsonify(user_details), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def update_user_details(request):
try:
decoded_token = request.decoded_token
user_email = decoded_token.get("email")
if not user_email:
return jsonify({"message": "Email not found in token"}), 400
# Get the updated details from the request body
user_updates = request.get_json()
if not user_updates:
return jsonify({"message": "Invalid request body"}), 400
collaborator_email = user_updates.get("email")
if not is_admin(decoded_token) and collaborator_email != user_email:
return jsonify({"message": "Invalid request body"}), 405
# Fetch current collaborators data
collaborators = get_collaborators_data()
user_updates["timestamp"] = datetime.datetime.now().isoformat()
# Update the user's details
user_found = False
for collaborator in collaborators:
if collaborator["email"] == collaborator_email:
for key, value in user_updates.items():
if key in collaborator:
collaborator[key] = value
user_found = True
break
if not user_found:
return jsonify({"message": "User not found"}), 404
# Save the updated data back to S3
# Convert the collaborators list back to CSV
fieldnames = collaborators[0].keys()
output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(collaborators)
csv_data = output.getvalue()
s3 = boto3.client(
"s3",
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name="us-east-1",
)
s3.put_object(
Bucket=COLLABORATORS_BUCKET,
Key=COLLABORATORS_KEY,
Body=csv_data.encode("utf-8"),
)
return jsonify({"message": "User details updated successfully"}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def delete_collaborator(request):
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
data = request.get_json()
if not data or "email" not in data:
return jsonify({"message": "Invalid request body"}), 400
collaborator_email = data["email"]
collaborators = get_collaborators_data()
# Remove the collaborator if they exist
updated_collaborators = [collab for collab in collaborators if collab["email"] != collaborator_email]
if len(updated_collaborators) == len(collaborators):
return jsonify({"message": "Collaborator not found"}), 404
update_s3_csv(COLLABORATORS_BUCKET, COLLABORATORS_KEY, updated_collaborators, collaborators[0].keys())
return jsonify({"message": "Collaborator deleted successfully"}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def add_collaborator(request):
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
collaborator_data = request.get_json()
if not collaborator_data or "email" not in collaborator_data:
return jsonify({"message": "Invalid request body"}), 400
collaborators = get_collaborators_data()
# Check if the collaborator already exists
for collaborator in collaborators:
if collaborator["email"] == collaborator_data["email"]:
return jsonify({"message": "Collaborator already exists"}), 400
max_index = max([int(collab["index"]) for collab in collaborators], default=0)
collaborator_data["index"] = str(max_index + 1)
collaborator_data["timestamp"] = datetime.datetime.now().isoformat()
# Add the new collaborator
collaborators.applicationend(collaborator_data)
update_s3_csv(COLLABORATORS_BUCKET, COLLABORATORS_KEY, collaborators, collaborators[0].keys())
return jsonify({"message": "Collaborator added successfully"}), 201
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def get_all_collaborators(request):
if request.method == "OPTIONS":
return _build_cors_preflight_response()
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
collaborators = get_collaborators_data()
return jsonify(collaborators), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def get_admins(request):
if request.method == "OPTIONS":
return _build_cors_preflight_response()
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
admins = get_admins_list()
return jsonify({"admins": admins}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def add_admin(request):
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
# Get the email of the new admin from the request body
data = request.get_json()
if not data or "email" not in data:
return jsonify({"message": "Invalid request body"}), 400
new_admin_email = data["email"]
# Get current admins
s3 = boto3.client(
"s3",
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name="us-east-1",
)
obj = s3.get_object(Bucket=ADMINS_BUCKET, Key=ADMINS_KEY)
data = obj["Body"].read().decode("utf-8").splitlines()
reader = csv.DictReader(data)
admins = list(reader)
# Check if the email is already an admin
for admin in admins:
if admin["email"] == new_admin_email:
return jsonify({"message": "User is already an admin"}), 400
# Add the new admin
admins.append({"email": new_admin_email})
# Write back to S3
fieldnames = ["email"]
output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(admins)
csv_data = output.getvalue()
s3.put_object(
Bucket=ADMINS_BUCKET, Key=ADMINS_KEY, Body=csv_data.encode("utf-8")
)
return jsonify({"message": "Admin added successfully"}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def delete_admin(request):
try:
decoded_token = request.decoded_token
if not is_admin(decoded_token):
return jsonify({"message": "Forbidden"}), 403
data = request.get_json()
if not data or "email" not in data:
return jsonify({"message": "Invalid request body"}), 400
admin_email = data["email"]
admins = get_admins_list()
# Remove the admin if they exist
if admin_email not in admins:
return jsonify({"message": "Admin not found"}), 404
updated_admins = [{"email": email} for email in admins if email != admin_email]
update_s3_csv(ADMINS_BUCKET, ADMINS_KEY, updated_admins, ["email"])
return jsonify({"message": "Admin deleted successfully"}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def check_admin_status(request):
try:
decoded_token = request.decoded_token
is_user_admin = is_admin(decoded_token)
return jsonify({"is_admin": is_user_admin}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({"message": "Internal server error"}), 500
def _build_cors_preflight_response():
response = jsonify({"status": "success"})
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
response.headers.add("Access-Control-Max-Age", "10000")
response.headers.add(
"Access-Control-Allow-Headers", "Content-Type,Authorization, X-Requested-With"
)
return response
if __name__ == "__main__":
application.run(port=5001, debug=True)