dj-waanverse-auth 3.0.0.dev0__tar.gz → 3.0.1.dev0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth.egg-info → dj_waanverse_auth-3.0.1.dev0}/PKG-INFO +1 -1
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/backends.py +182 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/base_account.py +4 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/exceptions/__init__.py +3 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/exceptions/exception_handler.py +44 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_auth.py +51 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_password_reset_codes.py +46 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_sessions.py +68 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_verification_codes.py +46 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/migrations/0001_initial.py +63 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/tokens.py +2 -0
- dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/version.py +1 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/signup_views.py +12 -22
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth.egg-info}/PKG-INFO +1 -1
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth.egg-info/SOURCES.txt +6 -7
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/backends.py +0 -60
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/management/commands/manage_sessions.py +0 -65
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0001_initial.py +0 -86
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0002_accesscode_remove_logincode_account_and_more.py +0 -51
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0003_passkey.py +0 -27
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0004_passkey_name.py +0 -18
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0005_verificationcode_delete_accesscode_and_more.py +0 -51
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0006_session.py +0 -34
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/migrations/0007_passwordresetcode.py +0 -30
- dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth/version.py +0 -1
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/LICENSE +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/MANIFEST.in +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/README.md +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/admin.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/apps.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/authentication.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/checks.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/config/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/config/settings.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/config/types.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/management/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/management/commands/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/middleware/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/middleware/client_ip.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/migrations/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/models.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/notifications/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/notifications/dispatch.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/notifications/email.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/notifications/sms.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/serializers/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/serializers/signup_serializer.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/templates/emails/account_verification_code.html +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/templates/emails/account_verification_link.html +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/templates/emails/signup_code.html +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/throttles.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/login_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/logout_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/password_reset_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/refresh_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/session_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/signup_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/url_paths/verify_urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/urls.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/cloudflare_ranges.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/identifiers.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/cookies.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/csrf.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/jwt.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/jwt_keys.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/utils/security/turnstile.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/__init__.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/login_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/logout_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/password_reset_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/refresh_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/session_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth/views/verify_views.py +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth.egg-info/dependency_links.txt +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth.egg-info/requires.txt +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth.egg-info/top_level.txt +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/pyproject.toml +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/setup.cfg +0 -0
- {dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/setup.py +0 -0
{dj_waanverse_auth-3.0.0.dev0/dj_waanverse_auth.egg-info → dj_waanverse_auth-3.0.1.dev0}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dj_waanverse_auth
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.1.dev0
|
|
4
4
|
Summary: A comprehensive Waanverse Labs Inc. internal package for managing user accounts and authentication
|
|
5
5
|
Home-page: https://dj-waanverse-auth.waanverse.com/
|
|
6
6
|
Author: Waanverse Labs Inc.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Django email backend for MailerSend (official `mailersend` v2 SDK).
|
|
3
|
+
|
|
4
|
+
settings.py:
|
|
5
|
+
|
|
6
|
+
EMAIL_BACKEND = "yourpackage.mailersend_backend.EmailBackend"
|
|
7
|
+
MAILERSEND_API_KEY = os.environ.get("MAILERSEND_API_KEY", "")
|
|
8
|
+
DEFAULT_FROM_EMAIL = "hello@yourdomain.com" # or "Name <hello@yourdomain.com>"
|
|
9
|
+
DEFAULT_FROM_NAME = "Jaitod" # optional; used when no display name is given
|
|
10
|
+
|
|
11
|
+
Attachments (tuples or MIME objects) and reply_to are supported. MailerSend accepts a
|
|
12
|
+
single reply-to address, so only the first one is used (a warning is logged if there
|
|
13
|
+
are more).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
from email.mime.base import MIMEBase
|
|
18
|
+
from email.utils import parseaddr
|
|
19
|
+
|
|
20
|
+
from django.conf import settings
|
|
21
|
+
from django.core.mail.backends.base import BaseEmailBackend
|
|
22
|
+
from django.utils.html import strip_tags
|
|
23
|
+
from mailersend import EmailBuilder, MailerSendClient
|
|
24
|
+
|
|
25
|
+
logger = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MailerSendDeliveryError(Exception):
|
|
29
|
+
"""MailerSend answered, but did not accept the email."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _parse_address(value):
|
|
33
|
+
"""
|
|
34
|
+
'Name <a@b.com>', 'a@b.com' or {'email': ..., 'name': ...}
|
|
35
|
+
-> {'email': 'a@b.com', 'name': 'Name' or ''}
|
|
36
|
+
"""
|
|
37
|
+
if isinstance(value, dict):
|
|
38
|
+
email, name = value.get("email", ""), value.get("name", "")
|
|
39
|
+
else:
|
|
40
|
+
name, email = parseaddr(str(value))
|
|
41
|
+
email = (email or "").strip()
|
|
42
|
+
name = (name or "").strip()
|
|
43
|
+
if "@" not in email:
|
|
44
|
+
raise ValueError(f"Invalid email address: {value!r}")
|
|
45
|
+
return {"email": email, "name": name}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _recipient_dicts(values):
|
|
49
|
+
"""Recipients in the shape the SDK expects. The name key is left out when empty."""
|
|
50
|
+
recipients = []
|
|
51
|
+
for value in values or []:
|
|
52
|
+
address = _parse_address(value)
|
|
53
|
+
item = {"email": address["email"]}
|
|
54
|
+
if address["name"]:
|
|
55
|
+
item["name"] = address["name"]
|
|
56
|
+
recipients.append(item)
|
|
57
|
+
return recipients
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _attachment_parts(attachment):
|
|
61
|
+
"""Django attachments are (filename, content, mimetype) tuples or MIME objects."""
|
|
62
|
+
if isinstance(attachment, MIMEBase):
|
|
63
|
+
return (
|
|
64
|
+
attachment.get_filename() or "attachment",
|
|
65
|
+
attachment.get_payload(decode=True) or b"",
|
|
66
|
+
)
|
|
67
|
+
filename, content, _mimetype = attachment
|
|
68
|
+
return filename or "attachment", content
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class EmailBackend(BaseEmailBackend):
|
|
72
|
+
def send_messages(self, email_messages):
|
|
73
|
+
if not email_messages:
|
|
74
|
+
return 0
|
|
75
|
+
|
|
76
|
+
api_key = getattr(settings, "MAILERSEND_API_KEY", "")
|
|
77
|
+
default_from_email = getattr(settings, "DEFAULT_FROM_EMAIL", "")
|
|
78
|
+
|
|
79
|
+
# Misconfiguration is a developer error, so it is raised even with fail_silently.
|
|
80
|
+
if not api_key:
|
|
81
|
+
raise ValueError("MAILERSEND_API_KEY is not set")
|
|
82
|
+
if not default_from_email:
|
|
83
|
+
raise ValueError("DEFAULT_FROM_EMAIL is not set")
|
|
84
|
+
|
|
85
|
+
try:
|
|
86
|
+
client = MailerSendClient(api_key=api_key)
|
|
87
|
+
except Exception:
|
|
88
|
+
if not self.fail_silently:
|
|
89
|
+
raise
|
|
90
|
+
logger.exception("Could not create the MailerSend client")
|
|
91
|
+
return 0
|
|
92
|
+
|
|
93
|
+
sent = 0
|
|
94
|
+
for message in email_messages:
|
|
95
|
+
try:
|
|
96
|
+
self._send_one(client, message)
|
|
97
|
+
sent += 1
|
|
98
|
+
except Exception:
|
|
99
|
+
if not self.fail_silently:
|
|
100
|
+
raise
|
|
101
|
+
logger.exception("MailerSend failed to send %r", message.subject)
|
|
102
|
+
return sent
|
|
103
|
+
|
|
104
|
+
def _send_one(self, client, message):
|
|
105
|
+
request = self._build_request(message)
|
|
106
|
+
response = client.emails.send(request)
|
|
107
|
+
|
|
108
|
+
# Depending on the failure, the SDK either raises or hands back a response
|
|
109
|
+
# object with success=False. Treat the second case as a failure too.
|
|
110
|
+
if getattr(response, "success", True) is False:
|
|
111
|
+
raise MailerSendDeliveryError(
|
|
112
|
+
f"MailerSend rejected {message.subject!r} "
|
|
113
|
+
f"(status {getattr(response, 'status_code', 'unknown')}): "
|
|
114
|
+
f"{getattr(response, 'data', '')}"
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def _build_request(self, message):
|
|
118
|
+
# Sender: message.from_email, falling back to the default. DEFAULT_FROM_NAME
|
|
119
|
+
# is only used for the default address when no display name was given.
|
|
120
|
+
default = _parse_address(settings.DEFAULT_FROM_EMAIL)
|
|
121
|
+
sender = _parse_address(message.from_email or settings.DEFAULT_FROM_EMAIL)
|
|
122
|
+
if not sender["name"] and sender["email"] == default["email"]:
|
|
123
|
+
sender["name"] = default["name"] or getattr(
|
|
124
|
+
settings, "DEFAULT_FROM_NAME", "No Reply"
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
to = _recipient_dicts(message.to)
|
|
128
|
+
if not to:
|
|
129
|
+
raise ValueError(f"Email {message.subject!r} has no 'to' recipients")
|
|
130
|
+
|
|
131
|
+
# Body: plain emails go out as text only. HTML comes from an html alternative
|
|
132
|
+
# or from content_subtype="html". A missing text part is made from the HTML.
|
|
133
|
+
html = None
|
|
134
|
+
text = None
|
|
135
|
+
if message.content_subtype == "html":
|
|
136
|
+
html = message.body or None
|
|
137
|
+
else:
|
|
138
|
+
text = message.body or None
|
|
139
|
+
for content, mimetype in getattr(message, "alternatives", []):
|
|
140
|
+
if mimetype == "text/html":
|
|
141
|
+
html = content
|
|
142
|
+
break
|
|
143
|
+
if not html and not text:
|
|
144
|
+
raise ValueError(f"Email {message.subject!r} has no body")
|
|
145
|
+
if html and not text:
|
|
146
|
+
text = strip_tags(html)
|
|
147
|
+
|
|
148
|
+
reply_to = _recipient_dicts(message.reply_to)
|
|
149
|
+
if len(reply_to) > 1:
|
|
150
|
+
logger.warning(
|
|
151
|
+
"MailerSend accepts one reply-to address; using the first and ignoring %d other(s) for %r",
|
|
152
|
+
len(reply_to) - 1,
|
|
153
|
+
message.subject,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
builder = EmailBuilder()
|
|
157
|
+
if sender["name"]:
|
|
158
|
+
builder = builder.from_email(sender["email"], sender["name"])
|
|
159
|
+
else:
|
|
160
|
+
builder = builder.from_email(sender["email"])
|
|
161
|
+
builder = builder.to_many(to).subject(message.subject)
|
|
162
|
+
|
|
163
|
+
cc = _recipient_dicts(message.cc)
|
|
164
|
+
bcc = _recipient_dicts(message.bcc)
|
|
165
|
+
if cc:
|
|
166
|
+
builder = builder.cc(cc)
|
|
167
|
+
if bcc:
|
|
168
|
+
builder = builder.bcc(bcc)
|
|
169
|
+
if reply_to:
|
|
170
|
+
first = reply_to[0]
|
|
171
|
+
if "name" in first:
|
|
172
|
+
builder = builder.reply_to(first["email"], first["name"])
|
|
173
|
+
else:
|
|
174
|
+
builder = builder.reply_to(first["email"])
|
|
175
|
+
if html:
|
|
176
|
+
builder = builder.html(html)
|
|
177
|
+
if text:
|
|
178
|
+
builder = builder.text(text)
|
|
179
|
+
for attachment in message.attachments:
|
|
180
|
+
filename, content = _attachment_parts(attachment)
|
|
181
|
+
builder = builder.attach_content(content, filename)
|
|
182
|
+
return builder.build()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from rest_framework.exceptions import ValidationError
|
|
2
|
+
from rest_framework.views import exception_handler as drf_exception_handler
|
|
3
|
+
|
|
4
|
+
NON_FIELD = "non_field_errors"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _to_text(value):
|
|
8
|
+
"""
|
|
9
|
+
DRF returns {"identifier": ["msg"]}. Collapse each list of messages into one
|
|
10
|
+
string so the client gets {"identifier": "msg"}. Nested dicts are handled too.
|
|
11
|
+
"""
|
|
12
|
+
if isinstance(value, dict):
|
|
13
|
+
return {key: _to_text(val) for key, val in value.items()}
|
|
14
|
+
if isinstance(value, (list, tuple)):
|
|
15
|
+
parts = [_to_text(item) for item in value]
|
|
16
|
+
if all(isinstance(p, str) for p in parts):
|
|
17
|
+
return " ".join(parts) # e.g. several password validator messages
|
|
18
|
+
return parts # list of dicts (many=True serializers)
|
|
19
|
+
return str(value)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def exception_handler(exc, context):
|
|
23
|
+
"""
|
|
24
|
+
Every handled error comes back as:
|
|
25
|
+
{"message": {"<field>": "<text>", ...}}
|
|
26
|
+
Errors that don't belong to a field go under "non_field_errors".
|
|
27
|
+
"""
|
|
28
|
+
response = drf_exception_handler(exc, context)
|
|
29
|
+
if response is None:
|
|
30
|
+
return None # unhandled exception -> normal 500
|
|
31
|
+
|
|
32
|
+
data = response.data
|
|
33
|
+
|
|
34
|
+
if isinstance(exc, ValidationError):
|
|
35
|
+
message = (
|
|
36
|
+
_to_text(data) if isinstance(data, dict) else {NON_FIELD: _to_text(data)}
|
|
37
|
+
)
|
|
38
|
+
else:
|
|
39
|
+
# 401 / 403 / 404 / 429 ...: DRF gives {"detail": "..."}
|
|
40
|
+
detail = data.get("detail", data) if isinstance(data, dict) else data
|
|
41
|
+
message = {NON_FIELD: _to_text(detail)}
|
|
42
|
+
|
|
43
|
+
response.data = {"msg": message}
|
|
44
|
+
return response
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from django.core.management import call_command
|
|
4
|
+
from django.core.management.base import BaseCommand
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Command(BaseCommand):
|
|
8
|
+
help = "Clean up expired and invalid authentication records."
|
|
9
|
+
|
|
10
|
+
def add_arguments(self, parser):
|
|
11
|
+
parser.add_argument(
|
|
12
|
+
"--inactive-days",
|
|
13
|
+
type=int,
|
|
14
|
+
default=None,
|
|
15
|
+
help="Delete sessions that have not been used for this many days. Default: 30.",
|
|
16
|
+
)
|
|
17
|
+
parser.add_argument(
|
|
18
|
+
"--dry-run",
|
|
19
|
+
action="store_true",
|
|
20
|
+
help="Show what would be deleted without deleting anything.",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def handle(self, *args, **options):
|
|
24
|
+
inactive_days = options["inactive_days"]
|
|
25
|
+
dry_run = options["dry_run"]
|
|
26
|
+
|
|
27
|
+
self.stdout.write("Cleaning authentication records...\n")
|
|
28
|
+
|
|
29
|
+
self.stdout.write(self.style.MIGRATE_HEADING("Sessions"))
|
|
30
|
+
call_command(
|
|
31
|
+
"cleanup_sessions",
|
|
32
|
+
inactive_days=inactive_days,
|
|
33
|
+
dry_run=dry_run,
|
|
34
|
+
stdout=self.stdout,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
self.stdout.write(self.style.MIGRATE_HEADING("Verification codes"))
|
|
38
|
+
call_command(
|
|
39
|
+
"cleanup_verification_codes",
|
|
40
|
+
dry_run=dry_run,
|
|
41
|
+
stdout=self.stdout,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
self.stdout.write(self.style.MIGRATE_HEADING("Password reset codes"))
|
|
45
|
+
call_command(
|
|
46
|
+
"cleanup_password_reset_codes",
|
|
47
|
+
dry_run=dry_run,
|
|
48
|
+
stdout=self.stdout,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
self.stdout.write(self.style.SUCCESS("\nAuthentication cleanup complete."))
|
dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_password_reset_codes.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from django.core.management.base import BaseCommand
|
|
4
|
+
from django.db.models import Q
|
|
5
|
+
from django.utils import timezone
|
|
6
|
+
|
|
7
|
+
from dj_waanverse_auth.models import PasswordResetCode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Command(BaseCommand):
|
|
11
|
+
help = "Delete used and fully expired password reset codes."
|
|
12
|
+
|
|
13
|
+
def add_arguments(self, parser):
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"--dry-run",
|
|
16
|
+
action="store_true",
|
|
17
|
+
help="Show what would be deleted without deleting anything.",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def handle(self, *args, **options):
|
|
21
|
+
dry_run = options["dry_run"]
|
|
22
|
+
now = timezone.now()
|
|
23
|
+
|
|
24
|
+
queryset = PasswordResetCode.objects.filter(
|
|
25
|
+
Q(is_used=True)
|
|
26
|
+
| Q(
|
|
27
|
+
code_expires_at__lt=now,
|
|
28
|
+
link_expires_at__lt=now,
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
count = queryset.count()
|
|
33
|
+
|
|
34
|
+
if dry_run:
|
|
35
|
+
self.stdout.write(
|
|
36
|
+
self.style.WARNING(
|
|
37
|
+
f"Dry run: {count} password reset code(s) would be deleted."
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
deleted, _ = queryset.delete()
|
|
43
|
+
|
|
44
|
+
self.stdout.write(
|
|
45
|
+
self.style.SUCCESS(f"Deleted {deleted} password reset code record(s).")
|
|
46
|
+
)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from dj_waanverse_auth import settings
|
|
3
|
+
from datetime import timedelta
|
|
4
|
+
|
|
5
|
+
from django.core.management.base import BaseCommand
|
|
6
|
+
from django.db.models import Q
|
|
7
|
+
from django.utils import timezone
|
|
8
|
+
|
|
9
|
+
from dj_waanverse_auth.models import Session
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Command(BaseCommand):
|
|
13
|
+
help = "Delete revoked and inactive authentication sessions."
|
|
14
|
+
|
|
15
|
+
def add_arguments(self, parser):
|
|
16
|
+
parser.add_argument(
|
|
17
|
+
"--inactive-days",
|
|
18
|
+
type=int,
|
|
19
|
+
default=None,
|
|
20
|
+
help="Delete sessions that have not been used for this many days. Default: 30.",
|
|
21
|
+
)
|
|
22
|
+
parser.add_argument(
|
|
23
|
+
"--dry-run",
|
|
24
|
+
action="store_true",
|
|
25
|
+
help="Show what would be deleted without deleting anything.",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
def handle(self, *args, **options):
|
|
29
|
+
inactive_days = options["inactive_days"]
|
|
30
|
+
dry_run = options["dry_run"]
|
|
31
|
+
|
|
32
|
+
if inactive_days is not None and inactive_days < 1:
|
|
33
|
+
self.stderr.write(self.style.ERROR("--inactive-days must be at least 1."))
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
now = timezone.now()
|
|
37
|
+
if inactive_days is None:
|
|
38
|
+
cutoff = now - settings.refresh_token_lifetime
|
|
39
|
+
else:
|
|
40
|
+
cutoff = now - timedelta(days=inactive_days)
|
|
41
|
+
|
|
42
|
+
revoked_qs = Session.objects.filter(is_revoked=True)
|
|
43
|
+
inactive_qs = Session.objects.filter(
|
|
44
|
+
is_revoked=False,
|
|
45
|
+
last_used_at__lt=cutoff,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
revoked_count = revoked_qs.count()
|
|
49
|
+
inactive_count = inactive_qs.count()
|
|
50
|
+
total = revoked_count + inactive_count
|
|
51
|
+
|
|
52
|
+
if dry_run:
|
|
53
|
+
self.stdout.write(f"Revoked sessions: {revoked_count}")
|
|
54
|
+
self.stdout.write(f"Inactive sessions: {inactive_count}")
|
|
55
|
+
self.stdout.write(
|
|
56
|
+
self.style.WARNING(f"Dry run: {total} session(s) would be deleted.")
|
|
57
|
+
)
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
deleted, _ = Session.objects.filter(
|
|
61
|
+
Q(is_revoked=True)
|
|
62
|
+
| Q(
|
|
63
|
+
is_revoked=False,
|
|
64
|
+
last_used_at__lt=cutoff,
|
|
65
|
+
)
|
|
66
|
+
).delete()
|
|
67
|
+
|
|
68
|
+
self.stdout.write(self.style.SUCCESS(f"Deleted {deleted} session record(s)."))
|
dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth/management/commands/cleanup_verification_codes.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from django.core.management.base import BaseCommand
|
|
4
|
+
from django.db.models import Q
|
|
5
|
+
from django.utils import timezone
|
|
6
|
+
|
|
7
|
+
from dj_waanverse_auth.models import VerificationCode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Command(BaseCommand):
|
|
11
|
+
help = "Delete used and fully expired account verification codes."
|
|
12
|
+
|
|
13
|
+
def add_arguments(self, parser):
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"--dry-run",
|
|
16
|
+
action="store_true",
|
|
17
|
+
help="Show what would be deleted without deleting anything.",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def handle(self, *args, **options):
|
|
21
|
+
dry_run = options["dry_run"]
|
|
22
|
+
now = timezone.now()
|
|
23
|
+
|
|
24
|
+
queryset = VerificationCode.objects.filter(
|
|
25
|
+
Q(is_used=True)
|
|
26
|
+
| Q(
|
|
27
|
+
code_expires_at__lt=now,
|
|
28
|
+
link_expires_at__lt=now,
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
count = queryset.count()
|
|
33
|
+
|
|
34
|
+
if dry_run:
|
|
35
|
+
self.stdout.write(
|
|
36
|
+
self.style.WARNING(
|
|
37
|
+
f"Dry run: {count} verification code(s) would be deleted."
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
deleted, _ = queryset.delete()
|
|
43
|
+
|
|
44
|
+
self.stdout.write(
|
|
45
|
+
self.style.SUCCESS(f"Deleted {deleted} verification code record(s).")
|
|
46
|
+
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Generated by Django 6.1.1 on 2026-10-02 07:52
|
|
2
|
+
|
|
3
|
+
import django.db.models.deletion
|
|
4
|
+
import uuid
|
|
5
|
+
from django.conf import settings
|
|
6
|
+
from django.db import migrations, models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Migration(migrations.Migration):
|
|
10
|
+
|
|
11
|
+
initial = True
|
|
12
|
+
|
|
13
|
+
dependencies = [
|
|
14
|
+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
operations = [
|
|
18
|
+
migrations.CreateModel(
|
|
19
|
+
name='PasswordResetCode',
|
|
20
|
+
fields=[
|
|
21
|
+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
|
22
|
+
('code_hash', models.CharField(db_index=True, max_length=64)),
|
|
23
|
+
('token_hash', models.CharField(db_index=True, max_length=64)),
|
|
24
|
+
('created_at', models.DateTimeField(auto_now_add=True)),
|
|
25
|
+
('code_expires_at', models.DateTimeField()),
|
|
26
|
+
('link_expires_at', models.DateTimeField()),
|
|
27
|
+
('attempts', models.PositiveSmallIntegerField(default=0)),
|
|
28
|
+
('is_used', models.BooleanField(default=False)),
|
|
29
|
+
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='password_reset_codes', to=settings.AUTH_USER_MODEL)),
|
|
30
|
+
],
|
|
31
|
+
),
|
|
32
|
+
migrations.CreateModel(
|
|
33
|
+
name='Session',
|
|
34
|
+
fields=[
|
|
35
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
36
|
+
('refresh_token_hash', models.CharField(max_length=64)),
|
|
37
|
+
('user_agent', models.CharField(blank=True, default='', max_length=255)),
|
|
38
|
+
('ip_address', models.GenericIPAddressField(blank=True, null=True)),
|
|
39
|
+
('created_at', models.DateTimeField(auto_now_add=True)),
|
|
40
|
+
('last_used_at', models.DateTimeField(auto_now_add=True)),
|
|
41
|
+
('is_revoked', models.BooleanField(default=False)),
|
|
42
|
+
('revoked_at', models.DateTimeField(blank=True, null=True)),
|
|
43
|
+
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sessions', to=settings.AUTH_USER_MODEL)),
|
|
44
|
+
],
|
|
45
|
+
options={
|
|
46
|
+
'ordering': ['-last_used_at'],
|
|
47
|
+
},
|
|
48
|
+
),
|
|
49
|
+
migrations.CreateModel(
|
|
50
|
+
name='VerificationCode',
|
|
51
|
+
fields=[
|
|
52
|
+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
|
53
|
+
('code_hash', models.CharField(db_index=True, max_length=64)),
|
|
54
|
+
('token_hash', models.CharField(db_index=True, max_length=64)),
|
|
55
|
+
('created_at', models.DateTimeField(auto_now_add=True)),
|
|
56
|
+
('code_expires_at', models.DateTimeField()),
|
|
57
|
+
('link_expires_at', models.DateTimeField()),
|
|
58
|
+
('attempts', models.PositiveSmallIntegerField(default=0)),
|
|
59
|
+
('is_used', models.BooleanField(default=False)),
|
|
60
|
+
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='verification_codes', to=settings.AUTH_USER_MODEL)),
|
|
61
|
+
],
|
|
62
|
+
),
|
|
63
|
+
]
|
|
@@ -60,6 +60,8 @@ def issue_tokens_for_account(account, request=None) -> IssuedTokens:
|
|
|
60
60
|
|
|
61
61
|
session.set_refresh_token(refresh_token)
|
|
62
62
|
session.save(update_fields=["refresh_token_hash"])
|
|
63
|
+
account.last_login = timezone.now()
|
|
64
|
+
account.save(update_fields=["last_login"])
|
|
63
65
|
|
|
64
66
|
return IssuedTokens(
|
|
65
67
|
access_token=access_token,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "3.0.1-dev"
|
|
@@ -5,6 +5,7 @@ from rest_framework.views import APIView
|
|
|
5
5
|
from dj_waanverse_auth.throttles import SignupIdentifierThrottle, SignupIPThrottle
|
|
6
6
|
from dj_waanverse_auth import settings as auth_config
|
|
7
7
|
from django.utils.module_loading import import_string
|
|
8
|
+
from rest_framework.exceptions import PermissionDenied
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
class SignupView(APIView):
|
|
@@ -19,28 +20,17 @@ class SignupView(APIView):
|
|
|
19
20
|
|
|
20
21
|
def post(self, request):
|
|
21
22
|
if auth_config.disable_signup:
|
|
22
|
-
|
|
23
|
-
{"msg": "Something went wrong. Please try again later."},
|
|
24
|
-
status=status.HTTP_403_FORBIDDEN,
|
|
25
|
-
)
|
|
23
|
+
raise PermissionDenied("Something went wrong. Please try again later.")
|
|
26
24
|
SerializerClass = self.get_serializer_class()
|
|
27
25
|
serializer = SerializerClass(data=request.data, context={"request": request})
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
error_msg = "Invalid request payload."
|
|
41
|
-
if serializer.errors:
|
|
42
|
-
first_field = list(serializer.errors.keys())[0]
|
|
43
|
-
errors = serializer.errors[first_field]
|
|
44
|
-
error_msg = errors[0] if isinstance(errors, list) else str(errors)
|
|
45
|
-
|
|
46
|
-
return Response({"msg": error_msg}, status=status.HTTP_400_BAD_REQUEST)
|
|
27
|
+
serializer.is_valid(raise_exception=True)
|
|
28
|
+
resp = serializer.save()
|
|
29
|
+
registration_type = resp["registration_type"]
|
|
30
|
+
return Response(
|
|
31
|
+
{
|
|
32
|
+
"msg": "Account created successfully.",
|
|
33
|
+
"registration_type": registration_type,
|
|
34
|
+
},
|
|
35
|
+
status=status.HTTP_201_CREATED,
|
|
36
|
+
)
|
{dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0/dj_waanverse_auth.egg-info}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dj_waanverse_auth
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.1.dev0
|
|
4
4
|
Summary: A comprehensive Waanverse Labs Inc. internal package for managing user accounts and authentication
|
|
5
5
|
Home-page: https://dj-waanverse-auth.waanverse.com/
|
|
6
6
|
Author: Waanverse Labs Inc.
|
{dj_waanverse_auth-3.0.0.dev0 → dj_waanverse_auth-3.0.1.dev0}/dj_waanverse_auth.egg-info/SOURCES.txt
RENAMED
|
@@ -23,18 +23,17 @@ dj_waanverse_auth.egg-info/top_level.txt
|
|
|
23
23
|
dj_waanverse_auth/config/__init__.py
|
|
24
24
|
dj_waanverse_auth/config/settings.py
|
|
25
25
|
dj_waanverse_auth/config/types.py
|
|
26
|
+
dj_waanverse_auth/exceptions/__init__.py
|
|
27
|
+
dj_waanverse_auth/exceptions/exception_handler.py
|
|
26
28
|
dj_waanverse_auth/management/__init__.py
|
|
27
29
|
dj_waanverse_auth/management/commands/__init__.py
|
|
28
|
-
dj_waanverse_auth/management/commands/
|
|
30
|
+
dj_waanverse_auth/management/commands/cleanup_auth.py
|
|
31
|
+
dj_waanverse_auth/management/commands/cleanup_password_reset_codes.py
|
|
32
|
+
dj_waanverse_auth/management/commands/cleanup_sessions.py
|
|
33
|
+
dj_waanverse_auth/management/commands/cleanup_verification_codes.py
|
|
29
34
|
dj_waanverse_auth/middleware/__init__.py
|
|
30
35
|
dj_waanverse_auth/middleware/client_ip.py
|
|
31
36
|
dj_waanverse_auth/migrations/0001_initial.py
|
|
32
|
-
dj_waanverse_auth/migrations/0002_accesscode_remove_logincode_account_and_more.py
|
|
33
|
-
dj_waanverse_auth/migrations/0003_passkey.py
|
|
34
|
-
dj_waanverse_auth/migrations/0004_passkey_name.py
|
|
35
|
-
dj_waanverse_auth/migrations/0005_verificationcode_delete_accesscode_and_more.py
|
|
36
|
-
dj_waanverse_auth/migrations/0006_session.py
|
|
37
|
-
dj_waanverse_auth/migrations/0007_passwordresetcode.py
|
|
38
37
|
dj_waanverse_auth/migrations/__init__.py
|
|
39
38
|
dj_waanverse_auth/notifications/__init__.py
|
|
40
39
|
dj_waanverse_auth/notifications/dispatch.py
|