choose the first valid email username (#380)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
|
||||||
@@ -16,6 +17,11 @@ from skyvern.exceptions import (
|
|||||||
LOG = structlog.get_logger()
|
LOG = structlog.get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_email(email: str) -> bool:
|
||||||
|
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
|
||||||
|
return re.match(pattern, email) is not None
|
||||||
|
|
||||||
|
|
||||||
class BitwardenConstants(StrEnum):
|
class BitwardenConstants(StrEnum):
|
||||||
CLIENT_ID = "BW_CLIENT_ID"
|
CLIENT_ID = "BW_CLIENT_ID"
|
||||||
CLIENT_SECRET = "BW_CLIENT_SECRET"
|
CLIENT_SECRET = "BW_CLIENT_SECRET"
|
||||||
@@ -161,8 +167,21 @@ class BitwardenService:
|
|||||||
if "login" in item
|
if "login" in item
|
||||||
]
|
]
|
||||||
|
|
||||||
# Todo: Handle multiple credentials, for now just return the last one
|
if len(credentials) == 0:
|
||||||
return credentials[-1] if credentials else {}
|
return {}
|
||||||
|
|
||||||
|
if len(credentials) == 1:
|
||||||
|
return credentials[0]
|
||||||
|
|
||||||
|
# Choose multiple credentials according to the defined rule,
|
||||||
|
# if no cred matches the rule, return the first one.
|
||||||
|
# TODO: For now hard code to choose the first valid email username
|
||||||
|
for cred in credentials:
|
||||||
|
if is_valid_email(cred.get(BitwardenConstants.USERNAME, "")):
|
||||||
|
return cred
|
||||||
|
|
||||||
|
LOG.warning("No credential in Bitwarden matches the rule, returning the frist match")
|
||||||
|
return credentials[0]
|
||||||
finally:
|
finally:
|
||||||
# Step 4: Log out
|
# Step 4: Log out
|
||||||
BitwardenService.logout()
|
BitwardenService.logout()
|
||||||
|
|||||||
Reference in New Issue
Block a user