Skip to content

Commit

Permalink
Use allauth uid encoding if installed (#266)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael <[email protected]>
  • Loading branch information
squio and iMerica authored Jun 2, 2021
1 parent 7dc50bd commit 1f28787
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ JWT_AUTH_COOKIE = 'jwt-auth'

### Testing

Install required modules with `pip install -r dj_rest_auth/tests/requirements.pip`

To run the tests within a virtualenv, run `python runtests.py` from the repository directory.
The easiest way to run test coverage is with [`coverage`](https://pypi.org/project/coverage/),
which runs the tests against all supported Django installs. To run the test coverage
Expand Down
10 changes: 9 additions & 1 deletion dj_rest_auth/serializers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm

from django.contrib.auth.tokens import default_token_generator
from django.urls import exceptions as url_exceptions

from django.utils.encoding import force_str
from django.utils.http import urlsafe_base64_decode as uid_decoder
from django.utils.module_loading import import_string

try:
from django.utils.translation import gettext_lazy as _
except ImportError:
from django.utils.translation import gettext_lazy as _

if 'allauth' in settings.INSTALLED_APPS:
from allauth.account.forms import default_token_generator
from allauth.account.utils import url_str_to_user_pk as uid_decoder
else:
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_decode as uid_decoder

from rest_framework import exceptions, serializers
from rest_framework.exceptions import ValidationError

Expand Down
15 changes: 9 additions & 6 deletions dj_rest_auth/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.utils.encoding import force_str
from rest_framework import status
from rest_framework.test import APIRequestFactory

from dj_rest_auth.registration.app_settings import register_permission_classes
from dj_rest_auth.registration.views import RegisterView

Expand Down Expand Up @@ -74,11 +73,15 @@ def setUp(self):

def _generate_uid_and_token(self, user):
result = {}
from django.contrib.auth.tokens import default_token_generator
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

result['uid'] = urlsafe_base64_encode(force_bytes(user.pk))
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account.forms import default_token_generator
from allauth.account.utils import user_pk_to_url_str
result['uid'] = user_pk_to_url_str(user)
else:
from django.utils.encoding import force_bytes
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_encode
result['uid'] = urlsafe_base64_encode(force_bytes(user.pk))
result['token'] = default_token_generator.make_token(user)
return result

Expand Down

0 comments on commit 1f28787

Please sign in to comment.