mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 18:01:39 +00:00
feat: implement passkey registration flow
This commit is contained in:
@@ -11,14 +11,16 @@ import (
|
||||
"github.com/onsonr/sonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/pkg/blocks/forms"
|
||||
"github.com/onsonr/sonr/pkg/common/response"
|
||||
"github.com/onsonr/sonr/pkg/gateway/config"
|
||||
"github.com/onsonr/sonr/pkg/gateway/internal/database"
|
||||
"github.com/onsonr/sonr/pkg/gateway/internal/pages/register"
|
||||
)
|
||||
|
||||
func HandleRegisterView(env config.Env) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return response.TemplEcho(c, register.ProfileFormView(env.GetTurnstileSiteKey()))
|
||||
func HandleRegisterView(c echo.Context) error {
|
||||
dat := forms.CreateProfileData{
|
||||
FirstNumber: 1,
|
||||
LastNumber: 2,
|
||||
}
|
||||
return response.TemplEcho(c, register.ProfileFormView(dat))
|
||||
}
|
||||
|
||||
func HandleRegisterStart(c echo.Context) error {
|
||||
@@ -47,21 +49,7 @@ func HandleRegisterFinish(c echo.Context) error {
|
||||
if credentialJSON == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "missing credential data")
|
||||
}
|
||||
|
||||
// Define the credential structure matching our frontend data
|
||||
var cred struct {
|
||||
ID string `json:"id"`
|
||||
RawID string `json:"rawId"`
|
||||
Type string `json:"type"`
|
||||
AuthenticatorAttachment string `json:"authenticatorAttachment"`
|
||||
Transports []string `json:"transports"`
|
||||
ClientExtensionResults map[string]interface{} `json:"clientExtensionResults"`
|
||||
Response struct {
|
||||
AttestationObject string `json:"attestationObject"`
|
||||
ClientDataJSON string `json:"clientDataJSON"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
cred := database.Credential{}
|
||||
// Unmarshal the credential JSON
|
||||
if err := json.Unmarshal([]byte(credentialJSON), &cred); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid credential format: %v", err))
|
||||
@@ -106,12 +94,5 @@ func HandleRegisterFinish(c echo.Context) error {
|
||||
len(attestationObj),
|
||||
len(clientData))
|
||||
|
||||
// TODO: Verify the attestation and store the credential
|
||||
// This is where you would:
|
||||
// 1. Verify the attestation signature
|
||||
// 2. Check the origin in client data
|
||||
// 3. Verify the challenge
|
||||
// 4. Store the credential for future authentications
|
||||
|
||||
return response.TemplEcho(c, register.LoadingVaultView())
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package database
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-webauthn/webauthn/protocol"
|
||||
"github.com/labstack/echo/v4"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -17,13 +16,27 @@ var (
|
||||
ErrUserNotFound = echo.NewHTTPError(http.StatusNotFound, "User not found")
|
||||
)
|
||||
|
||||
// Define the credential structure matching our frontend data
|
||||
type Credential struct {
|
||||
ID string `json:"id"`
|
||||
RawID string `json:"rawId"`
|
||||
Type string `json:"type"`
|
||||
AuthenticatorAttachment string `json:"authenticatorAttachment"`
|
||||
Transports []string `json:"transports"`
|
||||
ClientExtensionResults map[string]interface{} `json:"clientExtensionResults"`
|
||||
Response struct {
|
||||
AttestationObject string `json:"attestationObject"`
|
||||
ClientDataJSON string `json:"clientDataJSON"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Address string `json:"address"`
|
||||
Handle string `json:"handle"`
|
||||
Name string `json:"name"`
|
||||
CID string `json:"cid"`
|
||||
Credentials []*protocol.CredentialDescriptor `json:"credentials"`
|
||||
Address string `json:"address"`
|
||||
Handle string `json:"handle"`
|
||||
Name string `json:"name"`
|
||||
CID string `json:"cid"`
|
||||
Credentials []*Credential `json:"credentials"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"github.com/onsonr/sonr/pkg/blocks/text"
|
||||
)
|
||||
|
||||
templ ProfileFormView(turnstileSiteKey string) {
|
||||
templ ProfileFormView(data forms.CreateProfileData) {
|
||||
@layout.Root("New Profile | Sonr.ID") {
|
||||
@layout.Container() {
|
||||
@text.Header("Create a Profile", "Enter some basic information about yourself.")
|
||||
@forms.CreateProfile("/register/start", "POST")
|
||||
@forms.CreateProfile("/register/start", "POST", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/onsonr/sonr/pkg/blocks/text"
|
||||
)
|
||||
|
||||
func ProfileFormView(turnstileSiteKey string) templ.Component {
|
||||
func ProfileFormView(data forms.CreateProfileData) 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
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
@@ -67,7 +67,7 @@ func ProfileFormView(turnstileSiteKey string) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = forms.CreateProfile("/register/start", "POST").Render(ctx, templ_7745c5c3_Buffer)
|
||||
templ_7745c5c3_Err = forms.CreateProfile("/register/start", "POST", data).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -34,13 +33,6 @@ func (s *HTTPContext) InitSession() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Println(sess.BrowserName)
|
||||
fmt.Println(sess.BrowserVersion)
|
||||
fmt.Println(sess.UserArchitecture)
|
||||
fmt.Println(sess.Platform)
|
||||
fmt.Println(sess.PlatformVersion)
|
||||
fmt.Println(sess.DeviceModel)
|
||||
|
||||
s.sess = &sess
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/pkg/gateway/config"
|
||||
"github.com/onsonr/sonr/pkg/gateway/internal/database"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Middleware creates a new session middleware
|
||||
func Middleware(db *gorm.DB) echo.MiddlewareFunc {
|
||||
func Middleware(db *gorm.DB, env config.Env) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
cc := NewHTTPContext(c, db)
|
||||
@@ -26,6 +27,7 @@ type HTTPContext struct {
|
||||
echo.Context
|
||||
db *gorm.DB
|
||||
sess *database.Session
|
||||
env config.Env
|
||||
}
|
||||
|
||||
// Get returns the HTTPContext from the echo context
|
||||
|
||||
@@ -21,11 +21,11 @@ func RegisterRoutes(e *echo.Echo, env config.Env) error {
|
||||
}
|
||||
|
||||
// Inject session middleware with database connection
|
||||
e.Use(session.Middleware(db))
|
||||
e.Use(session.Middleware(db, env))
|
||||
|
||||
// Register routes
|
||||
e.GET("/", handlers.HandleIndex)
|
||||
e.GET("/register", handlers.HandleRegisterView(env))
|
||||
e.GET("/register", handlers.HandleRegisterView)
|
||||
e.POST("/register/start", handlers.HandleRegisterStart)
|
||||
e.POST("/register/finish", handlers.HandleRegisterFinish)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user