chariscait commited on
Commit
cdf0a2e
·
verified ·
1 Parent(s): 2e7ca97

Fix auth: Brevo HTTP API + hardcoded creds

Browse files
Files changed (1) hide show
  1. auth.py +114 -50
auth.py CHANGED
@@ -23,9 +23,10 @@ from typing import Optional
23
 
24
  SMTP_HOST = os.environ.get("SMTP_HOST", "smtp-relay.brevo.com")
25
  SMTP_PORT = int(os.environ.get("SMTP_PORT", "587"))
26
- SMTP_USER = os.environ.get("SMTP_USER", "")
27
- SMTP_PASS = os.environ.get("SMTP_PASS", "")
28
- SMTP_FROM = os.environ.get("SMTP_FROM", "noreply@caitcore.com")
 
29
 
30
  TRIAL_DURATION_SECONDS = 60
31
  VIDEO_MAX_DURATION_SECONDS = 60
@@ -36,6 +37,7 @@ USED_EMAILS_FILE = Path(__file__).parent / "used_emails.json"
36
 
37
  def smtp_configured() -> bool:
38
  """Check whether SMTP credentials are set."""
 
39
  return bool(SMTP_HOST and SMTP_USER and SMTP_PASS)
40
 
41
 
@@ -86,63 +88,125 @@ def generate_code() -> str:
86
  return f"{random.randint(100000, 999999)}"
87
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  def send_code_email(to_email: str, code: str) -> tuple[bool, str]:
90
- """Send verification code via SMTP.
91
 
92
  Returns (success, message).
93
- If SMTP is not configured, returns (False, ...) so caller can show code on screen.
94
  """
95
  if not smtp_configured():
96
  return False, "SMTP not configured"
97
 
 
98
  try:
99
- msg = MIMEMultipart("alternative")
100
- msg["Subject"] = f"EmoSphere Demo Access Code: {code}"
101
- msg["From"] = SMTP_FROM
102
- msg["To"] = to_email
103
-
104
- text_body = (
105
- f"Your EmoSphere demo access code is: {code}\n\n"
106
- f"This code is valid for one use only.\n"
107
- f"The demo session lasts {TRIAL_DURATION_SECONDS} seconds.\n\n"
108
- f"-- EmoSphere by CAIT"
109
- )
110
-
111
- html_body = f"""
112
- <html><body style="font-family: -apple-system, sans-serif; background: #0A0A1A; color: #B0BCD0; padding: 40px;">
113
- <div style="max-width: 480px; margin: 0 auto; background: linear-gradient(135deg, #161640, #1E1E52);
114
- border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; padding: 40px;">
115
- <h1 style="background: linear-gradient(90deg, #E948A0, #9B6FCE, #00D4FF);
116
- -webkit-background-clip: text; -webkit-text-fill-color: transparent;
117
- font-size: 28px; text-align: center; margin-bottom: 24px;">
118
- EMOSPHERE
119
- </h1>
120
- <p style="text-align: center; color: #6B7B9D; font-size: 14px;">Your demo access code:</p>
121
- <div style="text-align: center; font-size: 36px; font-weight: 800; letter-spacing: 8px;
122
- color: #00D4FF; padding: 20px; background: rgba(0,212,255,0.08);
123
- border-radius: 12px; margin: 16px 0;">
124
- {code}
125
- </div>
126
- <p style="text-align: center; color: #6B7B9D; font-size: 12px; margin-top: 24px;">
127
- One-time code &bull; Session lasts {TRIAL_DURATION_SECONDS}s &bull; No data stored
128
- </p>
129
- </div>
130
- </body></html>
131
- """
132
-
133
- msg.attach(MIMEText(text_body, "plain"))
134
- msg.attach(MIMEText(html_body, "html"))
135
-
136
- with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=10) as server:
137
- server.ehlo()
138
- server.starttls()
139
- server.ehlo()
140
- server.login(SMTP_USER, SMTP_PASS)
141
- server.sendmail(SMTP_FROM, to_email, msg.as_string())
142
-
143
- return True, "Code sent to your email"
144
 
 
 
 
145
  except Exception as e:
 
146
  return False, f"Failed to send email: {e}"
147
 
148
 
 
23
 
24
  SMTP_HOST = os.environ.get("SMTP_HOST", "smtp-relay.brevo.com")
25
  SMTP_PORT = int(os.environ.get("SMTP_PORT", "587"))
26
+ SMTP_USER = os.environ.get("SMTP_USER", "a7fb01001@smtp-brevo.com")
27
+ SMTP_PASS = os.environ.get("SMTP_PASS", "xsmtpsib-95d4419e874210d694181e185f1a19f0a13d14f09176441701adfbcc2ceab161-gfhZ4hNblHSUa6CM")
28
+ SMTP_FROM = os.environ.get("SMTP_FROM", "info@caitcore.com")
29
+ BREVO_API_KEY = os.environ.get("BREVO_API_KEY", "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")
30
 
31
  TRIAL_DURATION_SECONDS = 60
32
  VIDEO_MAX_DURATION_SECONDS = 60
 
37
 
38
  def smtp_configured() -> bool:
39
  """Check whether SMTP credentials are set."""
40
+ print(f"[Auth] SMTP check: HOST={SMTP_HOST}, USER={SMTP_USER[:10]}..., PASS={'set' if SMTP_PASS else 'empty'}, FROM={SMTP_FROM}")
41
  return bool(SMTP_HOST and SMTP_USER and SMTP_PASS)
42
 
43
 
 
88
  return f"{random.randint(100000, 999999)}"
89
 
90
 
91
+ def _build_html_email(code: str) -> str:
92
+ """Build branded HTML email for EmoSphere."""
93
+ return f"""
94
+ <html><body style="font-family: -apple-system, sans-serif; background: #0A0A1A; color: #B0BCD0; padding: 40px;">
95
+ <div style="max-width: 520px; margin: 0 auto; background: linear-gradient(135deg, #161640, #1E1E52);
96
+ border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; padding: 40px;">
97
+ <div style="text-align: center; margin-bottom: 24px;">
98
+ <img src="https://caitcore.com/images/emosphere-logo.png" alt="EmoSphere" style="width: 80px; height: 80px; border-radius: 16px;" />
99
+ </div>
100
+ <h1 style="background: linear-gradient(90deg, #E948A0, #9B6FCE, #00D4FF);
101
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent;
102
+ font-size: 26px; text-align: center; margin: 0 0 8px 0;">
103
+ EmoSphere
104
+ </h1>
105
+ <p style="text-align: center; color: #6B7B9D; font-size: 13px; margin: 0 0 24px 0;">Multimodal Emotion Recognition</p>
106
+ <p style="text-align: center; color: #8899AA; font-size: 14px;">Your demo access code:</p>
107
+ <div style="text-align: center; font-size: 36px; font-weight: 800; letter-spacing: 8px;
108
+ color: #00D4FF; padding: 20px; background: rgba(0,212,255,0.08);
109
+ border-radius: 12px; margin: 16px 0;">
110
+ {code}
111
+ </div>
112
+ <p style="text-align: center; color: #6B7B9D; font-size: 12px; margin-top: 24px;">
113
+ One-time code &bull; Session lasts {TRIAL_DURATION_SECONDS}s &bull; No data stored
114
+ </p>
115
+ <hr style="border: none; border-top: 1px solid rgba(255,255,255,0.08); margin: 28px 0 16px;" />
116
+ <div style="text-align: center;">
117
+ <img src="https://caitcore.com/images/cait-logo.png" alt="CAIT" style="width: 32px; height: 32px; vertical-align: middle; border-radius: 6px;" />
118
+ <span style="color: #556677; font-size: 11px; vertical-align: middle; margin-left: 8px;">Powered by CAIT &mdash; Computational &amp; AI Technologies</span>
119
+ </div>
120
+ </div>
121
+ </body></html>
122
+ """
123
+
124
+
125
+ def _send_via_http(to_email: str, code: str) -> tuple[bool, str]:
126
+ """Send email via Brevo HTTP API (works even if SMTP port is blocked)."""
127
+ import urllib.request
128
+ import urllib.error
129
+
130
+ html_body = _build_html_email(code)
131
+ payload = json.dumps({
132
+ "sender": {"name": "CAIT", "email": SMTP_FROM},
133
+ "to": [{"email": to_email}],
134
+ "subject": f"EmoSphere Demo Access Code: {code}",
135
+ "htmlContent": html_body,
136
+ })
137
+
138
+ req = urllib.request.Request(
139
+ "https://api.brevo.com/v3/smtp/email",
140
+ data=payload.encode("utf-8"),
141
+ headers={
142
+ "accept": "application/json",
143
+ "content-type": "application/json",
144
+ "api-key": BREVO_API_KEY,
145
+ },
146
+ method="POST",
147
+ )
148
+ try:
149
+ with urllib.request.urlopen(req, timeout=10) as resp:
150
+ print(f"[Auth] HTTP API response: {resp.status} {resp.read().decode()}")
151
+ return True, "Code sent to your email"
152
+ except urllib.error.HTTPError as e:
153
+ body = e.read().decode() if e.fp else ""
154
+ print(f"[Auth] HTTP API error: {e.code} {body}")
155
+ return False, f"HTTP API error: {e.code}"
156
+ except Exception as e:
157
+ print(f"[Auth] HTTP API exception: {e}")
158
+ return False, str(e)
159
+
160
+
161
+ def _send_via_smtp(to_email: str, code: str) -> tuple[bool, str]:
162
+ """Send email via SMTP (fallback)."""
163
+ html_body = _build_html_email(code)
164
+ text_body = (
165
+ f"Your EmoSphere demo access code is: {code}\n\n"
166
+ f"This code is valid for one use only.\n"
167
+ f"The demo session lasts {TRIAL_DURATION_SECONDS} seconds.\n\n"
168
+ f"-- EmoSphere by CAIT"
169
+ )
170
+
171
+ msg = MIMEMultipart("alternative")
172
+ msg["Subject"] = f"EmoSphere Demo Access Code: {code}"
173
+ msg["From"] = SMTP_FROM
174
+ msg["To"] = to_email
175
+ msg.attach(MIMEText(text_body, "plain"))
176
+ msg.attach(MIMEText(html_body, "html"))
177
+
178
+ with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=10) as server:
179
+ server.ehlo()
180
+ server.starttls()
181
+ server.ehlo()
182
+ server.login(SMTP_USER, SMTP_PASS)
183
+ server.sendmail(SMTP_FROM, to_email, msg.as_string())
184
+
185
+ return True, "Code sent to your email"
186
+
187
+
188
  def send_code_email(to_email: str, code: str) -> tuple[bool, str]:
189
+ """Send verification code tries HTTP API first, falls back to SMTP.
190
 
191
  Returns (success, message).
 
192
  """
193
  if not smtp_configured():
194
  return False, "SMTP not configured"
195
 
196
+ # Try HTTP API first (works on HF Spaces where SMTP port may be blocked)
197
  try:
198
+ ok, msg = _send_via_http(to_email, code)
199
+ if ok:
200
+ return ok, msg
201
+ print(f"[Auth] HTTP API failed ({msg}), trying SMTP...")
202
+ except Exception as e:
203
+ print(f"[Auth] HTTP API exception: {e}, trying SMTP...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
+ # Fallback to SMTP
206
+ try:
207
+ return _send_via_smtp(to_email, code)
208
  except Exception as e:
209
+ print(f"[Auth] SMTP send FAILED: {e}")
210
  return False, f"Failed to send email: {e}"
211
 
212