From 3353d9f622d5fba976014cbafefa2cff49c815e0 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Mon, 9 Dec 2024 14:51:12 -0500 Subject: [PATCH] feat: implement passkey registration flow --- pkg/blocks/cards/profile.templ | 24 +++++++------------- pkg/blocks/cards/profile_templ.go | 26 +++++++++++----------- pkg/blocks/forms/register_passkey.templ | 2 +- pkg/blocks/forms/register_passkey_templ.go | 23 +++++++++++++------ pkg/gateway/handlers/register_handler.go | 3 ++- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/pkg/blocks/cards/profile.templ b/pkg/blocks/cards/profile.templ index 962a54f94..904246483 100644 --- a/pkg/blocks/cards/profile.templ +++ b/pkg/blocks/cards/profile.templ @@ -1,19 +1,19 @@ package cards -type SonrProfileData struct { - Address string - Handle string - Name string - ChainID string - CreationBlock string +// Helper function to shorten address +func shortenAddress(address string) string { + if len(address) <= 20 { + return address + } + return address[:16] + "..." + address[len(address)-4:] } templ SonrProfile(addr string, name string, handle string, creationBlock string) {
-
+
-

sonr-testnet-1

+

sonr-testnet-1

{ handle }

@@ -36,11 +36,3 @@ templ SonrProfile(addr string, name string, handle string, creationBlock string)
} - -// Helper function to shorten address -func shortenAddress(address string) string { - if len(address) <= 20 { - return address - } - return address[:16] + "..." + address[len(address)-4:] -} diff --git a/pkg/blocks/cards/profile_templ.go b/pkg/blocks/cards/profile_templ.go index 7375e33a8..bfbf9231b 100644 --- a/pkg/blocks/cards/profile_templ.go +++ b/pkg/blocks/cards/profile_templ.go @@ -16,6 +16,14 @@ type SonrProfileData struct { CreationBlock string } +// Helper function to shorten address +func shortenAddress(address string) string { + if len(address) <= 20 { + return address + } + return address[:16] + "..." + address[len(address)-4:] +} + func SonrProfile(addr string, name string, handle string, creationBlock string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context @@ -37,14 +45,14 @@ func SonrProfile(addr string, name string, handle string, creationBlock string) templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

sonr-testnet-1

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

sonr-testnet-1

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(handle) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 17, Col: 43} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 25, Col: 43} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -57,7 +65,7 @@ func SonrProfile(addr string, name string, handle string, creationBlock string) var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(shortenAddress(addr)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 24, Col: 58} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 32, Col: 58} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -70,7 +78,7 @@ func SonrProfile(addr string, name string, handle string, creationBlock string) var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(creationBlock) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 29, Col: 55} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 37, Col: 55} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -83,7 +91,7 @@ func SonrProfile(addr string, name string, handle string, creationBlock string) var templ_7745c5c3_Var5 string templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 33, Col: 32} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/blocks/cards/profile.templ`, Line: 41, Col: 32} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) if templ_7745c5c3_Err != nil { @@ -97,12 +105,4 @@ func SonrProfile(addr string, name string, handle string, creationBlock string) }) } -// Helper function to shorten address -func shortenAddress(address string) string { - if len(address) <= 20 { - return address - } - return address[:16] + "..." + address[len(address)-4:] -} - var _ = templruntime.GeneratedTemplate diff --git a/pkg/blocks/forms/register_passkey.templ b/pkg/blocks/forms/register_passkey.templ index 6e5ac1002..e1be172cf 100644 --- a/pkg/blocks/forms/register_passkey.templ +++ b/pkg/blocks/forms/register_passkey.templ @@ -12,7 +12,7 @@ type RegisterPasskeyData struct { templ RegisterPasskey(action, method string, data RegisterPasskeyData) {

- +
diff --git a/pkg/blocks/forms/register_passkey_templ.go b/pkg/blocks/forms/register_passkey_templ.go index b8123411d..73c1b355d 100644 --- a/pkg/blocks/forms/register_passkey_templ.go +++ b/pkg/blocks/forms/register_passkey_templ.go @@ -61,7 +61,7 @@ func RegisterPasskey(action, method string, data RegisterPasskeyData) templ.Comp if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -133,8 +133,8 @@ func passkeyDropzone(addr string, userHandle string, challenge string) templ.Com func createPasskey(userId string, userHandle string, challenge string) templ.ComponentScript { return templ.ComponentScript{ - Name: `__templ_createPasskey_9b69`, - Function: `function __templ_createPasskey_9b69(userId, userHandle, challenge){const publicKey = { + Name: `__templ_createPasskey_6e79`, + Function: `function __templ_createPasskey_6e79(userId, userHandle, challenge){const publicKey = { challenge: Uint8Array.from(challenge, (c) => c.charCodeAt(0)), rp: { name: "Sonr.ID", @@ -170,16 +170,25 @@ func createPasskey(userId string, userHandle string, challenge string) templ.Com navigator.credentials .create({ publicKey }) .then((newCredentialInfo) => { - console.log(newCredentialInfo); - // Send new credential info to server for verification and registration. + // Convert credential to base64 string + const credentialJSON = JSON.stringify({ + id: newCredentialInfo.id, + rawId: Array.from(new Uint8Array(newCredentialInfo.rawId)), + response: { + attestationObject: Array.from(new Uint8Array(newCredentialInfo.response.attestationObject)), + clientDataJSON: Array.from(new Uint8Array(newCredentialInfo.response.clientDataJSON)) + }, + type: newCredentialInfo.type + }); + document.getElementById('credential-data').value = btoa(credentialJSON); }) .catch((err) => { console.error(err); // No acceptable authenticator or user refused consent. Handle appropriately. }); }`, - Call: templ.SafeScript(`__templ_createPasskey_9b69`, userId, userHandle, challenge), - CallInline: templ.SafeScriptInline(`__templ_createPasskey_9b69`, userId, userHandle, challenge), + Call: templ.SafeScript(`__templ_createPasskey_6e79`, userId, userHandle, challenge), + CallInline: templ.SafeScriptInline(`__templ_createPasskey_6e79`, userId, userHandle, challenge), } } diff --git a/pkg/gateway/handlers/register_handler.go b/pkg/gateway/handlers/register_handler.go index 698533946..460c050c0 100644 --- a/pkg/gateway/handlers/register_handler.go +++ b/pkg/gateway/handlers/register_handler.go @@ -40,6 +40,7 @@ func HandleRegisterStart(c echo.Context) error { } func HandleRegisterFinish(c echo.Context) error { - // cred := c.FormValue("credential") + cred := c.FormValue("credential") + fmt.Println(cred) return response.TemplEcho(c, register.LoadingVaultView()) }