2024-12-13 15:10:27 -05:00
|
|
|
package handlers
|
2024-12-10 13:12:08 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-webauthn/webauthn/protocol"
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
"github.com/onsonr/sonr/crypto/mpc"
|
2024-12-13 15:10:27 -05:00
|
|
|
"github.com/onsonr/sonr/internal/gateway/models"
|
|
|
|
|
"github.com/onsonr/sonr/internal/gateway/views"
|
2024-12-10 13:12:08 -05:00
|
|
|
"github.com/onsonr/sonr/pkg/common/response"
|
2024-12-13 15:10:27 -05:00
|
|
|
"golang.org/x/exp/rand"
|
2024-12-10 13:12:08 -05:00
|
|
|
)
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
func RenderProfileCreate(c echo.Context) error {
|
|
|
|
|
d := models.CreateProfileData{
|
|
|
|
|
FirstNumber: rand.Intn(5) + 1,
|
|
|
|
|
LastNumber: rand.Intn(4) + 1,
|
|
|
|
|
}
|
|
|
|
|
return response.TemplEcho(c, views.CreateProfileForm(d))
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
func RenderPasskeyCreate(c echo.Context) error {
|
2024-12-10 13:12:08 -05:00
|
|
|
challenge, _ := protocol.CreateChallenge()
|
|
|
|
|
handle := c.FormValue("handle")
|
|
|
|
|
firstName := c.FormValue("first_name")
|
|
|
|
|
lastName := c.FormValue("last_name")
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
ks, err := mpc.GenEnclave()
|
2024-12-10 13:12:08 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
dat := models.CreatePasskeyData{
|
2024-12-10 13:12:08 -05:00
|
|
|
Address: ks.Address(),
|
|
|
|
|
Handle: handle,
|
|
|
|
|
Name: fmt.Sprintf("%s %s", firstName, lastName),
|
|
|
|
|
Challenge: challenge.String(),
|
|
|
|
|
CreationBlock: "00001",
|
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
return response.TemplEcho(c, views.CreatePasskeyForm(dat))
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
func RenderVaultLoading(c echo.Context) error {
|
2024-12-10 13:12:08 -05:00
|
|
|
credentialJSON := c.FormValue("credential")
|
|
|
|
|
if credentialJSON == "" {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "missing credential data")
|
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
_, err := models.ExtractCredentialDescriptor(credentialJSON)
|
2024-12-10 13:12:08 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
return response.TemplEcho(c, views.LoadingVaultView())
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|