-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log out user from all devices (#5106)
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from allauth.usersessions.models import UserSession | ||
from django.urls import reverse | ||
|
||
from kobo.apps.kobo_auth.shortcuts import User | ||
from kpi.tests.base_test_case import BaseTestCase | ||
|
||
|
||
class TestLogoutAll(BaseTestCase): | ||
|
||
fixtures = ['test_data'] | ||
|
||
def test_logout_all_sessions(self): | ||
# create 2 user sessions | ||
user = User.objects.get(username='someuser') | ||
UserSession.objects.create(user=user, session_key='12345', ip='1.2.3.4') | ||
UserSession.objects.create(user=user, session_key='56789', ip='5.6.7.8') | ||
count = UserSession.objects.filter(user=user).count() | ||
self.assertEqual(count, 2) | ||
self.client.force_login(user) | ||
url = self._get_endpoint('logout_all') | ||
self.client.post(reverse(url)) | ||
|
||
# ensure both sessions have been deleted | ||
count = UserSession.objects.filter(user=user).count() | ||
self.assertEqual(count, 0) | ||
|
||
def test_logout_all_sessions_does_not_affect_other_users(self): | ||
user1 = User.objects.get(username='someuser') | ||
user2 = User.objects.get(username='anotheruser') | ||
# create sessions for user1 | ||
UserSession.objects.create( | ||
user=user1, session_key='12345', ip='1.2.3.4' | ||
) | ||
UserSession.objects.create( | ||
user=user1, session_key='56789', ip='5.6.7.8' | ||
) | ||
count = UserSession.objects.count() | ||
self.assertEqual(count, 2) | ||
|
||
# login user2 | ||
self.client.force_login(user2) | ||
url = self._get_endpoint('logout_all') | ||
self.client.post(reverse(url)) | ||
|
||
# ensure no sessions have been deleted | ||
count = UserSession.objects.filter().count() | ||
self.assertEqual(count, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from allauth.usersessions.adapter import get_adapter | ||
from allauth.usersessions.models import UserSession | ||
from rest_framework.decorators import api_view, permission_classes | ||
from rest_framework.response import Response | ||
|
||
from kpi.permissions import IsAuthenticated | ||
|
||
|
||
@api_view(['POST']) | ||
@permission_classes((IsAuthenticated,)) | ||
def logout_from_all_devices(request): | ||
""" | ||
Log calling user out from all devices | ||
<pre class="prettyprint"> | ||
<b>POST</b> /logout-all/ | ||
</pre> | ||
> Example | ||
> | ||
> curl -H 'Authorization Token 12345' -X POST https://[kpi-url]/logout-all | ||
> Response 200 | ||
> { "Logged out of all sessions" } | ||
""" | ||
user = request.user | ||
all_user_sessions = UserSession.objects.purge_and_list(user) | ||
adapter = get_adapter() | ||
adapter.end_sessions(all_user_sessions) | ||
return Response('Logged out of all sessions') |