Skip to content

Commit

Permalink
Merge pull request #8520 from OpenMined/hotfix/check_invalid_email_token
Browse files Browse the repository at this point in the history
Check Invalid Email Token
  • Loading branch information
jcardonnet committed Feb 21, 2024
2 parents 4a90632 + af47fcd commit e92609d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/syft/src/syft/service/notifier/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def __init__(
smtp_server=self.server, smtp_port=587, access_token=self.token
)

@staticmethod
def check_credentials(
server: str,
port: int,
token: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
) -> bool:
if token:
return SMTPClient(
smtp_server=server, smtp_port=port, access_token=token
).check_credentials()
else:
return SMTPClient(
smtp_server=server,
smtp_port=port,
username=username,
password=password,
).check_credentials()

def send(self, node: AbstractNode, notification: Notification) -> Result[Ok, Err]:
try:
user_service = node.get_service("userservice")
Expand Down Expand Up @@ -118,6 +138,11 @@ def slack_enabled(self) -> bool:
def app_enabled(self) -> bool:
return self.notifiers_status[NOTIFIERS.APP]

def valid_email_credentials(self, token: str) -> bool:
return self.notifiers[NOTIFIERS.EMAIL].check_credentials(
server="smtp.postmarkapp.com", port=587, token=token
)

def send_notifications(
self,
node: AbstractNode,
Expand Down
16 changes: 14 additions & 2 deletions packages/syft/src/syft/service/notifier/notifier_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ def turn_on(
notifier.active = True

# 4 - If email token is provided.
if email_token:
is_valid = notifier.valid_email_credentials(
token=email_token,
)
if email_token and is_valid:
notifier.email_token = email_token
else:
return SyftError(
message="The token provided is not valid. Please provide a valid token."
)

result = self.stash.update(credentials=context.credentials, settings=notifier)
if result.is_err():
Expand Down Expand Up @@ -166,8 +173,13 @@ def init_notifier(
if not notifier:
notifier = NotifierSettings(
active=active,
email_token=email_token,
)

is_valid = notifier.valid_email_credentials(token=email_token)
if email_token and is_valid:
notifier.email_token = email_token
else:
notifier.active = False
notifier_stash.set(node.signing_key.verify_key, notifier)
except Exception as e:
print("Unable to create base notifier", e)
Expand Down
22 changes: 22 additions & 0 deletions packages/syft/src/syft/service/notifier/smtp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ def send(self, sender: str, receiver: List[str], subject: str, body: str) -> Non

text = msg.as_string()
server.sendmail(sender, ", ".join(receiver), text)

def check_credentials(self) -> bool:
"""Check if the credentials are valid.
Returns:
bool: True if the credentials are valid, False otherwise.
"""
try:
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.ehlo()
if server.has_extn("STARTTLS"):
server.starttls()
server.ehlo()

if self.access_token:
server.login(self.access_token, self.access_token)
elif self.username and self.password:
server.login(self.username, self.password)

return True
except Exception:
return False

0 comments on commit e92609d

Please sign in to comment.