mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.5 KiB
130 lines
3.5 KiB
3 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package asymkey
|
||
3 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/perm"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
|
)
|
||
|
|
||
|
// __________ .__ .__ .__
|
||
|
// \______ _______|__| ____ ____ |_____________ | | ______
|
||
|
// | ___\_ __ | |/ \_/ ___\| \____ \__ \ | | / ___/
|
||
|
// | | | | \| | | \ \___| | |_> / __ \| |__\___ \
|
||
|
// |____| |__| |__|___| /\___ |__| __(____ |____/____ >
|
||
|
// \/ \/ |__| \/ \/
|
||
|
//
|
||
|
// This file contains functions related to principals
|
||
|
|
||
|
// AddPrincipalKey adds new principal to database and authorized_principals file.
|
||
|
func AddPrincipalKey(ownerID int64, content string, loginSourceID int64) (*PublicKey, error) {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
3 years ago
|
return nil, err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
|
sess := db.GetEngine(ctx)
|
||
3 years ago
|
|
||
|
// Principals cannot be duplicated.
|
||
|
has, err := sess.
|
||
|
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,
|
||
3 years ago
|
Mode: perm.AccessModeWrite,
|
||
3 years ago
|
Type: KeyTypePrincipal,
|
||
|
LoginSourceID: loginSourceID,
|
||
|
}
|
||
|
if err = addPrincipalKey(sess, key); err != nil {
|
||
|
return nil, fmt.Errorf("addKey: %v", err)
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = committer.Commit(); err != nil {
|
||
3 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
committer.Close()
|
||
3 years ago
|
|
||
|
return key, RewriteAllPrincipalKeys()
|
||
|
}
|
||
|
|
||
3 years ago
|
func addPrincipalKey(e db.Engine, key *PublicKey) (err error) {
|
||
3 years ago
|
// Save Key representing a principal.
|
||
|
if _, err = e.Insert(key); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
|
||
3 years ago
|
func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, err error) {
|
||
3 years ago
|
if setting.SSH.Disabled {
|
||
3 years ago
|
return "", db.ErrSSHDisabled{}
|
||
3 years ago
|
}
|
||
|
|
||
|
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":
|
||
3 years ago
|
emails, err := user_model.GetEmailAddresses(user.ID)
|
||
3 years ago
|
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.
|
||
3 years ago
|
func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
|
||
3 years ago
|
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal)
|
||
3 years ago
|
if listOptions.Page != 0 {
|
||
3 years ago
|
sess = db.SetSessionPagination(sess, &listOptions)
|
||
3 years ago
|
|
||
|
keys := make([]*PublicKey, 0, listOptions.PageSize)
|
||
|
return keys, sess.Find(&keys)
|
||
|
}
|
||
|
|
||
|
keys := make([]*PublicKey, 0, 5)
|
||
|
return keys, sess.Find(&keys)
|
||
|
}
|