Skip to content

Commit

Permalink
feat: dispatch emails on sso based logins
Browse files Browse the repository at this point in the history
  • Loading branch information
nimish-ks committed Sep 24, 2023
1 parent a049888 commit 33ab56a
Showing 1 changed file with 107 additions and 7 deletions.
114 changes: 107 additions & 7 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@
from rest_framework import status
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from backend.api.email import send_email

CLOUD_HOSTED = settings.APP_HOST == 'cloud'

# for custom gitlab adapter class

def get_client_ip(request):
"""
Get client IP address from request.
"""
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip

def _check_errors(response):
# 403 error's are presented as user-facing errors
Expand Down Expand Up @@ -85,9 +96,38 @@ def complete_login(self, request, app, token, response, **kwargs):
raise OAuth2Error("Invalid id_token") from e
login = self.get_provider().sociallogin_from_response(request, identity_data)
email = login.email_addresses[0]

if CLOUD_HOSTED and not CustomUser.objects.filter(email=email).exists():
# new user
notify_slack(f"New user signup: {email}")
try:
# Notify Slack
notify_slack(f"New user signup: {email}")
except Exception as e:
print(f"Error notifying Slack: {e}")

# Grabbing metadata
user_agent = request.META.get('HTTP_USER_AGENT', 'Unknown')
ip_address = get_client_ip(request)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# Creating context dictionary
context = {
'auth': 'GitHub',
'email': email,
'ip': ip_address,
'user_agent': user_agent,
'timestamp': timestamp
}

try:
# Calling send_email function
send_email(
'New Login Alert - Phase Console',
[email],
'backend/api/email_templates/login.html',
context
)
except Exception as e:
print(f"Error sending email: {e}")

return login

Expand Down Expand Up @@ -117,9 +157,39 @@ def complete_login(self, request, app, token, **kwargs):
extra_data["email"] = self.get_email(headers)

email = extra_data["email"]

if CLOUD_HOSTED and not CustomUser.objects.filter(email=email).exists():
# new user
notify_slack(f"New user signup: {email}")
try:
# Notify Slack
notify_slack(f"New user signup: {email}")
except Exception as e:
print(f"Error notifying Slack: {e}")

# Grabbing metadata
user_agent = request.META.get('HTTP_USER_AGENT', 'Unknown')
ip_address = get_client_ip(request)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# Creating context dictionary
context = {
'auth': 'GitHub',
'email': email,
'ip': ip_address,
'user_agent': user_agent,
'timestamp': timestamp
}

try:
# Calling send_email function
send_email(
'New Login Alert - Phase Console',
[email],
'backend/api/email_templates/login.html',
context
)
except Exception as e:
print(f"Error sending email: {e}")

return self.get_provider().sociallogin_from_response(request, extra_data)


Expand All @@ -145,9 +215,39 @@ def complete_login(self, request, app, token, response):

email = login.email_addresses[0]

if CLOUD_HOSTED and not CustomUser.objects.filter(email=email).exists():
# new user
notify_slack(f"New user signup: {email}")
if CLOUD_HOSTED:
# Check if user exists and notify Slack for new user signup
if not CustomUser.objects.filter(email=email).exists():
try:
notify_slack(f"New user signup: {email}")
except Exception as e:
print(f"Error notifying Slack: {e}")

# Grabbing metadata
user_agent = request.META.get('HTTP_USER_AGENT', 'Unknown')
ip_address = get_client_ip(request)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# Creating context dictionary
context = {
'auth': 'GitLab',
'email': email,
'ip': ip_address,
'user_agent': user_agent,
'timestamp': timestamp
}

try:
# Calling send_email function
send_email(
'New Login Alert - Phase Console',
[email],
'backend/api/email_templates/login.html',
context
)
except Exception as e:
print(f"Error sending email: {e}")

return login


Expand Down

0 comments on commit 33ab56a

Please sign in to comment.