-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
319 lines (256 loc) · 11.7 KB
/
Copy pathutils.py
File metadata and controls
319 lines (256 loc) · 11.7 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
import os
import random
import string
import logging
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
logger = logging.getLogger(__name__)
def generate_random_password(length=12):
"""Generate a random secure password."""
characters = string.ascii_letters + string.digits + string.punctuation
# Ensure at least one of each type of character
password = [
random.choice(string.ascii_lowercase),
random.choice(string.ascii_uppercase),
random.choice(string.digits),
random.choice('!@#$%^&*()_-+=')
]
# Fill the rest of the password with random characters
password.extend(random.choice(characters) for _ in range(length - len(password)))
# Shuffle the password characters
random.shuffle(password)
return ''.join(password)
def send_email(to_email, subject, body):
"""Send an email using zoho smtp server."""
try:
# configuration
smtp_host = "smtp.zoho.com" # Primary SMTP server
primary_port = 587 # Preferred TLS port
fallback_ports = [25] # Fallback TLS ports
sender_email = "admin@hq.iam-alliance.com" # Default sender email
smtp_username = os.environ.get("SMTP_RELAY_USER", sender_email) # Use env var or default to sender_email
smtp_password = os.environ.get("SMTP_RELAY_AUTHPW")
if not smtp_password:
logger.error("SMTP relay password not configured. Email cannot be sent.")
return False
# Create message
msg = MIMEMultipart()
msg['From'] = f"IaMA RVI Support <{sender_email}>"
msg['To'] = to_email
msg['Subject'] = subject
# Attach body
msg.attach(MIMEText(body, 'plain'))
# Try primary port first, then fallbacks if needed
ports_to_try = [primary_port] + fallback_ports
for port in ports_to_try:
try:
logger.info(f"Connecting to SMTP server {smtp_host} on port {port}")
with smtplib.SMTP(smtp_host, port) as server:
server.ehlo()
server.starttls()
server.ehlo()
# Authenticate with SMTP2GO
logger.info(f"Authenticating with SMTP2GO using username: {smtp_username}")
server.login(smtp_username, smtp_password)
# Send the email
server.send_message(msg)
logger.info(f"Email sent successfully to {to_email} via port {port}")
# If we reach here, email was sent successfully
return True
except Exception as port_error:
logger.warning(f"Failed to send email via port {port}: {str(port_error)}")
# Continue to next port if this one failed
continue
# If we get here, all ports failed
logger.error("All SMTP ports failed. Email could not be sent.")
return False
except Exception as e:
logger.error(f"Failed to send email: {str(e)}")
return False
def send_account_notification(admin_email, user_email, username, admin_name):
"""Send notification emails to both admin and new user."""
# Send to admin
admin_subject = f"New RVI User Account Created: {username}"
admin_body = f"""
Hello {admin_name},
A new user account has been created:
Username: {username}
Email: {user_email}
A temporary password has been generated for this account. Please provide it to the user via Signal or Element chat message.
Regards,
IaM-Alliance System
"""
send_email(admin_email, admin_subject, admin_body)
# Send to user
user_subject = "Welcome to IaM-Alliance Registration Vetting and Invitation System"
user_body = f"""
Hello {username},
An account has been created for you on the IaM-Alliance Vetting System. The URL for the site is:
https://rvi.iam-alliance.com
Your username is: {username}
A server administrator will send you the temporary password separately via Signal or Element chat message.
Upon first login, you will be prompted to change your password.
If you need password assistance, please contact a server administrator via Signal, Element, or email support@rvi.iam-alliance.com.
Regards,
IaM-Alliance Registration Vetting and Invitation System
"""
send_email(user_email, user_subject, user_body)
def send_token_notification(admin_email, admin_name, full_name, email, assigned_username, token, expiry_date, vetting_info):
"""Send notification email to admin with token and vetting information."""
subject = f"Matrix Registration Token for {full_name}"
verification_info = f"Verification method: {vetting_info.get('verification_method', 'Not specified')}\n"
if vetting_info.get('verification_date'):
verification_info += f"Verification date: {vetting_info.get('verification_date')}\n"
verification_info += f"Verification location: {vetting_info.get('verification_location', 'Not specified')}"
vetting_info_text = f"""
Vetting Score: {vetting_info.get('vetting_score', 'Not specified')}
Vetting Notes: {vetting_info.get('vetting_notes', 'None')}
"""
body = f"""
Hello {admin_name},
A Matrix registration token has been generated for an approved applicant:
APPLICANT INFORMATION:
Full Name: {full_name}
Email: {email}
Assigned Username: {assigned_username}
VETTING DETAILS:
{verification_info}
{vetting_info_text}
TOKEN INFORMATION:
Token: {token}
Expiry Date: {expiry_date}
NEXT STEPS:
This token is ready for the approved new member to use.
1. First, please send the person a Signal message - or an email if the person is not available on Signal - informing them that they are approved as a new member of IaM-Alliance.
2. In that message, include the Assigned Username (with the caveat that if they do not use the assigned username, their account may be deleted and replaced).
3. The message should also include a link to the FAQs and User Guide for the Element chat client application, and a link to element.iam-alliance.com.
4. IN A SEPARATE NEW MESSAGE: Please provide the token to the person via a Signal message - or an email if the person is not available on Signal. In the second message, include only the token and expiration date of the token.
Regards,
IaM-Alliance System
"""
return send_email(admin_email, subject, body)
import requests
import time
import json
from datetime import datetime, timedelta
def matrix_api_post(user_fullname, user_email, assigned_username):
"""
Request the Matrix API to generate a new registration token.
Instead of creating our own token, we ask the Matrix server to create one.
"""
try:
# Endpoint to create a new token (without specifying token value)
api_url = "https://matrix.iam-alliance.com/_synapse/admin/v1/registration_tokens/new"
bearer_token = os.environ.get('MATRIX_ADMIN_TOKEN')
if not bearer_token:
logger.error("MATRIX_ADMIN_TOKEN environment variable not set")
return {
"success": False,
"error": "API Bearer Token not configured",
"response": None
}
# Calculate expiry time (30 days from now in milliseconds since epoch)
expiry_time_seconds = int(time.time()) + (30 * 24 * 60 * 60) # 30 days in seconds
expiry_time_ms = expiry_time_seconds * 1000 # Convert to milliseconds
# Prepare request data based on the documented token object structure
request_data = {
"uses_allowed": 1,
"expiry_time": expiry_time_ms
}
# Make the API request - using POST to create a new token
response = requests.post(
api_url,
json=request_data,
headers={
"Authorization": f"Bearer {bearer_token}",
"Accept": "application/json",
"Content-Type": "application/json"
},
timeout=10
)
# Parse the response
if response.status_code == 200:
response_data = response.json()
# Calculate the expiry date in the format YYYY-MMM-DD
# Subtract 48 hours (172800 seconds) from the expiry time as specified
adjusted_expiry_time = expiry_time_seconds - 172800
expiry_date = datetime.fromtimestamp(adjusted_expiry_time).strftime('%Y-%b-%d')
# Return success response with parsed data
return {
"success": True,
"response": response_data,
"expiry_time": expiry_time_ms,
"expiry_date": expiry_date,
"response_timestamp": datetime.utcnow().isoformat() + 'Z' # ISO 8601 format with Z for UTC
}
else:
# Log error details
logger.error(f"Matrix API error: {response.status_code}, Response: {response.text}")
# Return error response
return {
"success": False,
"error": f"API returned status code {response.status_code}",
"response": response.text
}
except Exception as e:
# Log exception
logger.error(f"Exception in matrix_api_post: {str(e)}", exc_info=True)
# Return exception details
return {
"success": False,
"error": str(e),
"response": None
}
def get_matrix_token_info(token):
"""
Get information about a specific Matrix token from the Matrix API.
Returns status of token including usage (pending/completed) based on Matrix API documentation.
"""
try:
# Matching the same API endpoint structure shown in the documentation
api_url = f"https://matrix.iam-alliance.com/_synapse/admin/v1/registration_tokens/{token}"
bearer_token = os.environ.get('MATRIX_ADMIN_TOKEN')
if not bearer_token:
logger.error("MATRIX_ADMIN_TOKEN environment variable not set")
return {
"success": False,
"error": "API Bearer Token not configured",
"response": None
}
# Make the API request with the appropriate headers
response = requests.get(
api_url,
headers={
"Authorization": f"Bearer {bearer_token}",
"Accept": "application/json"
},
timeout=10
)
# Parse the response
if response.status_code == 200:
response_data = response.json()
# API returns token details with usage status: pending, completed, uses_allowed, expiry_time
return {
"success": True,
"response": response_data,
"timestamp": datetime.utcnow().isoformat() + 'Z' # ISO 8601 format
}
else:
# Log error details
logger.error(f"Matrix API error: {response.status_code}, Response: {response.text}")
# Return error response
return {
"success": False,
"error": f"API returned status code {response.status_code}",
"response": response.text
}
except Exception as e:
# Log exception
logger.error(f"Exception in get_matrix_token_info: {str(e)}", exc_info=True)
# Return exception details
return {
"success": False,
"error": str(e),
"response": None
}