-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Passwordless Authentication #645
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends "emails/content.txt" %} | ||
{% block content %}{% load action_extras %}Hi, | ||
|
||
someone request a login link for your account. To log in, please use the following link (valid for {% action_link_expiration_hours action_serializer %} hours): | ||
|
||
{% action_link action_serializer %} | ||
|
||
If you did not request this, please contact [email protected]. | ||
|
||
Stay secure, | ||
The deSEC Team | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[deSEC] Login information |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,14 +51,11 @@ def register(self, email, password, captcha=None, **kwargs): | |
reverse("v1:register"), {"email": email, "password": password, **kwargs} | ||
) | ||
|
||
def login_user(self, email, password): | ||
return self.post( | ||
reverse("v1:login"), | ||
{ | ||
"email": email, | ||
"password": password, | ||
}, | ||
) | ||
def login_user(self, email, password=None): | ||
payload = {"email": email} | ||
if password is not None: | ||
payload["password"] = password | ||
return self.post(reverse("v1:login"), payload) | ||
|
||
def logout(self, token): | ||
return self.post(reverse("v1:logout"), HTTP_AUTHORIZATION=f"Token {token}") | ||
|
@@ -127,7 +124,7 @@ def register_user(self, email=None, password=None, late_captcha=False, **kwargs) | |
self.client.register(email, password, captcha, **kwargs), | ||
) | ||
|
||
def login_user(self, email, password): | ||
def login_user(self, email, password=None): | ||
return self.client.login_user(email, password) | ||
|
||
def logout(self, token): | ||
|
@@ -550,6 +547,52 @@ def _test_delete_account(self, email, password): | |
self.assertUserDoesNotExist(email) | ||
|
||
|
||
class PasswordlessUserTestCase(UserManagementTestCase): | ||
|
||
def assertLoginSuccessResponse(self, response): | ||
return self.assertContains( | ||
response=response, text="instructions", status_code=status.HTTP_202_ACCEPTED | ||
) | ||
|
||
def assertCreateLoginTokenVerificationEmail(self, reset=True): | ||
return self.assertEmailSent( | ||
subject_contains="Login information", | ||
body_contains="login link for your account", | ||
recipient=[self.email], | ||
reset=reset, | ||
pattern=r"following link[^:]*:\s+([^\s]*)", | ||
) | ||
|
||
def assertCreateLoginTokenVerificationSuccessResponse(self, response): | ||
return self.assertContains( | ||
response=response, | ||
text=f"token", | ||
status_code=status.HTTP_200_OK, | ||
) | ||
|
||
def setUp(self): | ||
self.email, _, _ = self.register_user(self.random_username()) | ||
confirmation_link = self.assertRegistrationEmail(self.email) | ||
self.assertConfirmationLinkRedirect(confirmation_link) | ||
response = self.client.verify(confirmation_link) | ||
self.assertRegistrationVerificationSuccessResponse(response) | ||
self.assertTrue(User.objects.get(email=self.email).is_active) | ||
self.assertEmailSent(reset=True) | ||
|
||
def test_login_successful(self): | ||
response = self.login_user(self.email, None) | ||
self.assertLoginSuccessResponse(response) | ||
link = self.assertCreateLoginTokenVerificationEmail() | ||
self.assertCreateLoginTokenVerificationSuccessResponse( | ||
self.client.verify(link) | ||
) | ||
|
||
def test_login_unsuccessful(self): | ||
response = self.login_user("[email protected]", None) | ||
self.assertLoginSuccessResponse(response) # no disclosure that this account does not exist! | ||
self.assertNoEmailSent() | ||
|
||
|
||
class UserLifeCycleTestCase(UserManagementTestCase): | ||
def test_life_cycle(self): | ||
self.email, self.password = self._test_registration( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import user_logged_in | ||
from rest_framework import generics, mixins, status | ||
|
@@ -93,23 +91,32 @@ def post(self, request, *args, **kwargs): | |
|
||
class AccountLoginView(generics.GenericAPIView): | ||
authentication_classes = (authentication.EmailPasswordPayloadAuthentication,) | ||
permission_classes = (IsAuthenticated,) | ||
serializer_class = serializers.TokenSerializer | ||
throttle_scope = "account_management_passive" | ||
|
||
def post(self, request, *args, **kwargs): | ||
user = self.request.user | ||
token = Token.objects.create( | ||
user=user, | ||
perm_manage_tokens=True, | ||
max_age=timedelta(days=7), | ||
max_unused_period=timedelta(hours=1), | ||
mfa=False, | ||
) | ||
user_logged_in.send(sender=user.__class__, request=self.request, user=user) | ||
|
||
data = self.get_serializer(token, include_plain=True).data | ||
return Response(data) | ||
if request.user and request.user.is_authenticated: | ||
# password was provided | ||
user = self.request.user | ||
data = self.get_serializer( | ||
Token.create_login_token(user), include_plain=True | ||
).data | ||
user_logged_in.send(sender=user.__class__, request=request, user=user) | ||
return Response(data) | ||
else: | ||
# password was not provided | ||
message = "Login instructions have been sent to the email address associated with your user account." | ||
response = Response(data={"detail": message}, status=status.HTTP_202_ACCEPTED) | ||
|
||
# TODO how to determine user ID properly? Use a serializer? | ||
try: | ||
user = User.objects.get(email=request.data.get('email')) | ||
except User.DoesNotExist: | ||
pass | ||
else: | ||
serializers.AuthenticatedCreateLoginTokenActionSerializer.build_and_save(user=user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows spamming a user with log-in emails. To avoid such scenarios with sign-up emails, we store user pending registrations, and clean them up after 24h unless activated. During that time, no other registration email can be sent. -- Is there a way to deal with this here? (One way could be to only allow passwordless login if no password is set. This would need UI to switch between password and passwordless login.) Password reset seems to have the same problem, though. |
||
|
||
return response | ||
|
||
|
||
class AccountLogoutView(APIView, mixins.DestroyModelMixin): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script> | ||
import GenericActionHandler from "./GenericActionHandler" | ||
import {HTTP} from "../utils"; | ||
|
||
export default { | ||
name: 'CreateLoginTokenActionHandler', | ||
extends: GenericActionHandler, | ||
data: () => ({ | ||
auto_submit: true, | ||
}), | ||
watch: { | ||
success(value) { | ||
if(value) { | ||
HTTP.defaults.headers.common.Authorization = `Token ${value.token}`; | ||
this.$store.commit('login', value); | ||
if (this.useSessionStorage) { | ||
sessionStorage.setItem('token', JSON.stringify(value)); | ||
} | ||
this.$router.replace({ name: 'domains' }); | ||
} | ||
} | ||
} | ||
}; | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should at least have the requesting IP address and perhaps the user agent here, so users can associate the request with themselves. (Geo-mapping seems not very reliable, so I'd vote for not having that.)