package database

import (
	"context"
	"encoding/base64"
	"encoding/binary"
	"fmt"
	"math/big"
	"os"
	"path"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/cockroachdb/errors"
	"github.com/unknwon/com"
	"golang.org/x/crypto/ssh"
	"gorm.io/gorm"
	log "unknwon.dev/clog/v2"

	"gogs.io/gogs/internal/conf"
	"gogs.io/gogs/internal/errutil"
	"gogs.io/gogs/internal/process"
)

const (
	tplPublicKey = `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
)

var sshOpLocker sync.Mutex

type KeyType int

const (
	KeyTypeUser = iota + 1
	KeyTypeDeploy
)

// PublicKey represents a user or deploy SSH public key.
type PublicKey struct {
	ID          int64      `gorm:"primaryKey"`
	OwnerID     int64      `gorm:"index;not null"`
	Name        string     `gorm:"not null"`
	Fingerprint string     `gorm:"not null"`
	Content     string     `gorm:"type:text;not null"`
	Mode        AccessMode `gorm:"not null;default:2"`
	Type        KeyType    `gorm:"not null;default:1"`

	Created           time.Time `gorm:"-" json:"-"`
	CreatedUnix       int64
	Updated           time.Time `gorm:"-" json:"-"` // Note: Updated must below Created for AfterFind.
	UpdatedUnix       int64
	HasRecentActivity bool `gorm:"-" json:"-"`
	HasUsed           bool `gorm:"-" json:"-"`
}

func (k *PublicKey) BeforeCreate(tx *gorm.DB) error {
	if k.CreatedUnix == 0 {
		k.CreatedUnix = tx.NowFunc().Unix()
	}
	return nil
}

func (k *PublicKey) BeforeUpdate(tx *gorm.DB) error {
	k.UpdatedUnix = tx.NowFunc().Unix()
	return nil
}

func (k *PublicKey) AfterFind(tx *gorm.DB) error {
	k.Created = time.Unix(k.CreatedUnix, 0).Local()
	if k.UpdatedUnix > 0 {
		k.Updated = time.Unix(k.UpdatedUnix, 0).Local()
		k.HasUsed = k.Updated.After(k.Created)
		k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(tx.NowFunc())
	}
	return nil
}

// OmitEmail returns content of public key without email address.
func (k *PublicKey) OmitEmail() string {
	return strings.Join(strings.Split(k.Content, " ")[:2], " ")
}

// AuthorizedString returns formatted public key string for authorized_keys file.
func (k *PublicKey) AuthorizedString() string {
	return fmt.Sprintf(tplPublicKey, conf.AppPath(), k.ID, conf.CustomConf, k.Content)
}

// IsDeployKey returns true if the public key is used as deploy key.
func (k *PublicKey) IsDeployKey() bool {
	return k.Type == KeyTypeDeploy
}

func extractTypeFromBase64Key(key string) (string, error) {
	b, err := base64.StdEncoding.DecodeString(key)
	if err != nil || len(b) < 4 {
		return "", errors.Newf("invalid key format: %v", err)
	}

	keyLength := int(binary.BigEndian.Uint32(b))
	if len(b) < 4+keyLength {
		return "", errors.Newf("invalid key format: not enough length %d", keyLength)
	}

	return string(b[4 : 4+keyLength]), nil
}

// parseKeyString parses any key string in OpenSSH or SSH2 format to clean OpenSSH string (RFC4253).
func parseKeyString(content string) (string, error) {
	// Transform all legal line endings to a single "\n"

	// Replace all windows full new lines ("\r\n")
	content = strings.ReplaceAll(content, "\r\n", "\n")

	// Replace all windows half new lines ("\r"), if it happen not to match replace above
	content = strings.ReplaceAll(content, "\r", "\n")

	// Replace ending new line as its may cause unwanted behaviour (extra line means not a single line key | OpenSSH key)
	content = strings.TrimRight(content, "\n")

	// split lines
	lines := strings.Split(content, "\n")

	var keyType, keyContent, keyComment string

	if len(lines) == 1 {
		// Parse OpenSSH format.
		parts := strings.SplitN(lines[0], " ", 3)
		switch len(parts) {
		case 0:
			return "", errors.New("empty key")
		case 1:
			keyContent = parts[0]
		case 2:
			keyType = parts[0]
			keyContent = parts[1]
		default:
			keyType = parts[0]
			keyContent = parts[1]
			keyComment = parts[2]
		}

		// If keyType is not given, extract it from content. If given, validate it.
		t, err := extractTypeFromBase64Key(keyContent)
		if err != nil {
			return "", errors.Newf("extractTypeFromBase64Key: %v", err)
		}
		if keyType == "" {
			keyType = t
		} else if keyType != t {
			return "", errors.Newf("key type and content does not match: %s - %s", keyType, t)
		}
	} else {
		// Parse SSH2 file format.
		continuationLine := false

		for _, line := range lines {
			// Skip lines that:
			// 1) are a continuation of the previous line,
			// 2) contain ":" as that are comment lines
			// 3) contain "-" as that are begin and end tags
			if continuationLine || strings.ContainsAny(line, ":-") {
				continuationLine = strings.HasSuffix(line, "\\")
			} else {
				keyContent = keyContent + line
			}
		}

		t, err := extractTypeFromBase64Key(keyContent)
		if err != nil {
			return "", errors.Newf("extractTypeFromBase64Key: %v", err)
		}
		keyType = t
	}
	return keyType + " " + keyContent + " " + keyComment, nil
}

// writeTmpKeyFile writes key content to a temporary file
// and returns the name of that file, along with any possible errors.
func writeTmpKeyFile(content string) (string, error) {
	tmpFile, err := os.CreateTemp(conf.SSH.KeyTestPath, "gogs_keytest")
	if err != nil {
		return "", errors.Newf("TempFile: %v", err)
	}
	defer tmpFile.Close()

	if _, err = tmpFile.WriteString(content); err != nil {
		return "", errors.Newf("WriteString: %v", err)
	}
	return tmpFile.Name(), nil
}

// SSHKeyGenParsePublicKey extracts key type and length using ssh-keygen.
func SSHKeyGenParsePublicKey(key string) (string, int, error) {
	tmpName, err := writeTmpKeyFile(key)
	if err != nil {
		return "", 0, errors.Newf("writeTmpKeyFile: %v", err)
	}
	defer os.Remove(tmpName)

	stdout, stderr, err := process.Exec("SSHKeyGenParsePublicKey", conf.SSH.KeygenPath, "-lf", tmpName)
	if err != nil {
		return "", 0, errors.Newf("fail to parse public key: %s - %s", err, stderr)
	}
	if strings.Contains(stdout, "is not a public key file") {
		return "", 0, ErrKeyUnableVerify{stdout}
	}

	fields := strings.Split(stdout, " ")
	if len(fields) < 4 {
		return "", 0, errors.Newf("invalid public key line: %s", stdout)
	}

	keyType := strings.Trim(fields[len(fields)-1], "()\r\n")
	return strings.ToLower(keyType), com.StrTo(fields[0]).MustInt(), nil
}

// SSHNativeParsePublicKey extracts the key type and length using the golang SSH library.
func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
	fields := strings.Fields(keyLine)
	if len(fields) < 2 {
		return "", 0, errors.Newf("not enough fields in public key line: %s", keyLine)
	}

	raw, err := base64.StdEncoding.DecodeString(fields[1])
	if err != nil {
		return "", 0, err
	}

	pkey, err := ssh.ParsePublicKey(raw)
	if err != nil {
		if strings.Contains(err.Error(), "ssh: unknown key algorithm") {
			return "", 0, ErrKeyUnableVerify{err.Error()}
		}
		return "", 0, errors.Newf("ParsePublicKey: %v", err)
	}

	// The ssh library can parse the key, so next we find out what key exactly we have.
	switch pkey.Type() {
	case ssh.KeyAlgoDSA:
		rawPub := struct {
			Name       string
			P, Q, G, Y *big.Int
		}{}
		if err := ssh.Unmarshal(pkey.Marshal(), &rawPub); err != nil {
			return "", 0, err
		}
		// as per https://bugzilla.mindrot.org/show_bug.cgi?id=1647 we should never
		// see dsa keys != 1024 bit, but as it seems to work, we will not check here
		return "dsa", rawPub.P.BitLen(), nil // use P as per crypto/dsa/dsa.go (is L)
	case ssh.KeyAlgoRSA:
		rawPub := struct {
			Name string
			E    *big.Int
			N    *big.Int
		}{}
		if err := ssh.Unmarshal(pkey.Marshal(), &rawPub); err != nil {
			return "", 0, err
		}
		return "rsa", rawPub.N.BitLen(), nil // use N as per crypto/rsa/rsa.go (is bits)
	case ssh.KeyAlgoECDSA256:
		return "ecdsa", 256, nil
	case ssh.KeyAlgoECDSA384:
		return "ecdsa", 384, nil
	case ssh.KeyAlgoECDSA521:
		return "ecdsa", 521, nil
	case ssh.KeyAlgoED25519:
		return "ed25519", 256, nil
	}
	return "", 0, errors.Newf("unsupported key length detection for type: %s", pkey.Type())
}

// CheckPublicKeyString checks if the given public key string is recognized by SSH.
// It returns the actual public key line on success.
func CheckPublicKeyString(content string) (_ string, err error) {
	if conf.SSH.Disabled {
		return "", errors.New("SSH is disabled")
	}

	content, err = parseKeyString(content)
	if err != nil {
		return "", err
	}

	content = strings.TrimRight(content, "\n\r")
	if strings.ContainsAny(content, "\n\r") {
		return "", errors.New("only a single line with a single key please")
	}

	// Remove any unnecessary whitespace
	content = strings.TrimSpace(content)

	if !conf.SSH.MinimumKeySizeCheck {
		return content, nil
	}

	var (
		fnName  string
		keyType string
		length  int
	)
	if conf.SSH.StartBuiltinServer {
		fnName = "SSHNativeParsePublicKey"
		keyType, length, err = SSHNativeParsePublicKey(content)
	} else {
		fnName = "SSHKeyGenParsePublicKey"
		keyType, length, err = SSHKeyGenParsePublicKey(content)
	}
	if err != nil {
		return "", errors.Newf("%s: %v", fnName, err)
	}
	log.Trace("Key info [native: %v]: %s-%d", conf.SSH.StartBuiltinServer, keyType, length)

	if minLen, found := conf.SSH.MinimumKeySizes[keyType]; found && length >= minLen {
		return content, nil
	} else if found && length < minLen {
		return "", errors.Newf("key length is not enough: got %d, needs %d", length, minLen)
	}
	return "", errors.Newf("key type is not allowed: %s", keyType)
}

// appendAuthorizedKeysToFile appends new SSH keys' content to authorized_keys file.
func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
	sshOpLocker.Lock()
	defer sshOpLocker.Unlock()

	fpath := filepath.Join(conf.SSH.RootPath, "authorized_keys")
	f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
	if err != nil {
		return err
	}
	defer f.Close()

	// Note: chmod command does not support in Windows.
	if !conf.IsWindowsRuntime() {
		fi, err := f.Stat()
		if err != nil {
			return err
		}

		// .ssh directory should have mode 700, and authorized_keys file should have mode 600.
		if fi.Mode().Perm() > 0o600 {
			log.Error("authorized_keys file has unusual permission flags: %s - setting to -rw-------", fi.Mode().Perm().String())
			if err = f.Chmod(0o600); err != nil {
				return err
			}
		}
	}

	for _, key := range keys {
		if _, err = f.WriteString(key.AuthorizedString()); err != nil {
			return err
		}
	}
	return nil
}

// checkKeyContent onlys checks if key content has been used as public key,
// it is OK to use same key as deploy key for multiple repositories/users.
func checkKeyContent(content string) error {
	err := db.Where("content = ? AND type = ?", content, KeyTypeUser).First(&PublicKey{}).Error
	if err == nil {
		return ErrKeyAlreadyExist{0, content}
	} else if !errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}
	return nil
}

func addKey(tx *gorm.DB, key *PublicKey) (err error) {
	// Calculate fingerprint.
	tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/")
	_ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
	if err = os.WriteFile(tmpPath, []byte(key.Content), 0o644); err != nil {
		return err
	}

	stdout, stderr, err := process.Exec("AddPublicKey", conf.SSH.KeygenPath, "-lf", tmpPath)
	if err != nil {
		return errors.Newf("fail to parse public key: %s - %s", err, stderr)
	} else if len(stdout) < 2 {
		return errors.New("not enough output for calculating fingerprint: " + stdout)
	}
	key.Fingerprint = strings.Split(stdout, " ")[1]

	// Save SSH key.
	if err = tx.Create(key).Error; err != nil {
		return err
	}

	// Don't need to rewrite this file if builtin SSH server is enabled.
	if conf.SSH.StartBuiltinServer {
		return nil
	}
	return appendAuthorizedKeysToFile(key)
}

// AddPublicKey adds new public key to database and authorized_keys file.
func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {
	log.Trace("Add public key: %s", content)
	if err := checkKeyContent(content); err != nil {
		return nil, err
	}

	// Key name of same user cannot be duplicated.
	err := db.Where("owner_id = ? AND name = ?", ownerID, name).First(new(PublicKey)).Error
	if err == nil {
		return nil, ErrKeyNameAlreadyUsed{ownerID, name}
	} else if !errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, err
	}

	key := &PublicKey{
		OwnerID: ownerID,
		Name:    name,
		Content: content,
		Mode:    AccessModeWrite,
		Type:    KeyTypeUser,
	}
	err = db.Transaction(func(tx *gorm.DB) error {
		return addKey(tx, key)
	})
	if err != nil {
		return nil, errors.Newf("addKey: %v", err)
	}

	return key, nil
}

// GetPublicKeyByID returns public key by given ID.
func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
	key := new(PublicKey)
	err := db.Where("id = ?", keyID).First(key).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, ErrKeyNotExist{keyID}
		}
		return nil, err
	}
	return key, nil
}

// SearchPublicKeyByContent searches a public key using the content as prefix
// (i.e. ignore the email part). It returns ErrKeyNotExist if no such key
// exists.
func SearchPublicKeyByContent(content string) (*PublicKey, error) {
	key := new(PublicKey)
	err := db.Where("content LIKE ?", content+"%").First(key).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, ErrKeyNotExist{}
		}
		return nil, err
	}
	return key, nil
}

// ListPublicKeys returns a list of public keys belongs to given user.
func ListPublicKeys(uid int64) ([]*PublicKey, error) {
	keys := make([]*PublicKey, 0, 5)
	return keys, db.Where("owner_id = ?", uid).Find(&keys).Error
}

// UpdatePublicKey updates given public key.
func UpdatePublicKey(key *PublicKey) error {
	return db.Model(key).Where("id = ?", key.ID).Updates(key).Error
}

// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
func deletePublicKeys(tx *gorm.DB, keyIDs ...int64) error {
	if len(keyIDs) == 0 {
		return nil
	}

	return tx.Where("id IN ?", keyIDs).Delete(new(PublicKey)).Error
}

// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
func DeletePublicKey(doer *User, id int64) (err error) {
	key, err := GetPublicKeyByID(id)
	if err != nil {
		if IsErrKeyNotExist(err) {
			return nil
		}
		return errors.Newf("GetPublicKeyByID: %v", err)
	}

	// Check if user has access to delete this key.
	if !doer.IsAdmin && doer.ID != key.OwnerID {
		return ErrKeyAccessDenied{doer.ID, key.ID, "public"}
	}

	err = db.Transaction(func(tx *gorm.DB) error {
		return deletePublicKeys(tx, id)
	})
	if err != nil {
		return err
	}

	return RewriteAuthorizedKeys()
}

// RewriteAuthorizedKeys removes any authorized key and rewrite all keys from database again.
// Note: x.Iterate does not get latest data after insert/delete, so we have to call this function
// outside any session scope independently.
//
// Deprecated: Use PublicKeys.RewriteAuthorizedKeys instead.
func RewriteAuthorizedKeys() error {
	sshOpLocker.Lock()
	defer sshOpLocker.Unlock()

	log.Trace("Doing: RewriteAuthorizedKeys")

	_ = os.MkdirAll(conf.SSH.RootPath, os.ModePerm)
	fpath := authorizedKeysPath()
	tmpPath := fpath + ".tmp"
	f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
	if err != nil {
		return err
	}
	defer os.Remove(tmpPath)

	err = x.Iterate(new(PublicKey), func(idx int, bean any) (err error) {
		_, err = f.WriteString((bean.(*PublicKey)).AuthorizedString())
		return err
	})
	_ = f.Close()
	if err != nil {
		return err
	}

	if com.IsExist(fpath) {
		if err = os.Remove(fpath); err != nil {
			return err
		}
	}
	if err = os.Rename(tmpPath, fpath); err != nil {
		return err
	}

	return nil
}

// ________                .__                 ____  __.
// \______ \   ____ ______ |  |   ____ ___.__.|    |/ _|____ ___.__.
//  |    |  \_/ __ \\____ \|  |  /  _ <   |  ||      <_/ __ <   |  |
//  |    `   \  ___/|  |_> >  |_(  <_> )___  ||    |  \  ___/\___  |
// /_______  /\___  >   __/|____/\____// ____||____|__ \___  > ____|
//         \/     \/|__|               \/             \/   \/\/

// DeployKey represents deploy key information and its relation with repository.
type DeployKey struct {
	ID          int64
	KeyID       int64 `gorm:"uniqueIndex:s;index"`
	RepoID      int64 `gorm:"uniqueIndex:s;index"`
	Name        string
	Fingerprint string
	Content     string `gorm:"-" json:"-"`

	Created           time.Time `gorm:"-" json:"-"`
	CreatedUnix       int64
	Updated           time.Time `gorm:"-" json:"-"` // Note: Updated must below Created for AfterFind.
	UpdatedUnix       int64
	HasRecentActivity bool `gorm:"-" json:"-"`
	HasUsed           bool `gorm:"-" json:"-"`
}

func (k *DeployKey) BeforeCreate(tx *gorm.DB) error {
	if k.CreatedUnix == 0 {
		k.CreatedUnix = tx.NowFunc().Unix()
	}
	return nil
}

func (k *DeployKey) BeforeUpdate(tx *gorm.DB) error {
	k.UpdatedUnix = tx.NowFunc().Unix()
	return nil
}

func (k *DeployKey) AfterFind(tx *gorm.DB) error {
	k.Created = time.Unix(k.CreatedUnix, 0).Local()
	if k.UpdatedUnix > 0 {
		k.Updated = time.Unix(k.UpdatedUnix, 0).Local()
		k.HasUsed = k.Updated.After(k.Created)
		k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(tx.NowFunc())
	}
	return nil
}

// GetContent gets associated public key content.
func (k *DeployKey) GetContent() error {
	pkey, err := GetPublicKeyByID(k.KeyID)
	if err != nil {
		return err
	}
	k.Content = pkey.Content
	return nil
}

func checkDeployKey(tx *gorm.DB, keyID, repoID int64, name string) error {
	// Note: We want error detail, not just true or false here.
	err := tx.Where("key_id = ? AND repo_id = ?", keyID, repoID).First(new(DeployKey)).Error
	if err == nil {
		return ErrDeployKeyAlreadyExist{keyID, repoID}
	} else if !errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}

	err = tx.Where("repo_id = ? AND name = ?", repoID, name).First(new(DeployKey)).Error
	if err == nil {
		return ErrDeployKeyNameAlreadyUsed{repoID, name}
	} else if !errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}

	return nil
}

// addDeployKey adds new key-repo relation.
func addDeployKey(tx *gorm.DB, keyID, repoID int64, name, fingerprint string) (*DeployKey, error) {
	if err := checkDeployKey(tx, keyID, repoID, name); err != nil {
		return nil, err
	}

	key := &DeployKey{
		KeyID:       keyID,
		RepoID:      repoID,
		Name:        name,
		Fingerprint: fingerprint,
	}
	err := tx.Create(key).Error
	return key, err
}

// HasDeployKey returns true if public key is a deploy key of given repository.
func HasDeployKey(keyID, repoID int64) bool {
	err := db.Where("key_id = ? AND repo_id = ?", keyID, repoID).First(new(DeployKey)).Error
	return err == nil
}

// AddDeployKey add new deploy key to database and authorized_keys file.
func AddDeployKey(repoID int64, name, content string) (*DeployKey, error) {
	if err := checkKeyContent(content); err != nil {
		return nil, err
	}

	pkey := &PublicKey{
		Content: content,
		Mode:    AccessModeRead,
		Type:    KeyTypeDeploy,
	}
	err := db.Where("content = ? AND mode = ? AND type = ?", content, AccessModeRead, KeyTypeDeploy).First(pkey).Error
	has := err == nil
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, err
	}

	var key *DeployKey
	err = db.Transaction(func(tx *gorm.DB) error {
		// First time use this deploy key.
		if !has {
			if err := addKey(tx, pkey); err != nil {
				return errors.Newf("addKey: %v", err)
			}
		}

		var err error
		key, err = addDeployKey(tx, pkey.ID, repoID, name, pkey.Fingerprint)
		if err != nil {
			return errors.Newf("addDeployKey: %v", err)
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return key, nil
}

var _ errutil.NotFound = (*ErrDeployKeyNotExist)(nil)

type ErrDeployKeyNotExist struct {
	args map[string]any
}

func IsErrDeployKeyNotExist(err error) bool {
	_, ok := err.(ErrDeployKeyNotExist)
	return ok
}

func (err ErrDeployKeyNotExist) Error() string {
	return fmt.Sprintf("deploy key does not exist: %v", err.args)
}

func (ErrDeployKeyNotExist) NotFound() bool {
	return true
}

// GetDeployKeyByID returns deploy key by given ID.
func GetDeployKeyByID(id int64) (*DeployKey, error) {
	key := new(DeployKey)
	err := db.Where("id = ?", id).First(key).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, ErrDeployKeyNotExist{args: map[string]any{"deployKeyID": id}}
		}
		return nil, err
	}
	return key, nil
}

// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
	key := &DeployKey{
		KeyID:  keyID,
		RepoID: repoID,
	}
	err := db.Where("key_id = ? AND repo_id = ?", keyID, repoID).First(key).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, ErrDeployKeyNotExist{args: map[string]any{"keyID": keyID, "repoID": repoID}}
		}
		return nil, err
	}
	return key, nil
}

// UpdateDeployKey updates deploy key information.
func UpdateDeployKey(key *DeployKey) error {
	return db.Model(key).Where("id = ?", key.ID).Updates(key).Error
}

// DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
func DeleteDeployKey(doer *User, id int64) error {
	key, err := GetDeployKeyByID(id)
	if err != nil {
		if IsErrDeployKeyNotExist(err) {
			return nil
		}
		return errors.Newf("GetDeployKeyByID: %v", err)
	}

	// Check if user has access to delete this key.
	if !doer.IsAdmin {
		repo, err := GetRepositoryByID(key.RepoID)
		if err != nil {
			return errors.Newf("GetRepositoryByID: %v", err)
		}
		if !Handle.Permissions().Authorize(context.TODO(), doer.ID, repo.ID, AccessModeAdmin,
			AccessModeOptions{
				OwnerID: repo.OwnerID,
				Private: repo.IsPrivate,
			},
		) {
			return ErrKeyAccessDenied{doer.ID, key.ID, "deploy"}
		}
	}

	return db.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id = ?", key.ID).Delete(new(DeployKey)).Error; err != nil {
			return errors.Newf("delete deploy key [%d]: %v", key.ID, err)
		}

		// Check if this is the last reference to same key content.
		err := tx.Where("key_id = ?", key.KeyID).First(new(DeployKey)).Error
		if errors.Is(err, gorm.ErrRecordNotFound) {
			if err = deletePublicKeys(tx, key.KeyID); err != nil {
				return err
			}
		} else if err != nil {
			return err
		}

		return nil
	})
}

// ListDeployKeys returns all deploy keys by given repository ID.
func ListDeployKeys(repoID int64) ([]*DeployKey, error) {
	keys := make([]*DeployKey, 0, 5)
	return keys, db.Where("repo_id = ?", repoID).Find(&keys).Error
}
