Lightweight and easily self-hostable Git forge. git.poy.ooo
git-forge selfhosting homebrew
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475package ssh
import ( "crypto/ed25519" "crypto/rand" "errors" "testing"
"farga/db/types"
"github.com/google/uuid" "golang.org/x/crypto/ssh")
type stubStore struct { usersByFingerprint map[string]types.User}
func (s stubStore) UserByFingerprint(fingerprint string) (types.User, error) { user, ok := s.usersByFingerprint[fingerprint] if !ok { return types.User{}, errors.New("no such key") } return user, nil}
func (s stubStore) Repo(repo types.Repo) (types.Repo, error) { return repo, nil}
func (s stubStore) UserHandle(handle string) (*types.User, error) { return nil, errors.New("no such user")}
func TestAuthenticatePublicKey(t *testing.T) { _, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } pub, err := ssh.NewPublicKey(priv.Public()) if err != nil { t.Fatal(err) }
t.Run("registered key", func(t *testing.T) { userID := uuid.New() store := stubStore{usersByFingerprint: map[string]types.User{ ssh.FingerprintSHA256(pub): {UUID_ID: types.UUID_ID{ID: userID}, Handle: "poyo"}, }}
perms, err := AuthenticatePublicKey(store, nil, pub) if err != nil { t.Fatalf("registered key should authenticate, got error: %v", err) } if got := perms.Extensions[userIDKey]; got != userID.String() { t.Errorf("userID extension = %q, want %q", got, userID.String()) } if perms.Extensions[isAnonymousKey] == "true" { t.Error("registered key must not be marked anonymous") } })
t.Run("unknown key authenticates as anonymous", func(t *testing.T) { store := stubStore{usersByFingerprint: map[string]types.User{}}
perms, err := AuthenticatePublicKey(store, nil, pub) if err != nil { t.Fatalf("unknown key should authenticate as anonymous, got error: %v", err) } if perms.Extensions[isAnonymousKey] != "true" { t.Errorf("is_anonymous extension = %q, want \"true\"", perms.Extensions[isAnonymousKey]) } })}