This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackends.py
59 lines (53 loc) · 1.96 KB
/
backends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
SMTP Authentication for Healthchecks.io
Replace the default EmailBackend in "hc/accounts/backends.py" with this updated class.
The following options need to be added to "hc/settings.py":
AUTH_SMTP_HOST - Hostname of SMTP server (string)
AUTH_SMTP_PORT - Port for SMTP server (int)
AUTH_SMTP_CREATE - Create a user for this SMTP email if login is valid and no DB user exists (bool)
AUTH_SMTP_DOMAINS - Valid domains for auto-generated user accounts. Only valid if AUTH_SMTP_CREATE is True (bool)
AUTH_SMTP_STARTTLS - Use STARTTLS for SMTP (bool)
"""
"""
Add these imports to the top of the file.
"""
from uuid import uuid4
from smtplib import SMTP
from django.conf import settings
"""
End Imports
"""
class EmailBackend(BasicBackend):
def authenticate_smtp(self, username, password):
try:
with SMTP(host=settings.AUTH_SMTP_HOST, port=settings.AUTH_SMTP_PORT) as s:
if settings.AUTH_SMTP_STARTTLS:
s.starttls()
s.login(username, password)
s.noop()
s.close()
except Exception:
return None
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
user = None
if user is None and settings.AUTH_SMTP_CREATE:
d = username.split("@")
if len(d) == 2 and len(d[1]) > 0:
if d[1].lower() not in settings.AUTH_SMTP_DOMAINS:
return None
user = User.objects.create_user(
username, email=username, password=str(uuid4())
)
return user
def authenticate(self, request=None, username=None, password=None):
user = self.authenticate_smtp(username, password)
if user is not None:
return user
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
return None
if user.check_password(password):
return user