Skip to content

Commit

Permalink
Clean-up webauthn support; add pyupgrade to pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dekoza committed Jan 13, 2022
1 parent ede06f2 commit c62371e
Show file tree
Hide file tree
Showing 23 changed files with 133 additions and 47 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ repos:
rev: 4.0.1
hooks:
- id: flake8

- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
hooks:
- id: pyupgrade
args: [--py37-plus]
2 changes: 1 addition & 1 deletion djoser/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __getattribute__(self, item):
val = [import_string(v) if isinstance(v, str) else v for v in val]
self[item] = val
except KeyError:
val = super(ObjDict, self).__getattribute__(item)
val = super().__getattribute__(item)

return val

Expand Down
2 changes: 1 addition & 1 deletion djoser/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.utils.translation import gettext_lazy as _


class Messages(object):
class Messages:
INVALID_CREDENTIALS_ERROR = _("Unable to log in with provided credentials.")
INACTIVE_ACCOUNT_ERROR = _("User account is disabled.")
INVALID_TOKEN_ERROR = _("Invalid token for given user.")
Expand Down
8 changes: 3 additions & 5 deletions djoser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,12 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.username_field = settings.LOGIN_FIELD
self._default_username_field = User.USERNAME_FIELD
self.fields["new_{}".format(self.username_field)] = self.fields.pop(
self.username_field
)
self.fields[f"new_{self.username_field}"] = self.fields.pop(self.username_field)

def save(self, **kwargs):
if self.username_field != self._default_username_field:
kwargs[User.USERNAME_FIELD] = self.validated_data.get(
"new_{}".format(self.username_field)
f"new_{self.username_field}"
)
return super().save(**kwargs)

Expand All @@ -288,7 +286,7 @@ def __init__(self, *args, **kwargs):
def validate(self, attrs):
attrs = super().validate(attrs)
new_username = attrs[settings.LOGIN_FIELD]
if new_username != attrs["re_new_{}".format(settings.LOGIN_FIELD)]:
if new_username != attrs[f"re_new_{settings.LOGIN_FIELD}"]:
self.fail("username_mismatch")
else:
return attrs
Expand Down
2 changes: 1 addition & 1 deletion djoser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def logout_user(request):
logout(request)


class ActionViewMixin(object):
class ActionViewMixin:
def post(self, request, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand Down
8 changes: 3 additions & 5 deletions djoser/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def reset_password_confirm(self, request, *args, **kwargs):
settings.EMAIL.password_changed_confirmation(self.request, context).send(to)
return Response(status=status.HTTP_204_NO_CONTENT)

@action(["post"], detail=False, url_path="set_{}".format(User.USERNAME_FIELD))
@action(["post"], detail=False, url_path=f"set_{User.USERNAME_FIELD}")
def set_username(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand All @@ -277,7 +277,7 @@ def set_username(self, request, *args, **kwargs):
settings.EMAIL.username_changed_confirmation(self.request, context).send(to)
return Response(status=status.HTTP_204_NO_CONTENT)

@action(["post"], detail=False, url_path="reset_{}".format(User.USERNAME_FIELD))
@action(["post"], detail=False, url_path=f"reset_{User.USERNAME_FIELD}")
def reset_username(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand All @@ -290,9 +290,7 @@ def reset_username(self, request, *args, **kwargs):

return Response(status=status.HTTP_204_NO_CONTENT)

@action(
["post"], detail=False, url_path="reset_{}_confirm".format(User.USERNAME_FIELD)
)
@action(["post"], detail=False, url_path=f"reset_{User.USERNAME_FIELD}_confirm")
def reset_username_confirm(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand Down
5 changes: 1 addition & 4 deletions djoser/webauthn/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib.auth import get_user_model
from django.db import IntegrityError, transaction
from rest_framework import serializers

from djoser.conf import settings
Expand Down Expand Up @@ -29,9 +28,7 @@ def create(self, validated_data):

def validate_username(self, username):
if User.objects.filter(username=username).exists():
raise serializers.ValidationError(
"User {} already exists.".format(username)
)
raise serializers.ValidationError(f"User {username} already exists.")
return username


Expand Down
12 changes: 7 additions & 5 deletions djoser/webauthn/urls.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from django.conf.urls import url
from django.urls import re_path

from . import views

urlpatterns = [
url(
re_path(
r"^signup_request/$",
views.SingupRequestView.as_view(),
name="webauthn_signup_request",
),
url(r"^signup/(?P<ukey>.+)/$", views.SignupView.as_view(), name="webauthn_signup"),
url(
re_path(
r"^signup/(?P<ukey>.+)/$", views.SignupView.as_view(), name="webauthn_signup"
),
re_path(
r"^login_request/$",
views.LoginRequestView.as_view(),
name="webauthn_login_request",
),
url(r"^login/$", views.LoginView.as_view(), name="webauthn_login"),
re_path(r"^login/$", views.LoginView.as_view(), name="webauthn_login"),
]
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# djoser documentation build configuration file, created by
# sphinx-quickstart on Fri Jun 2 10:56:45 2017.
Expand Down
5 changes: 3 additions & 2 deletions docs/source/webauthn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ WebAuthn
Configuration
=============

Assuming you have Djoser installed, add ``djoser.webauthn`` to ``INSTALLED_APPS``:
First make sure to install ``webauthn<1.0``. You can use webauthn as "extras" when installing djoser, e.g. ``pip install djoser[webauthn]`` or ``poetry add djoser -E webauthn``.
Then add ``djoser.webauthn`` to ``INSTALLED_APPS``:

.. code-block:: python
Expand Down Expand Up @@ -144,7 +145,7 @@ Running example app
-------------------

.. code-block:: bash
$ git clone git@github.com:sunscrapers/djoser.git
$ pip install -r requirements.txt
$ cd djoser/testproject
Expand Down
94 changes: 86 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ pytest-django = {version="^4.5.2", optional=true}
pytest-pythonpath = {version="^0.7.3", optional=true}
djet = {version="^0.3.0", optional=true}
pytz = "^2021.3"
webauthn = {version="<1.0", optional=true}


[tool.poetry.group.dev.dependencies]
pyupgrade = "^2.31.0"

[tool.poetry.dev-dependencies]
pre-commit = "^2.7.1"
tox = "^3.20.0"
Expand All @@ -66,7 +70,9 @@ test = [
"pytest-pythonpath",
"djet",
]

webauthn = [
"webauthn",
]

[tool.black]
line-length = 88
Expand Down
2 changes: 1 addition & 1 deletion testproject/testapp/static/js/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

exports.toByteArray = b64ToByteArray
exports.fromByteArray = uint8ToBase64
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
Loading

0 comments on commit c62371e

Please sign in to comment.