2021-07-24 15:46:34 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-10 13:44:24 +05:30
|
|
|
package asymkey
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-11 12:33:30 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 15:46:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// __________ .__ .__ .__
|
|
|
|
// \______ _______|__| ____ ____ |_____________ | | ______
|
|
|
|
// | ___\_ __ | |/ \_/ ___\| \____ \__ \ | | / ___/
|
|
|
|
// | | | | \| | | \ \___| | |_> / __ \| |__\___ \
|
|
|
|
// |____| |__| |__|___| /\___ |__| __(____ |____/____ >
|
|
|
|
// \/ \/ |__| \/ \/
|
|
|
|
//
|
|
|
|
// This file contains functions related to principals
|
|
|
|
|
|
|
|
// AddPrincipalKey adds new principal to database and authorized_principals file.
|
2022-01-02 18:42:35 +05:30
|
|
|
func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) {
|
2022-11-13 01:48:50 +05:30
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2021-11-21 21:11:00 +05:30
|
|
|
if err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
defer committer.Close()
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
// Principals cannot be duplicated.
|
2022-05-20 19:38:52 +05:30
|
|
|
has, err := db.GetEngine(ctx).
|
2021-07-24 15:46:34 +05:30
|
|
|
Where("content = ? AND type = ?", content, KeyTypePrincipal).
|
|
|
|
Get(new(PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if has {
|
|
|
|
return nil, ErrKeyAlreadyExist{0, "", content}
|
|
|
|
}
|
|
|
|
|
|
|
|
key := &PublicKey{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Name: content,
|
|
|
|
Content: content,
|
2021-11-28 17:28:28 +05:30
|
|
|
Mode: perm.AccessModeWrite,
|
2021-07-24 15:46:34 +05:30
|
|
|
Type: KeyTypePrincipal,
|
2022-01-02 18:42:35 +05:30
|
|
|
LoginSourceID: authSourceID,
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2022-05-20 19:38:52 +05:30
|
|
|
if err = db.Insert(ctx, key); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("addKey: %w", err)
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
if err = committer.Commit(); err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
committer.Close()
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
return key, RewriteAllPrincipalKeys(db.DefaultContext)
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
|
2021-11-24 15:19:20 +05:30
|
|
|
func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, err error) {
|
2021-07-24 15:46:34 +05:30
|
|
|
if setting.SSH.Disabled {
|
2021-12-10 13:44:24 +05:30
|
|
|
return "", db.ErrSSHDisabled{}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
content = strings.TrimSpace(content)
|
|
|
|
if strings.ContainsAny(content, "\r\n") {
|
|
|
|
return "", errors.New("only a single line with a single principal please")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check all the allowed principals, email, username or anything
|
|
|
|
// if any matches, return ok
|
|
|
|
for _, v := range setting.SSH.AuthorizedPrincipalsAllow {
|
|
|
|
switch v {
|
|
|
|
case "anything":
|
|
|
|
return content, nil
|
|
|
|
case "email":
|
2021-11-11 12:33:30 +05:30
|
|
|
emails, err := user_model.GetEmailAddresses(user.ID)
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, email := range emails {
|
|
|
|
if !email.IsActivated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if content == email.Email {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "username":
|
|
|
|
if content == user.Name {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("didn't match allowed principals: %s", setting.SSH.AuthorizedPrincipalsAllow)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPrincipalKeys returns a list of principals belongs to given user.
|
2021-09-24 17:02:56 +05:30
|
|
|
func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal)
|
2021-07-24 15:46:34 +05:30
|
|
|
if listOptions.Page != 0 {
|
2021-09-24 17:02:56 +05:30
|
|
|
sess = db.SetSessionPagination(sess, &listOptions)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
keys := make([]*PublicKey, 0, listOptions.PageSize)
|
|
|
|
return keys, sess.Find(&keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]*PublicKey, 0, 5)
|
|
|
|
return keys, sess.Find(&keys)
|
|
|
|
}
|