Skip to content

Commit f66f5fb

Browse files
committed
feat: auth-react servers
1 parent 24da5f7 commit f66f5fb

File tree

6 files changed

+261
-136
lines changed

6 files changed

+261
-136
lines changed

tests/auth-react/django3x/mysite/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async def send_email(
190190
):
191191
save_webauthn_token(
192192
user={
193-
"email": user_context["email"],
193+
"email": template_vars.user.email,
194194
"recover_account_link": "",
195195
"token": "",
196196
},
@@ -529,6 +529,7 @@ async def sign_in_post(
529529
if body is not None and "generalErrorMessage" in body:
530530
msg = body["generalErrorMessage"]
531531
return GeneralErrorResponse(msg)
532+
532533
return await original_sign_in_post(
533534
form_fields,
534535
tenant_id,
@@ -551,6 +552,7 @@ async def sign_up_post(
551552
)
552553
if is_general_error:
553554
return GeneralErrorResponse("general error from API sign up")
555+
554556
return await original_sign_up_post(
555557
form_fields,
556558
tenant_id,
@@ -997,7 +999,7 @@ async def resync_session_and_fetch_mfa_info_put(
997999
config=WebauthnConfig(
9981000
email_delivery=EmailDeliveryConfigWithService[
9991001
TypeWebauthnEmailDeliveryInput
1000-
](service=CustomWebwuthnEmailService())
1002+
](service=CustomWebwuthnEmailService()) # type: ignore
10011003
)
10021004
),
10031005
},

tests/auth-react/django3x/polls/urls.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@
3737
path("test/setup/app", views.setup_core_app),
3838
path("test/setup/st", views.setup_st),
3939
path("test/webauthn/get-token", views.get_webauthn_token, name="getWebauthnToken"),
40-
path("test/webauth/create-and-assert-credential", views.webauthn_create_and_assert_credential, name="webauthnCreateAndAssertCredential"),
41-
path("test/webauthn/create-credential", views.webauthn_create_credential, name="webauthnCreateCredential"),
40+
path(
41+
"test/webauthn/create-and-assert-credential",
42+
views.webauthn_create_and_assert_credential,
43+
name="webauthnCreateAndAssertCredential",
44+
),
45+
path(
46+
"test/webauthn/create-credential",
47+
views.webauthn_create_credential,
48+
name="webauthnCreateCredential",
49+
),
4250
]
4351

4452
mode = os.environ.get("APP_MODE", "asgi")

tests/auth-react/django3x/polls/views.py

Lines changed: 18 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
# under the License.
1414
import json
1515
import os
16-
from typing import Any, Dict, List, cast
16+
from typing import Any, Dict, List
1717

18-
# Python loader for WASM. Allows native imports
19-
import wasmtime.loader # type: ignore # noqa: F401
18+
import httpx
2019
from django.http import HttpRequest, HttpResponse, JsonResponse
2120
from mysite.store import get_codes, get_url_with_token, webauthn_store
2221
from mysite.utils import custom_init
@@ -72,9 +71,6 @@
7271
from supertokens_python.types.auth_utils import LinkingToSessionUserFailedError
7372
from supertokens_python.types.base import AccountInfoInput
7473

75-
# Load the required functions from the WASM binary
76-
from ..webauthn import createAndAssertCredential, createCredential # type: ignore
77-
7874
mode = os.environ.get("APP_MODE", "asgi")
7975

8076

@@ -468,6 +464,7 @@ def test_feature_flags(request: HttpRequest):
468464
"recipeConfig",
469465
"accountlinking-fixes",
470466
"oauth2",
467+
"webauthn",
471468
]
472469
}
473470
)
@@ -493,113 +490,36 @@ def setup_st(request: HttpRequest):
493490

494491

495492
def get_webauthn_token(request: HttpRequest):
496-
body = json.loads(request.body)
497-
if body is None:
498-
raise Exception("Invalid request body")
499-
500-
webauthn = webauthn_store.get(body["email"])
493+
webauthn = webauthn_store.get(request.GET.get("email", ""))
501494
if webauthn is None:
502495
return JsonResponse({"error": "Webauthn not found"}, status=404)
503496

504497
return JsonResponse({"token": webauthn["token"]})
505498

506499

507-
def create_credential(
508-
register_options: Dict[str, Any],
509-
rp_id: str,
510-
rp_name: str,
511-
origin: str,
512-
user_not_present: bool = True,
513-
user_not_verified: bool = True,
514-
):
515-
register_options_str = json.dumps(register_options)
516-
result = createCredential( # type: ignore
517-
register_options_str,
518-
rp_id,
519-
rp_name,
520-
origin,
521-
user_not_present,
522-
user_not_verified,
523-
)
524-
525-
if result is None:
526-
raise Exception("Failed to create credential")
527-
528-
try:
529-
credential = json.loads(cast(str, result))
530-
return credential
531-
except Exception:
532-
raise Exception("Failed to parse credential")
533-
534-
535-
def create_and_assert_credential(
536-
register_options: Dict[str, Any],
537-
sign_in_options: Dict[str, Any],
538-
rp_id: str,
539-
rp_name: str,
540-
origin: str,
541-
user_not_present: bool = True,
542-
user_not_verified: bool = True,
543-
):
544-
register_options_str = json.dumps(register_options)
545-
sign_in_options_str = json.dumps(sign_in_options)
546-
result = createAndAssertCredential( # type: ignore
547-
register_options_str,
548-
sign_in_options_str,
549-
rp_id,
550-
rp_name,
551-
origin,
552-
user_not_present,
553-
user_not_verified,
554-
)
555-
556-
if result is None:
557-
raise Exception("Failed to create/assert credential")
558-
559-
try:
560-
parsed_result: Dict[str, Any] = json.loads(cast(str, result))
561-
return {
562-
"attestation": parsed_result["attestation"],
563-
"assertion": parsed_result["assertion"],
564-
}
565-
except Exception:
566-
raise Exception("Failed to parse result")
567-
568-
569500
def webauthn_create_and_assert_credential(request: HttpRequest):
570501
body = json.loads(request.body)
571502
if body is None:
572503
raise Exception("Invalid request body")
573504

574-
try:
575-
credential = create_and_assert_credential( # type: ignore
576-
register_options=body["registerOptionsResponse"],
577-
sign_in_options=body["signInOptionsResponse"],
578-
rp_id=body["rpId"],
579-
rp_name=body["rpName"],
580-
origin=body["origin"],
581-
user_not_present=False,
582-
user_not_verified=False,
583-
)
584-
return JsonResponse({"credential": credential})
585-
except Exception as err:
586-
return JsonResponse({"error": str(err)}, status=500)
505+
test_server_port = os.environ.get("NODE_PORT", 8082)
506+
response = httpx.post(
507+
url=f"http://localhost:{test_server_port}/test/webauthn/create-and-assert-credential",
508+
json=body,
509+
)
510+
511+
return JsonResponse(response.json())
587512

588513

589514
def webauthn_create_credential(request: HttpRequest):
590515
body = json.loads(request.body)
591516
if body is None:
592517
raise Exception("Invalid request body")
593518

594-
try:
595-
credential = create_credential( # type: ignore
596-
register_options=body["registerOptionsResponse"],
597-
rp_id=body["rpId"],
598-
rp_name=body["rpName"],
599-
origin=body["origin"],
600-
user_not_present=False,
601-
user_not_verified=False,
602-
)
603-
return JsonResponse({"credential": credential})
604-
except Exception as err:
605-
return JsonResponse({"error": str(err)}, status=500)
519+
test_server_port = os.environ.get("NODE_PORT", 8082)
520+
response = httpx.post(
521+
url=f"http://localhost:{test_server_port}/test/webauthn/create-credential",
522+
json=body,
523+
)
524+
525+
return JsonResponse(response.json())

0 commit comments

Comments
 (0)