Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Invalid Email Token #8520

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/syft/src/syft/service/notifier/notifier.py
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
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
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