mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package asymkey
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
// CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
|
|
func CheckPrincipalKeyString(ctx context.Context, user *user_model.User, content string) (_ string, err error) {
|
|
if setting.SSH.Disabled {
|
|
return "", db.ErrSSHDisabled{}
|
|
}
|
|
|
|
content = strings.TrimSpace(content)
|
|
if strings.ContainsAny(content, "\r\n") {
|
|
return "", util.NewInvalidArgumentErrorf("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":
|
|
emails, err := user_model.GetEmailAddresses(ctx, user.ID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for _, email := range emails {
|
|
if !email.IsActivated {
|
|
continue
|
|
}
|
|
if strings.EqualFold(content, email.LowerEmail) {
|
|
return content, nil
|
|
}
|
|
}
|
|
|
|
case "username":
|
|
if content == user.Name {
|
|
return content, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf("didn't match allowed principals: %s", setting.SSH.AuthorizedPrincipalsAllow)
|
|
}
|