-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hawi74-webauthn-clean'
- Loading branch information
Showing
26 changed files
with
1,129 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[settings] | ||
multi_line_output=3 | ||
include_trailing_comma=True | ||
line_length=88 |
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
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
Empty file.
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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class WebauthnConfig(AppConfig): | ||
name = "djoser.webauthn" |
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,45 @@ | ||
# Generated by Django 2.2.4 on 2019-09-05 05:11 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL)] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="CredentialOptions", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("challenge", models.TextField()), | ||
("username", models.TextField(unique=True)), | ||
("display_name", models.TextField()), | ||
("ukey", models.TextField(unique=True)), | ||
("credential_id", models.TextField()), | ||
("sign_count", models.IntegerField(null=True)), | ||
("public_key", models.TextField()), | ||
( | ||
"user", | ||
models.OneToOneField( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="credential_options", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
) | ||
] |
Empty file.
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,18 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
|
||
|
||
class CredentialOptions(models.Model): | ||
challenge = models.TextField() | ||
username = models.TextField(unique=True) | ||
display_name = models.TextField() | ||
ukey = models.TextField(unique=True) | ||
user = models.OneToOneField( | ||
settings.AUTH_USER_MODEL, | ||
related_name="credential_options", | ||
null=True, | ||
on_delete=models.CASCADE, | ||
) | ||
credential_id = models.TextField() | ||
sign_count = models.IntegerField(null=True) | ||
public_key = models.TextField() |
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,69 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.db import IntegrityError, transaction | ||
from rest_framework import serializers | ||
|
||
from djoser.conf import settings | ||
from djoser.serializers import UserCreateMixin | ||
|
||
from .models import CredentialOptions | ||
from .utils import create_challenge, create_ukey | ||
|
||
User = get_user_model() | ||
|
||
|
||
class WebauthnSignupSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CredentialOptions | ||
fields = ("username", "display_name") | ||
|
||
def create(self, validated_data): | ||
validated_data.update( | ||
{ | ||
"challenge": create_challenge( | ||
length=settings.WEBAUTHN["CHALLENGE_LENGTH"] | ||
), | ||
"ukey": create_ukey(length=settings.WEBAUTHN["UKEY_LENGTH"]), | ||
} | ||
) | ||
return super().create(validated_data) | ||
|
||
def validate_username(self, username): | ||
if User.objects.filter(username=username).exists(): | ||
raise serializers.ValidationError( | ||
"User {} already exists.".format(username) | ||
) | ||
return username | ||
|
||
|
||
class WebauthnCreateUserSerializer(UserCreateMixin, serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = tuple(User.REQUIRED_FIELDS) + ( | ||
settings.LOGIN_FIELD, | ||
User._meta.pk.name, | ||
) | ||
|
||
|
||
class WebauthnLoginSerializer(serializers.Serializer): | ||
default_error_messages = { | ||
"invalid_credentials": settings.CONSTANTS.messages.INVALID_CREDENTIALS_ERROR | ||
} | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.fields[settings.LOGIN_FIELD] = serializers.CharField(required=True) | ||
|
||
def validate_username(self, username): | ||
try: | ||
search_kwargs = { | ||
settings.LOGIN_FIELD: username, | ||
"credential_options__isnull": False, | ||
} | ||
self.user = user = User.objects.get(**search_kwargs) | ||
except User.DoesNotExist: | ||
self.fail("invalid_credentials") | ||
|
||
if not user.is_active: | ||
self.fail("invalid_credentials") | ||
|
||
return username |
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,18 @@ | ||
from django.conf.urls import url | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
url( | ||
r"^signup_request/$", | ||
views.SingupRequestView.as_view(), | ||
name="webauthn_signup_request", | ||
), | ||
url(r"^signup/(?P<ukey>.+)/$", views.SignupView.as_view(), name="webauthn_signup"), | ||
url( | ||
r"^login_request/$", | ||
views.LoginRequestView.as_view(), | ||
name="webauthn_login_request", | ||
), | ||
url(r"^login/$", views.LoginView.as_view(), name="webauthn_login"), | ||
] |
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,13 @@ | ||
import string | ||
from random import SystemRandom | ||
|
||
random = SystemRandom() | ||
challenge_characters = string.ascii_letters + string.digits | ||
|
||
|
||
def create_challenge(length): | ||
return "".join(random.choices(challenge_characters, k=length)) | ||
|
||
|
||
def create_ukey(length): | ||
return create_challenge(length) |
Oops, something went wrong.