feat: accept botEmail/botPassword from request, auto-create system bot in DB
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
f743932768
commit
7b4be3dbc9
1 changed files with 56 additions and 33 deletions
|
|
@ -728,42 +728,65 @@ async def testAuth(
|
||||||
if not meetingUrl:
|
if not meetingUrl:
|
||||||
raise HTTPException(status_code=400, detail="meetingUrl is required")
|
raise HTTPException(status_code=400, detail="meetingUrl is required")
|
||||||
|
|
||||||
# Load system bot credentials for the auth test variant
|
# Load system bot credentials:
|
||||||
email = None
|
# 1. Use email/password from request body (direct override)
|
||||||
password = None
|
# 2. Fallback: load from DB (system bot record)
|
||||||
credentialDebug = {"mandateId": mandateId, "botFound": False, "botEmail": None, "botMandateId": None, "searchStrategy": "byMandate"}
|
email = body.get("botEmail")
|
||||||
|
password = body.get("botPassword")
|
||||||
|
credentialDebug = {"mandateId": mandateId, "source": "none"}
|
||||||
|
|
||||||
systemBot = interface.getActiveSystemBot(mandateId)
|
if email and password:
|
||||||
|
# Direct override from request body
|
||||||
# Fallback: if no bot found for this mandate, search ALL system bots
|
credentialDebug["source"] = "requestBody"
|
||||||
if not systemBot:
|
|
||||||
logger.info(f"[test-auth] No bot for mandate {mandateId}, searching all system bots...")
|
|
||||||
credentialDebug["searchStrategy"] = "allBots"
|
|
||||||
allBots = interface.db.getRecordset(TeamsbotSystemBot, recordFilter={"isActive": True})
|
|
||||||
if allBots:
|
|
||||||
systemBot = allBots[0]
|
|
||||||
credentialDebug["allBotsCount"] = len(allBots)
|
|
||||||
credentialDebug["botMandateId"] = systemBot.get("mandateId")
|
|
||||||
logger.info(f"[test-auth] Found {len(allBots)} active bot(s), using first: {systemBot.get('email')} (mandate={systemBot.get('mandateId')})")
|
|
||||||
|
|
||||||
if systemBot:
|
|
||||||
email = systemBot.get("email")
|
|
||||||
encryptedPwd = systemBot.get("encryptedPassword")
|
|
||||||
credentialDebug["botFound"] = True
|
|
||||||
credentialDebug["botEmail"] = email
|
credentialDebug["botEmail"] = email
|
||||||
logger.info(f"[test-auth] System bot: email={email}, hasEncryptedPwd={bool(encryptedPwd)}")
|
logger.info(f"[test-auth] Using credentials from request: {email}")
|
||||||
if encryptedPwd:
|
|
||||||
try:
|
# Also create/update system bot in DB for future use
|
||||||
from modules.shared.configuration import decryptValue
|
try:
|
||||||
password = decryptValue(encryptedPwd, userId=str(context.user.id), keyName="systemBotPassword")
|
from modules.shared.configuration import encryptValue
|
||||||
credentialDebug["passwordDecrypted"] = True
|
encryptedPassword = encryptValue(password, userId=str(context.user.id), keyName="systemBotPassword")
|
||||||
logger.info(f"[test-auth] Password decrypted, length={len(password) if password else 0}")
|
existingBot = interface.getActiveSystemBot(mandateId)
|
||||||
except Exception as e:
|
if not existingBot:
|
||||||
credentialDebug["passwordError"] = str(e)
|
botData = TeamsbotSystemBot(
|
||||||
logger.error(f"[test-auth] Password decryption failed: {e}")
|
mandateId=mandateId,
|
||||||
|
name=email.split("@")[0].replace(".", " ").title(),
|
||||||
|
email=email,
|
||||||
|
encryptedPassword=encryptedPassword,
|
||||||
|
isActive=True,
|
||||||
|
).model_dump()
|
||||||
|
interface.createSystemBot(botData)
|
||||||
|
credentialDebug["dbCreated"] = True
|
||||||
|
logger.info(f"[test-auth] Created system bot in DB: {email} for mandate {mandateId}")
|
||||||
|
else:
|
||||||
|
credentialDebug["dbExists"] = True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[test-auth] Could not save system bot to DB: {e}")
|
||||||
else:
|
else:
|
||||||
logger.warning(f"[test-auth] No active system bot found anywhere")
|
# Try loading from DB
|
||||||
credentialDebug["searchStrategy"] = "noneFound"
|
systemBot = interface.getActiveSystemBot(mandateId)
|
||||||
|
if not systemBot:
|
||||||
|
# Fallback: search ALL active bots
|
||||||
|
allBots = interface.db.getRecordset(TeamsbotSystemBot, recordFilter={"isActive": True})
|
||||||
|
if allBots:
|
||||||
|
systemBot = allBots[0]
|
||||||
|
credentialDebug["source"] = "dbFallback"
|
||||||
|
|
||||||
|
if systemBot:
|
||||||
|
email = systemBot.get("email")
|
||||||
|
encryptedPwd = systemBot.get("encryptedPassword")
|
||||||
|
credentialDebug["source"] = credentialDebug.get("source", "db")
|
||||||
|
credentialDebug["botEmail"] = email
|
||||||
|
if encryptedPwd:
|
||||||
|
try:
|
||||||
|
from modules.shared.configuration import decryptValue
|
||||||
|
password = decryptValue(encryptedPwd, userId=str(context.user.id), keyName="systemBotPassword")
|
||||||
|
logger.info(f"[test-auth] Loaded from DB: {email}")
|
||||||
|
except Exception as e:
|
||||||
|
credentialDebug["passwordError"] = str(e)
|
||||||
|
logger.error(f"[test-auth] Password decryption failed: {e}")
|
||||||
|
else:
|
||||||
|
logger.warning(f"[test-auth] No credentials provided and no system bot in DB")
|
||||||
|
credentialDebug["source"] = "noneFound"
|
||||||
|
|
||||||
# Forward to browser bot service
|
# Forward to browser bot service
|
||||||
browserBotUrl = effectiveConfig._getEffectiveBrowserBotUrl()
|
browserBotUrl = effectiveConfig._getEffectiveBrowserBotUrl()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue