Signing works
This commit is contained in:
parent
d272fa90b4
commit
da2a89010c
19 changed files with 348 additions and 100 deletions
|
@ -2,19 +2,18 @@ package webdebug
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
httputils "git.mstar.dev/mstar/goutils/http"
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/shared"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
webshared "git.mstar.dev/mstar/linstrom/web/shared"
|
||||
|
@ -27,25 +26,27 @@ func getNonDeletedUsers(w http.ResponseWriter, r *http.Request) {
|
|||
var err error
|
||||
page, err = strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
httputils.HttpErr(w, 0, "page is not a number", http.StatusBadRequest)
|
||||
webutils.HttpErr(w, 0, "page is not a number", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
users, err := dbgen.User.GetPagedAllNonDeleted(uint(page))
|
||||
if err != nil {
|
||||
httputils.HttpErr(w, 0, "failed to get users", http.StatusInternalServerError)
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
"/errors/db-failure",
|
||||
"database failure",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
return
|
||||
}
|
||||
marshalled, err := json.Marshal(sliceutils.Map(users, func(t models.User) webshared.User {
|
||||
webutils.SendJson(w, sliceutils.Map(users, func(t models.User) webshared.User {
|
||||
u := webshared.User{}
|
||||
u.FromModel(&t)
|
||||
return u
|
||||
}))
|
||||
if err != nil {
|
||||
httputils.HttpErr(w, 0, "failed to marshal users", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(marshalled))
|
||||
}
|
||||
|
||||
func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -61,19 +62,43 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
|||
data := Inbound{}
|
||||
err := jsonDecoder.Decode(&data)
|
||||
if err != nil {
|
||||
httputils.HttpErr(w, 0, "decode failed", http.StatusBadRequest)
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-request-data",
|
||||
"bad request data",
|
||||
nil,
|
||||
map[string]any{
|
||||
"sample": Inbound{
|
||||
Username: "bob",
|
||||
Displayname: "Bob Bobbington",
|
||||
Description: "Bobbing Bobs bop to Bobs bobbing beats",
|
||||
Birthday: other.IntoPointer(time.Now()),
|
||||
Location: nil,
|
||||
IsBot: false,
|
||||
},
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// publicKey, privateKey, err := ed25519.GenerateKey(nil)
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
publicKeyBytes := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey)
|
||||
publicKeyEdBytes, privateKeyEdBytes, err := shared.GenerateKeypair(true)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to generate and marshal public key")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
publicKeyRsaBytes, privateKeyRsaBytes, err := shared.GenerateKeypair(false)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to generate and marshal public key")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
pkeyId := make([]byte, 64)
|
||||
_, err = rand.Read(pkeyId)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to generate passkey id")
|
||||
httputils.HttpErr(w, 0, "failed to generate passkey id", http.StatusInternalServerError)
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -84,8 +109,10 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
|||
u.Description,
|
||||
u.IsBot,
|
||||
u.ServerId,
|
||||
u.PrivateKey,
|
||||
u.PublicKey,
|
||||
u.PrivateKeyEd,
|
||||
u.PublicKeyEd,
|
||||
u.PrivateKeyRsa,
|
||||
u.PublicKeyRsa,
|
||||
u.PasskeyId,
|
||||
)
|
||||
if data.Birthday != nil {
|
||||
|
@ -95,14 +122,16 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
|||
query = query.Select(u.Location)
|
||||
}
|
||||
user := models.User{
|
||||
Username: data.Username,
|
||||
DisplayName: data.Displayname,
|
||||
Description: data.Description,
|
||||
IsBot: data.IsBot,
|
||||
ServerId: 1, // Hardcoded, Self is always first ID
|
||||
PublicKey: publicKeyBytes,
|
||||
PrivateKey: privateKeyBytes,
|
||||
PasskeyId: pkeyId,
|
||||
Username: data.Username,
|
||||
DisplayName: data.Displayname,
|
||||
Description: data.Description,
|
||||
IsBot: data.IsBot,
|
||||
ServerId: 1, // Hardcoded, Self is always first ID
|
||||
PublicKeyRsa: publicKeyRsaBytes,
|
||||
PublicKeyEd: publicKeyEdBytes,
|
||||
PrivateKeyRsa: privateKeyRsaBytes,
|
||||
PrivateKeyEd: privateKeyEdBytes,
|
||||
PasskeyId: pkeyId,
|
||||
}
|
||||
if data.Birthday != nil {
|
||||
user.Birthday = sql.NullTime{Valid: true, Time: *data.Birthday}
|
||||
|
@ -112,7 +141,7 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if err = u.Create(&user); err != nil {
|
||||
log.Error().Err(err).Msg("failed to create new local user")
|
||||
httputils.HttpErr(w, 0, "db failure", http.StatusInternalServerError)
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue