mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-04 10:21:40 +00:00
feature/implement wss routes (#1196)
* feat(database): create schema for hway and motr * fix(gateway): correct naming inconsistencies in handlers * build: update schema file to be compatible with postgresql syntax * fix: update schema to be compatible with PostgreSQL syntax * chore: update query_hway.sql to follow sqlc syntax * ```text refactor: update query_hway.sql for PostgreSQL and sqlc ``` * feat: add vaults table to store encrypted data * refactor: Update vaults table schema for sqlc compatibility * chore(deps): Upgrade dependencies and add pgx/v5 * refactor(Makefile): move sqlc generate to internal/models * docs(foundations): remove outdated pages * chore(build): add Taskfile for build tasks * refactor(embed): move embed files to internal package * docs: add documentation for Cosmos SDK ORM
This commit is contained in:
@@ -4,11 +4,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/internal/database/repository"
|
||||
"github.com/onsonr/sonr/pkg/gateway/types"
|
||||
"github.com/onsonr/sonr/internal/models/drivers/hwayorm"
|
||||
)
|
||||
|
||||
func ListCredentials(c echo.Context, handle string) ([]*types.CredentialDescriptor, error) {
|
||||
func ListCredentials(c echo.Context, handle string) ([]*CredentialDescriptor, error) {
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Credentials Context not found")
|
||||
@@ -17,10 +16,10 @@ func ListCredentials(c echo.Context, handle string) ([]*types.CredentialDescript
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return types.CredentialArrayToDescriptors(creds), nil
|
||||
return CredentialArrayToDescriptors(creds), nil
|
||||
}
|
||||
|
||||
func SubmitCredential(c echo.Context, cred *types.CredentialDescriptor) error {
|
||||
func SubmitCredential(c echo.Context, cred *CredentialDescriptor) error {
|
||||
origin := GetOrigin(c)
|
||||
handle := GetHandle(c)
|
||||
md := cred.ToModel(handle, origin)
|
||||
@@ -30,7 +29,7 @@ func SubmitCredential(c echo.Context, cred *types.CredentialDescriptor) error {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Credentials Context not found")
|
||||
}
|
||||
|
||||
_, err := cc.dbq.InsertCredential(bgCtx(), repository.InsertCredentialParams{
|
||||
_, err := cc.dbq.InsertCredential(bgCtx(), hwayorm.InsertCredentialParams{
|
||||
Handle: handle,
|
||||
CredentialID: md.CredentialID,
|
||||
Origin: origin,
|
||||
@@ -42,3 +41,43 @@ func SubmitCredential(c echo.Context, cred *types.CredentialDescriptor) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Define the credential structure matching our frontend data
|
||||
type CredentialDescriptor struct {
|
||||
ID string `json:"id"`
|
||||
RawID string `json:"rawId"`
|
||||
Type string `json:"type"`
|
||||
AuthenticatorAttachment string `json:"authenticatorAttachment"`
|
||||
Transports string `json:"transports"`
|
||||
ClientExtensionResults map[string]string `json:"clientExtensionResults"`
|
||||
Response struct {
|
||||
AttestationObject string `json:"attestationObject"`
|
||||
ClientDataJSON string `json:"clientDataJSON"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
func (c *CredentialDescriptor) ToModel(handle, origin string) *hwayorm.Credential {
|
||||
return &hwayorm.Credential{
|
||||
Handle: handle,
|
||||
Origin: origin,
|
||||
CredentialID: c.ID,
|
||||
Type: c.Type,
|
||||
Transports: c.Transports,
|
||||
AuthenticatorAttachment: c.AuthenticatorAttachment,
|
||||
}
|
||||
}
|
||||
|
||||
func CredentialArrayToDescriptors(credentials []hwayorm.Credential) []*CredentialDescriptor {
|
||||
var descriptors []*CredentialDescriptor
|
||||
for _, cred := range credentials {
|
||||
cd := &CredentialDescriptor{
|
||||
ID: cred.CredentialID,
|
||||
RawID: cred.CredentialID,
|
||||
Type: cred.Type,
|
||||
AuthenticatorAttachment: cred.AuthenticatorAttachment,
|
||||
Transports: cred.Transports,
|
||||
}
|
||||
descriptors = append(descriptors, cd)
|
||||
}
|
||||
return descriptors
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/medama-io/go-useragent"
|
||||
"github.com/onsonr/sonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/internal/config/hway"
|
||||
"github.com/onsonr/sonr/internal/database/repository"
|
||||
"github.com/onsonr/sonr/internal/models/drivers/hwayorm"
|
||||
"github.com/onsonr/sonr/pkg/common"
|
||||
)
|
||||
|
||||
@@ -15,21 +13,21 @@ type GatewayContext struct {
|
||||
echo.Context
|
||||
agent useragent.UserAgent
|
||||
id string
|
||||
dbq *repository.Queries
|
||||
dbq *hwayorm.Queries
|
||||
ipfsClient common.IPFS
|
||||
tokenStore common.IPFSTokenStore
|
||||
stagedEnclaves map[string]mpc.Enclave
|
||||
grpcAddr string
|
||||
}
|
||||
|
||||
func UseGateway(env hway.Hway, ipc common.IPFS, db *sql.DB) echo.MiddlewareFunc {
|
||||
func UseGateway(env hway.Hway, ipc common.IPFS, db *hwayorm.Queries) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ua := useragent.NewParser()
|
||||
ctx := &GatewayContext{
|
||||
agent: ua.Parse(c.Request().UserAgent()),
|
||||
agent: ua.Parse(c.Request().UserAgent()),
|
||||
Context: c,
|
||||
dbq: repository.New(db),
|
||||
dbq: db,
|
||||
ipfsClient: ipc,
|
||||
grpcAddr: env.GetSonrGrpcUrl(),
|
||||
tokenStore: common.NewUCANStore(ipc),
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/internal/context"
|
||||
"github.com/onsonr/sonr/internal/database/repository"
|
||||
repository "github.com/onsonr/sonr/internal/models/drivers/hwayorm"
|
||||
)
|
||||
|
||||
func CheckHandleUnique(c echo.Context, handle string) bool {
|
||||
@@ -98,3 +98,28 @@ func DeleteProfile(c echo.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Create Profile (/register/profile) │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
// DefaultCreateProfileParams returns a default CreateProfileParams
|
||||
func DefaultCreateProfileParams() CreateProfileParams {
|
||||
return CreateProfileParams{
|
||||
TurnstileSiteKey: "",
|
||||
FirstNumber: 0,
|
||||
LastNumber: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateProfileParams represents the parameters for creating a profile
|
||||
type CreateProfileParams struct {
|
||||
TurnstileSiteKey string
|
||||
FirstNumber int
|
||||
LastNumber int
|
||||
}
|
||||
|
||||
// Sum returns the sum of the first and last number
|
||||
func (d CreateProfileParams) Sum() int {
|
||||
return d.FirstNumber + d.LastNumber
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@ package middleware
|
||||
import (
|
||||
gocontext "context"
|
||||
|
||||
"github.com/go-webauthn/webauthn/protocol"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/internal/context"
|
||||
"github.com/onsonr/sonr/internal/database"
|
||||
"github.com/medama-io/go-useragent"
|
||||
ctx "github.com/onsonr/sonr/internal/context"
|
||||
"github.com/onsonr/sonr/internal/models/drivers/hwayorm"
|
||||
"github.com/segmentio/ksuid"
|
||||
)
|
||||
|
||||
func NewSession(c echo.Context) error {
|
||||
@@ -13,13 +16,13 @@ func NewSession(c echo.Context) error {
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
baseSessionCreateParams := database.BaseSessionCreateParams(cc)
|
||||
baseSessionCreateParams := BaseSessionCreateParams(cc)
|
||||
cc.id = baseSessionCreateParams.ID
|
||||
if _, err := cc.dbq.CreateSession(bgCtx(), baseSessionCreateParams); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set Cookie
|
||||
if err := context.WriteCookie(c, context.SessionID, cc.id); err != nil {
|
||||
if err := ctx.WriteCookie(c, ctx.SessionID, cc.id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -46,10 +49,10 @@ func GetSessionID(c echo.Context) string {
|
||||
}
|
||||
// check from cookie
|
||||
if cc.id == "" {
|
||||
if ok := context.CookieExists(c, context.SessionID); !ok {
|
||||
if ok := ctx.CookieExists(c, ctx.SessionID); !ok {
|
||||
return ""
|
||||
}
|
||||
cc.id = context.ReadCookieUnsafe(c, context.SessionID)
|
||||
cc.id = ctx.ReadCookieUnsafe(c, ctx.SessionID)
|
||||
}
|
||||
return cc.id
|
||||
}
|
||||
@@ -68,7 +71,7 @@ func GetSessionChallenge(c echo.Context) string {
|
||||
|
||||
func GetHandle(c echo.Context) string {
|
||||
// First check for the cookie
|
||||
handle := context.ReadCookieUnsafe(c, context.UserHandle)
|
||||
handle := ctx.ReadCookieUnsafe(c, ctx.UserHandle)
|
||||
if handle != "" {
|
||||
return handle
|
||||
}
|
||||
@@ -107,3 +110,50 @@ func bgCtx() gocontext.Context {
|
||||
ctx := gocontext.Background()
|
||||
return ctx
|
||||
}
|
||||
|
||||
func BaseSessionCreateParams(e echo.Context) hwayorm.CreateSessionParams {
|
||||
// f := rand.Intn(5) + 1
|
||||
// l := rand.Intn(4) + 1
|
||||
challenge, _ := protocol.CreateChallenge()
|
||||
id := getOrCreateSessionID(e)
|
||||
ua := useragent.NewParser()
|
||||
s := ua.Parse(e.Request().UserAgent())
|
||||
|
||||
return hwayorm.CreateSessionParams{
|
||||
ID: id,
|
||||
BrowserName: s.GetBrowser(),
|
||||
BrowserVersion: s.GetMajorVersion(),
|
||||
ClientIpaddr: e.RealIP(),
|
||||
Platform: s.GetOS(),
|
||||
IsMobile: s.IsMobile(),
|
||||
IsTablet: s.IsTablet(),
|
||||
IsDesktop: s.IsDesktop(),
|
||||
IsBot: s.IsBot(),
|
||||
IsTv: s.IsTV(),
|
||||
// IsHumanFirst: int64(f),
|
||||
// IsHumanLast: int64(l),
|
||||
Challenge: challenge.String(),
|
||||
}
|
||||
}
|
||||
|
||||
func getOrCreateSessionID(c echo.Context) string {
|
||||
if ok := ctx.CookieExists(c, ctx.SessionID); !ok {
|
||||
sessionID := ksuid.New().String()
|
||||
ctx.WriteCookie(c, ctx.SessionID, sessionID)
|
||||
return sessionID
|
||||
}
|
||||
|
||||
sessionID, err := ctx.ReadCookie(c, ctx.SessionID)
|
||||
if err != nil {
|
||||
sessionID = ksuid.New().String()
|
||||
ctx.WriteCookie(c, ctx.SessionID, sessionID)
|
||||
}
|
||||
return sessionID
|
||||
}
|
||||
|
||||
func boolToInt64(b bool) int64 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -6,11 +6,10 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/internal/context"
|
||||
"github.com/onsonr/sonr/pkg/gateway/types"
|
||||
"lukechampine.com/blake3"
|
||||
)
|
||||
|
||||
func Spawn(c echo.Context) (types.CreatePasskeyParams, error) {
|
||||
func Spawn(c echo.Context) (CreatePasskeyParams, error) {
|
||||
cc := c.(*GatewayContext)
|
||||
block := fmt.Sprintf("%d", CurrentBlock(c))
|
||||
handle := GetHandle(c)
|
||||
@@ -19,15 +18,15 @@ func Spawn(c echo.Context) (types.CreatePasskeyParams, error) {
|
||||
sid := GetSessionID(c)
|
||||
nonce, err := calcNonce(sid)
|
||||
if err != nil {
|
||||
return types.DefaultCreatePasskeyParams(), err
|
||||
return defaultCreatePasskeyParams(), err
|
||||
}
|
||||
encl, err := mpc.GenEnclave(nonce)
|
||||
if err != nil {
|
||||
return types.DefaultCreatePasskeyParams(), err
|
||||
return defaultCreatePasskeyParams(), err
|
||||
}
|
||||
cc.stagedEnclaves[sid] = encl
|
||||
context.WriteCookie(c, context.SonrAddress, encl.Address())
|
||||
return types.CreatePasskeyParams{
|
||||
return CreatePasskeyParams{
|
||||
Address: encl.Address(),
|
||||
Handle: handle,
|
||||
Name: origin,
|
||||
@@ -36,8 +35,8 @@ func Spawn(c echo.Context) (types.CreatePasskeyParams, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Claim() (types.CreatePasskeyParams, error) {
|
||||
return types.CreatePasskeyParams{}, nil
|
||||
func Claim() (CreatePasskeyParams, error) {
|
||||
return CreatePasskeyParams{}, nil
|
||||
}
|
||||
|
||||
// Uses blake3 to hash the sessionID to generate a nonce of length 12 bytes
|
||||
@@ -55,3 +54,27 @@ func calcNonce(sessionID string) ([]byte, error) {
|
||||
}
|
||||
return nonce, nil
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Create Passkey (/register/passkey) │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
// defaultCreatePasskeyParams returns a default CreatePasskeyParams
|
||||
func defaultCreatePasskeyParams() CreatePasskeyParams {
|
||||
return CreatePasskeyParams{
|
||||
Address: "",
|
||||
Handle: "",
|
||||
Name: "",
|
||||
Challenge: "",
|
||||
CreationBlock: "",
|
||||
}
|
||||
}
|
||||
|
||||
// CreatePasskeyParams represents the parameters for creating a passkey
|
||||
type CreatePasskeyParams struct {
|
||||
Address string
|
||||
Handle string
|
||||
Name string
|
||||
Challenge string
|
||||
CreationBlock string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user