Personal Git forge.

Code git clone ssh://git.poy.ooo:6969/~poyo/farga ZIP
ssh/auth_test.go / 1KiB #e89c96f9
Raw
package sshimport (	"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])		}	})}