Signing works

This commit is contained in:
Melody Becker 2025-04-10 16:40:06 +02:00
parent d272fa90b4
commit da2a89010c
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
19 changed files with 348 additions and 100 deletions

View file

@ -40,14 +40,16 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
_user.BackgroundId = field.NewField(tableName, "background_id")
_user.BannerId = field.NewField(tableName, "banner_id")
_user.Indexable = field.NewBool(tableName, "indexable")
_user.PublicKey = field.NewBytes(tableName, "public_key")
_user.PublicKeyRsa = field.NewBytes(tableName, "public_key_rsa")
_user.PublicKeyEd = field.NewBytes(tableName, "public_key_ed")
_user.RestrictedFollow = field.NewBool(tableName, "restricted_follow")
_user.Location = field.NewField(tableName, "location")
_user.Birthday = field.NewField(tableName, "birthday")
_user.Verified = field.NewBool(tableName, "verified")
_user.PasskeyId = field.NewBytes(tableName, "passkey_id")
_user.FinishedRegistration = field.NewBool(tableName, "finished_registration")
_user.PrivateKey = field.NewBytes(tableName, "private_key")
_user.PrivateKeyRsa = field.NewBytes(tableName, "private_key_rsa")
_user.PrivateKeyEd = field.NewBytes(tableName, "private_key_ed")
_user.RemoteInfo = userHasOneRemoteInfo{
db: db.Session(&gorm.Session{}),
@ -353,14 +355,16 @@ type user struct {
BackgroundId field.Field
BannerId field.Field
Indexable field.Bool
PublicKey field.Bytes
PublicKeyRsa field.Bytes
PublicKeyEd field.Bytes
RestrictedFollow field.Bool
Location field.Field
Birthday field.Field
Verified field.Bool
PasskeyId field.Bytes
FinishedRegistration field.Bool
PrivateKey field.Bytes
PrivateKeyRsa field.Bytes
PrivateKeyEd field.Bytes
RemoteInfo userHasOneRemoteInfo
InfoFields userHasManyInfoFields
@ -413,14 +417,16 @@ func (u *user) updateTableName(table string) *user {
u.BackgroundId = field.NewField(table, "background_id")
u.BannerId = field.NewField(table, "banner_id")
u.Indexable = field.NewBool(table, "indexable")
u.PublicKey = field.NewBytes(table, "public_key")
u.PublicKeyRsa = field.NewBytes(table, "public_key_rsa")
u.PublicKeyEd = field.NewBytes(table, "public_key_ed")
u.RestrictedFollow = field.NewBool(table, "restricted_follow")
u.Location = field.NewField(table, "location")
u.Birthday = field.NewField(table, "birthday")
u.Verified = field.NewBool(table, "verified")
u.PasskeyId = field.NewBytes(table, "passkey_id")
u.FinishedRegistration = field.NewBool(table, "finished_registration")
u.PrivateKey = field.NewBytes(table, "private_key")
u.PrivateKeyRsa = field.NewBytes(table, "private_key_rsa")
u.PrivateKeyEd = field.NewBytes(table, "private_key_ed")
u.fillFieldMap()
@ -437,7 +443,7 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (u *user) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 33)
u.fieldMap = make(map[string]field.Expr, 35)
u.fieldMap["id"] = u.ID
u.fieldMap["username"] = u.Username
u.fieldMap["created_at"] = u.CreatedAt
@ -451,14 +457,16 @@ func (u *user) fillFieldMap() {
u.fieldMap["background_id"] = u.BackgroundId
u.fieldMap["banner_id"] = u.BannerId
u.fieldMap["indexable"] = u.Indexable
u.fieldMap["public_key"] = u.PublicKey
u.fieldMap["public_key_rsa"] = u.PublicKeyRsa
u.fieldMap["public_key_ed"] = u.PublicKeyEd
u.fieldMap["restricted_follow"] = u.RestrictedFollow
u.fieldMap["location"] = u.Location
u.fieldMap["birthday"] = u.Birthday
u.fieldMap["verified"] = u.Verified
u.fieldMap["passkey_id"] = u.PasskeyId
u.fieldMap["finished_registration"] = u.FinishedRegistration
u.fieldMap["private_key"] = u.PrivateKey
u.fieldMap["private_key_rsa"] = u.PrivateKeyRsa
u.fieldMap["private_key_ed"] = u.PrivateKeyEd
}

View file

@ -47,9 +47,10 @@ type User struct {
Background *MediaMetadata ` json:"-"`
BackgroundId sql.NullString ` json:"background_id"` // ID of a media file used as background image
Banner *MediaMetadata ` json:"-"`
BannerId sql.NullString ` json:"banner_id"` // ID of a media file used as banner
Indexable bool ` json:"indexable"` // Whether this account can be found by crawlers
PublicKey []byte ` json:"public_key"` // The public key of the account
BannerId sql.NullString ` json:"banner_id"` // ID of a media file used as banner
Indexable bool ` json:"indexable"` // Whether this account can be found by crawlers
PublicKeyRsa []byte ` json:"public_key_rsa"` // The public RSA key of the account
PublicKeyEd []byte ` json:"public_key_ed"` // The public Ed25519 key of the account
// Whether this account restricts following
// If true, the owner must approve of a follow request first
RestrictedFollow bool ` json:"restricted_follow"`
@ -66,7 +67,8 @@ type User struct {
// saved space is worth
PasskeyId []byte `json:"-"`
FinishedRegistration bool `json:"-"` // Whether this account has completed registration yet
PrivateKey []byte `json:"-"`
PrivateKeyRsa []byte `json:"-"`
PrivateKeyEd []byte `json:"-"`
// ---- "Remote" linked values
InfoFields []UserInfoField `json:"-"`

View file

@ -2,8 +2,6 @@ package storage
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"database/sql"
"git.mstar.dev/mstar/goutils/other"
@ -11,6 +9,7 @@ import (
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/config"
"git.mstar.dev/mstar/linstrom/shared"
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
"git.mstar.dev/mstar/linstrom/storage-new/models"
)
@ -27,7 +26,7 @@ func InsertSelf() error {
if err != nil {
return other.Error("storage", "failed to save/update self server", err)
}
user, err := insertUser(server)
user, err := insertUser(server, duck)
if err != nil {
return other.Error("storage", "failed to save/update self user", err)
}
@ -95,7 +94,10 @@ func insertServer(duck *models.MediaMetadata) (*models.RemoteServer, error) {
return &server, nil
}
func insertUser(server *models.RemoteServer) (*models.User, error) {
func insertUser(
server *models.RemoteServer,
duckMedia *models.MediaMetadata,
) (*models.User, error) {
dbUser, err := dbgen.User.GetByUsername("linstrom")
if err == nil {
return dbUser, nil
@ -103,16 +105,14 @@ func insertUser(server *models.RemoteServer) (*models.User, error) {
if err != gorm.ErrRecordNotFound {
return nil, err
}
// publicKey, privateKey, err := ed25519.GenerateKey(nil)
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
publicEdKeyBytes, privateEdKeyBytes, err := shared.GenerateKeypair(true)
if err != nil {
return nil, err
}
if err = privateKey.Validate(); err != nil {
publicRsaKeyBytes, privateRsaKeyBytes, err := shared.GenerateKeypair(false)
if err != nil {
return nil, err
}
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
publicKeyBytes := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey)
pkeyId := make([]byte, 64)
_, err = rand.Read(pkeyId)
if err != nil {
@ -129,15 +129,17 @@ func insertUser(server *models.RemoteServer) (*models.User, error) {
DisplayName: config.GlobalConfig.Self.ServerActorDisplayName,
Description: "The default linstrom server user",
IsBot: true,
Icon: nil,
IconId: sql.NullString{Valid: false},
Icon: duckMedia,
IconId: sql.NullString{Valid: true, String: duckMedia.ID},
Background: nil,
BackgroundId: sql.NullString{Valid: false},
Banner: nil,
BannerId: sql.NullString{Valid: false},
Indexable: false,
PublicKey: publicKeyBytes,
PrivateKey: privateKeyBytes,
PublicKeyEd: publicEdKeyBytes,
PrivateKeyEd: privateEdKeyBytes,
PublicKeyRsa: publicRsaKeyBytes,
PrivateKeyRsa: privateRsaKeyBytes,
Verified: true,
FinishedRegistration: true,
PasskeyId: pkeyId,