Rename the main module to gotea.dev and the binary to gotea, and update the application name and English locale. Keep the external gitea.dev/sdk and gitea.dev/actionslib module imports unchanged. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package packages
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
user_model "gotea.dev/models/user"
|
|
"gotea.dev/modules/globallock"
|
|
"gotea.dev/modules/util"
|
|
)
|
|
|
|
// GetOrCreateKeyPair gets the owner's key pair used to sign repository files,
|
|
// generating and storing it if it does not exist yet.
|
|
func GetOrCreateKeyPair(ctx context.Context, ownerID int64, settingKeyPriv, settingKeyPub string, generate func() (priv, pub string, err error)) (string, string, error) {
|
|
priv, pub, err := getKeyPair(ctx, ownerID, settingKeyPriv, settingKeyPub)
|
|
if err != nil || (priv != "" && pub != "") {
|
|
return priv, pub, err
|
|
}
|
|
|
|
err = globallock.LockAndDo(ctx, fmt.Sprintf("pkg-keypair-%s-%d", settingKeyPriv, ownerID), func(ctx context.Context) error {
|
|
priv, pub, err = getKeyPair(ctx, ownerID, settingKeyPriv, settingKeyPub) // re-read inside the lock, another request may have created it
|
|
if err != nil || (priv != "" && pub != "") {
|
|
return err
|
|
}
|
|
|
|
if priv, pub, err = generate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := user_model.SetUserSetting(ctx, ownerID, settingKeyPriv, priv); err != nil {
|
|
return err
|
|
}
|
|
return user_model.SetUserSetting(ctx, ownerID, settingKeyPub, pub)
|
|
})
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return priv, pub, nil
|
|
}
|
|
|
|
func getKeyPair(ctx context.Context, ownerID int64, settingKeyPriv, settingKeyPub string) (string, string, error) {
|
|
priv, err := user_model.GetSetting(ctx, ownerID, settingKeyPriv)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return "", "", err
|
|
}
|
|
|
|
pub, err := user_model.GetSetting(ctx, ownerID, settingKeyPub)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return "", "", err
|
|
}
|
|
|
|
return priv, pub, nil
|
|
}
|