refactor: move gateway and vault packages to internal directory

This commit is contained in:
Prad Nukala
2024-12-10 12:52:19 -05:00
parent b81bdebd64
commit dc6f02a000
73 changed files with 58 additions and 59 deletions
-49
View File
@@ -1,49 +0,0 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/app/gateway/internal/pages/index"
"github.com/onsonr/sonr/app/gateway/internal/session"
"github.com/onsonr/sonr/pkg/common/response"
)
func HandleIndex(c echo.Context) error {
if isExpired(c) {
return response.TemplEcho(c, index.ReturningView())
}
return response.TemplEcho(c, index.InitialView())
}
// ╭─────────────────────────────────────────────────────────╮
// │ Utility Functions │
// ╰─────────────────────────────────────────────────────────╯
// Initial users have no authorization, user handle, or vault address
func isInitial(c echo.Context) bool {
sess, err := session.Get(c)
if err != nil {
return false
}
data := sess.Session()
return data.UserHandle == "" && data.VaultAddress == ""
}
// Expired users have either a user handle or vault address
func isExpired(c echo.Context) bool {
sess, err := session.Get(c)
if err != nil {
return false
}
data := sess.Session()
return data.UserHandle != "" || data.VaultAddress != ""
}
// Returning users have a valid authorization, and either a user handle or vault address
func isReturning(c echo.Context) bool {
sess, err := session.Get(c)
if err != nil {
return false
}
data := sess.Session()
return data.UserHandle != "" && data.VaultAddress != ""
}
-24
View File
@@ -1,24 +0,0 @@
package handlers
import "github.com/labstack/echo/v4"
func HandleLogin(c echo.Context) error {
return nil
}
//
// func LoginHandler(c echo.Context) error {
// options := PublicKeyCredentialRequestOptions{
// Challenge: "your-challenge-base64url",
// RpID: "yourdomain.com",
// Timeout: 60000,
// AllowCredentials: []CredentialDescriptor{
// {
// Type: "public-key",
// ID: "credential-id-base64url",
// },
// },
// }
//
// return GetCredentials(c, options)
// }
-98
View File
@@ -1,98 +0,0 @@
package handlers
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/go-webauthn/webauthn/protocol"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/app/gateway/internal/database"
"github.com/onsonr/sonr/app/gateway/internal/pages/register"
"github.com/onsonr/sonr/crypto/mpc"
"github.com/onsonr/sonr/pkg/common/response"
"github.com/onsonr/sonr/pkg/common/styles/forms"
)
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 {
challenge, _ := protocol.CreateChallenge()
handle := c.FormValue("handle")
firstName := c.FormValue("first_name")
lastName := c.FormValue("last_name")
ks, err := mpc.NewKeyset()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
dat := forms.RegisterPasskeyData{
Address: ks.Address(),
Handle: handle,
Name: fmt.Sprintf("%s %s", firstName, lastName),
Challenge: challenge.String(),
CreationBlock: "00001",
}
return response.TemplEcho(c, register.LinkCredentialView(dat))
}
func HandleRegisterFinish(c echo.Context) error {
// Get the raw credential JSON string
credentialJSON := c.FormValue("credential")
if credentialJSON == "" {
return echo.NewHTTPError(http.StatusBadRequest, "missing credential data")
}
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))
}
// Validate required fields
if cred.ID == "" || cred.RawID == "" {
return echo.NewHTTPError(http.StatusBadRequest, "missing credential ID")
}
if cred.Type != "public-key" {
return echo.NewHTTPError(http.StatusBadRequest, "invalid credential type")
}
if cred.Response.AttestationObject == "" || cred.Response.ClientDataJSON == "" {
return echo.NewHTTPError(http.StatusBadRequest, "missing attestation data")
}
// Decode attestation object and client data
attestationObj, err := base64.RawURLEncoding.DecodeString(cred.Response.AttestationObject)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid attestation object encoding")
}
clientData, err := base64.RawURLEncoding.DecodeString(cred.Response.ClientDataJSON)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid client data encoding")
}
// Log detailed credential information
fmt.Printf("Credential Details:\n"+
"ID: %s\n"+
"Raw ID: %s\n"+
"Type: %s\n"+
"Authenticator Attachment: %s\n"+
"Transports: %v\n"+
"Attestation Object Size: %d bytes\n"+
"Client Data Size: %d bytes\n",
cred.ID,
cred.RawID,
cred.Type,
cred.AuthenticatorAttachment,
cred.Transports,
len(attestationObj),
len(clientData))
return response.TemplEcho(c, register.LoadingVaultView())
}
-33
View File
@@ -1,33 +0,0 @@
package handlers
//
// type SpawnVaultRequest struct {
// Name string `json:"name"`
// }
//
// func SpawnVault(c echo.Context) error {
// ks, err := mpc.NewKeyset()
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
// src, err := mpc.NewSource(ks)
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
// tk, err := src.OriginToken()
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
// // Create the vault keyshare auth token
// kscid, err := tk.CID()
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
// // Create the vault config
// dir, err := config.NewFS(config.GetVaultConfig(src.Address(), kscid.String()))
// path, err := clients.IPFSAdd(c, dir)
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
// return c.Redirect(http.StatusFound, path)
// }