Files
sonr/pkg/gateway/middleware/credentials.go
T

84 lines
2.5 KiB
Go
Raw Normal View History

2024-12-16 15:29:54 -05:00
package middleware
import (
"net/http"
"github.com/labstack/echo/v4"
2024-12-19 06:22:44 -05:00
hwayorm "github.com/onsonr/sonr/pkg/gateway/orm"
2024-12-16 15:29:54 -05:00
)
2024-12-18 15:53:45 -05:00
func ListCredentials(c echo.Context, handle string) ([]*CredentialDescriptor, error) {
2024-12-16 15:29:54 -05:00
cc, ok := c.(*GatewayContext)
if !ok {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Credentials Context not found")
}
creds, err := cc.dbq.GetCredentialsByHandle(bgCtx(), handle)
if err != nil {
return nil, err
}
2024-12-18 15:53:45 -05:00
return CredentialArrayToDescriptors(creds), nil
2024-12-16 15:29:54 -05:00
}
2024-12-18 15:53:45 -05:00
func SubmitCredential(c echo.Context, cred *CredentialDescriptor) error {
2024-12-16 15:29:54 -05:00
origin := GetOrigin(c)
handle := GetHandle(c)
md := cred.ToModel(handle, origin)
cc, ok := c.(*GatewayContext)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, "Credentials Context not found")
}
2024-12-18 15:53:45 -05:00
_, err := cc.dbq.InsertCredential(bgCtx(), hwayorm.InsertCredentialParams{
2024-12-16 15:29:54 -05:00
Handle: handle,
CredentialID: md.CredentialID,
Origin: origin,
Type: md.Type,
Transports: md.Transports,
})
if err != nil {
return err
}
return nil
}
2024-12-18 15:53:45 -05:00
// 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
}