2024-10-10 13:44:17 -04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-webauthn/webauthn/protocol"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-10-12 12:52:20 -04:00
|
|
|
|
|
|
|
"github.com/onsonr/sonr/internal/orm"
|
2024-10-10 13:44:17 -04:00
|
|
|
)
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
type authAPI struct{}
|
|
|
|
|
|
|
|
var Auth = new(authAPI)
|
|
|
|
|
2024-10-10 13:44:17 -04:00
|
|
|
// ╭───────────────────────────────────────────────────────────╮
|
|
|
|
// │ Login Handlers │
|
|
|
|
// ╰───────────────────────────────────────────────────────────╯
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// LoginSubjectCheck handles the login subject check.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) LoginSubjectCheck(e echo.Context) error {
|
2024-10-10 13:44:17 -04:00
|
|
|
return e.JSON(200, "HandleCredentialAssertion")
|
|
|
|
}
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// LoginSubjectStart handles the login subject start.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) LoginSubjectStart(e echo.Context) error {
|
2024-10-11 16:47:52 -04:00
|
|
|
opts := &protocol.PublicKeyCredentialRequestOptions{
|
|
|
|
UserVerification: "preferred",
|
|
|
|
Challenge: []byte("challenge"),
|
|
|
|
}
|
|
|
|
return e.JSON(200, opts)
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// LoginSubjectFinish handles the login subject finish.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) LoginSubjectFinish(e echo.Context) error {
|
2024-10-11 16:47:52 -04:00
|
|
|
var crr protocol.CredentialAssertionResponse
|
|
|
|
if err := e.Bind(&crr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.JSON(200, crr)
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ╭───────────────────────────────────────────────────────────╮
|
|
|
|
// │ Register Handlers │
|
|
|
|
// ╰───────────────────────────────────────────────────────────╯
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// RegisterSubjectCheck handles the register subject check.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) RegisterSubjectCheck(e echo.Context) error {
|
2024-10-12 12:52:20 -04:00
|
|
|
subject := e.FormValue("subject")
|
|
|
|
return e.JSON(200, subject)
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// RegisterSubjectStart handles the register subject start.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) RegisterSubjectStart(e echo.Context) error {
|
2024-10-12 12:52:20 -04:00
|
|
|
// Get subject and address
|
|
|
|
subject := e.FormValue("subject")
|
|
|
|
address := e.FormValue("address")
|
|
|
|
|
|
|
|
// Get challenge
|
2024-10-15 14:31:19 -04:00
|
|
|
chal, err := protocol.CreateChallenge()
|
2024-10-12 12:52:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
2024-10-12 12:52:20 -04:00
|
|
|
return e.JSON(201, orm.NewCredentialCreationOptions(subject, address, chal))
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-23 09:52:07 -04:00
|
|
|
// RegisterSubjectFinish handles the register subject finish.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (a *authAPI) RegisterSubjectFinish(e echo.Context) error {
|
2024-10-10 13:44:17 -04:00
|
|
|
// Deserialize the JSON into a temporary struct
|
|
|
|
var ccr protocol.CredentialCreationResponse
|
2024-10-11 16:47:52 -04:00
|
|
|
if err := e.Bind(&ccr); err != nil {
|
|
|
|
return err
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// // Parse the CredentialCreationResponse
|
|
|
|
// parsedData, err := ccr.Parse()
|
|
|
|
// if err != nil {
|
|
|
|
// return e.JSON(500, err.Error())
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Create the Credential
|
|
|
|
// // credential := orm.NewCredential(parsedData, e.Request().Host, "")
|
2024-10-12 12:52:20 -04:00
|
|
|
return e.JSON(201, ccr)
|
2024-10-10 13:44:17 -04:00
|
|
|
}
|