Skip to content

Commit 3050b0f

Browse files
committed
Merge branch 'passkey' of git.20c.com:gh/20c/django-security-keys into passkey
2 parents 17cf5ec + 8d6d08e commit 3050b0f

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

src/django_security_keys/backends.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
class PasskeyAuthenticationBackend(ModelBackend):
21-
2221
"""
2322
Passkey authentication through webauthn
2423
"""

src/django_security_keys/models.py

-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434

3535
class UserHandle(models.Model):
36-
3736
"""
3837
Unique identifier used to map users to their webauthn security keys
3938
@@ -100,7 +99,6 @@ def require_for_user(cls, user: User | SimpleLazyObject) -> UserHandle:
10099

101100

102101
class SecurityKey(models.Model):
103-
104102
"""
105103
Describes a Webauthn (U2F) SecurityKey be used for passkey
106104
login or 2FA

src/django_security_keys/templatetags/two_factor_ext.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Template filters / tags to help with two-factor auth
33
"""
4+
45
from django import template
56
from django.utils.translation import gettext_lazy as _
67
from django_otp import devices_for_user

src/django_security_keys/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
path(
2020
"decommission-form/", views.remove_security_key_form, name="decommission-form"
2121
),
22+
path("update-security-key/", views.update_security_key, name="update-security-key"),
2223
]

src/django_security_keys/views.py

+31
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,34 @@ def remove_security_key_form(
295295
remove_security_key(request, **kwargs)
296296

297297
return redirect(reverse("security-keys:manage-keys"))
298+
299+
300+
@login_required
301+
@transaction.atomic
302+
def update_security_key(request: WSGIRequest, **kwargs: Any) -> JsonResponse:
303+
"""
304+
Update a security key's passkey login status.
305+
306+
This requires the following POST data:
307+
- id (`int`): key id
308+
- passkey_login (`bool`): whether to enable passkey login
309+
310+
Returns a JSON response with the updated key's details
311+
"""
312+
id = request.POST.get("id")
313+
passkey_login = convert_to_bool(request.POST.get("passkey_login", False))
314+
315+
try:
316+
sec_key = request.user.webauthn_security_keys.get(pk=id)
317+
except SecurityKey.DoesNotExist:
318+
return JsonResponse({"non_field_errors": [_("Key not found")]}, status=404)
319+
320+
sec_key.passkey_login = passkey_login
321+
sec_key.save()
322+
323+
return JsonResponse({
324+
"status": "ok",
325+
"id": sec_key.id,
326+
"name": sec_key.name,
327+
"passkey_login": sec_key.passkey_login
328+
})

tests/project/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
16+
1617
from django.contrib import admin
1718
from django.urls import include, path, re_path
1819
from two_factor.urls import urlpatterns as tf_urls

0 commit comments

Comments
 (0)