1010from django .shortcuts import get_object_or_404 , render , resolve_url
1111from django .utils import timezone
1212from django .utils .http import url_has_allowed_host_and_scheme
13+ from django .utils .translation import gettext_lazy as _
1314from django .views .decorators .http import require_GET , require_POST
1415from django_ratelimit .decorators import ratelimit
1516from webauthn import (
@@ -85,7 +86,11 @@ def passkey_register_begin(request):
8586 existing_passkeys = list (user .passkeys .all ())
8687 if len (existing_passkeys ) >= MAX_PASSKEYS_PER_USER :
8788 return JsonResponse (
88- {"error" : f"Maximum of { MAX_PASSKEYS_PER_USER } passkeys allowed" },
89+ {
90+ "error" : _ ("Maximum of {max} passkeys allowed" ).format (
91+ max = MAX_PASSKEYS_PER_USER
92+ )
93+ },
8994 status = 400 ,
9095 )
9196 existing = [
@@ -117,12 +122,12 @@ def passkey_register_complete(request):
117122 try :
118123 data = json .loads (request .body )
119124 except (json .JSONDecodeError , ValueError ):
120- return JsonResponse ({"error" : "Invalid JSON" }, status = 400 )
125+ return JsonResponse ({"error" : _ ( "Invalid JSON" ) }, status = 400 )
121126
122127 try :
123128 challenge = _load_challenge (request , SESSION_CHALLENGE_KEY_REGISTER )
124129 except PermissionDenied :
125- return JsonResponse ({"error" : "No active WebAuthn challenge" }, status = 400 )
130+ return JsonResponse ({"error" : _ ( "No active WebAuthn challenge" ) }, status = 400 )
126131
127132 try :
128133 credential = RegistrationCredential (
@@ -149,7 +154,7 @@ def passkey_register_complete(request):
149154 request .user .pk ,
150155 exc_info = True ,
151156 )
152- return JsonResponse ({"error" : "Verification failed" }, status = 400 )
157+ return JsonResponse ({"error" : _ ( "Verification failed" ) }, status = 400 )
153158
154159 name = (data .get ("name" ) or "" ).strip ()[:128 ] or timezone .now ().strftime (
155160 "Passkey %Y-%m-%d"
@@ -188,30 +193,30 @@ def passkey_auth_complete(request):
188193 try :
189194 data = json .loads (request .body )
190195 except (json .JSONDecodeError , ValueError ):
191- return JsonResponse ({"error" : "Invalid JSON" }, status = 400 )
196+ return JsonResponse ({"error" : _ ( "Invalid JSON" ) }, status = 400 )
192197
193198 try :
194199 challenge = _load_challenge (request , SESSION_CHALLENGE_KEY_AUTH )
195200 except PermissionDenied :
196- return JsonResponse ({"error" : "No active WebAuthn challenge" }, status = 400 )
201+ return JsonResponse ({"error" : _ ( "No active WebAuthn challenge" ) }, status = 400 )
197202
198203 try :
199204 credential_id_b64 = bytes_to_base64url (base64url_to_bytes (data ["rawId" ]))
200205 except Exception :
201- return JsonResponse ({"error" : "Invalid credential" }, status = 400 )
206+ return JsonResponse ({"error" : _ ( "Invalid credential" ) }, status = 400 )
202207
203208 try :
204209 passkey = Passkey .objects .select_related ("user" ).get (
205210 credential_id = credential_id_b64
206211 )
207212 except Passkey .DoesNotExist :
208- return JsonResponse ({"error" : "Unknown credential" }, status = 400 )
213+ return JsonResponse ({"error" : _ ( "Unknown credential" ) }, status = 400 )
209214
210215 try :
211216 user_handle = data ["response" ].get ("userHandle" )
212217 if user_handle :
213218 if base64url_to_bytes (user_handle ) != str (passkey .user .pk ).encode ():
214- return JsonResponse ({"error" : "User handle mismatch" }, status = 400 )
219+ return JsonResponse ({"error" : _ ( "User handle mismatch" ) }, status = 400 )
215220 credential = AuthenticationCredential (
216221 id = data ["id" ],
217222 raw_id = base64url_to_bytes (data ["rawId" ]),
@@ -239,7 +244,7 @@ def passkey_auth_complete(request):
239244 credential_id_b64 ,
240245 exc_info = True ,
241246 )
242- return JsonResponse ({"error" : "Verification failed" }, status = 400 )
247+ return JsonResponse ({"error" : _ ( "Verification failed" ) }, status = 400 )
243248
244249 passkey .sign_count = verification .new_sign_count
245250 passkey .last_used_at = timezone .now ()
0 commit comments