Copied from internal/domain/identity, internal/domain/org, internal/domain/access, internal/infrastructure/access and internal/infrastructure/postgres.
124 lines
4.4 KiB
Go
124 lines
4.4 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestAuthenticationSettingsRejectUntrustedOrigins(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, url, issuer, client, audience string
|
|
want bool
|
|
}{
|
|
{name: "public TLS", url: "https://archie.example", want: true},
|
|
{name: "localhost", url: "http://localhost:8484", want: true},
|
|
{name: "userinfo is not an origin", url: "https://user:password@archie.example", want: false}, //nolint:gosec // G101: a fake URL with userinfo, which must be refused.
|
|
{name: "URL path cannot change the RP origin", url: "https://archie.example/subpath", want: false},
|
|
{name: "query is not an origin", url: "https://archie.example?token=1", want: false},
|
|
{name: "complete provider", url: "https://archie.example", issuer: "https://identity.example", client: "archie", audience: "archie", want: true},
|
|
{name: "remote insecure provider", url: "https://archie.example", issuer: "http://identity.example", client: "archie", audience: "archie", want: false},
|
|
{name: "issuer without client", url: "https://archie.example", issuer: "https://identity.example", audience: "archie", want: false},
|
|
{name: "audience without provider", url: "https://archie.example", audience: "archie", want: false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := (AuthenticationSettings{PublicURL: tc.url, OIDCIssuer: tc.issuer, OIDCClientID: tc.client, OIDCAudience: tc.audience}).Validate()
|
|
if (err == nil) != tc.want {
|
|
t.Fatalf("validation err=%v, allowed=%v", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPasswordVerification(t *testing.T) {
|
|
hash, err := HashPassword("a sufficiently long password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name, password, hash string
|
|
want bool
|
|
}{
|
|
{"correct", "a sufficiently long password", hash, true},
|
|
{"wrong", "a different long password", hash, false},
|
|
{"malformed", "a sufficiently long password", "bad", false},
|
|
{"unbounded parameters", "password", "$argon2id$v=19$m=999999999,t=2,p=1$c2FsdA$aGFzaA", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := VerifyPassword(tc.password, tc.hash); got != tc.want {
|
|
t.Fatalf("verification = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
for _, password := range []string{"short", strings.Repeat("a", 1025), "123456789012345", "passwordpassword"} {
|
|
if _, err := HashPassword(password); err == nil {
|
|
t.Fatal("accepted invalid password length")
|
|
}
|
|
}
|
|
second, err := HashPassword("a sufficiently long password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hash == second {
|
|
t.Fatal("password hashes reuse salts")
|
|
}
|
|
}
|
|
|
|
func TestStableIDIsRFC4122(t *testing.T) {
|
|
for _, name := range []string{"agent", "operator", "archie", "Some Bot", ""} {
|
|
got := string(StableID(name))
|
|
id, err := uuid.Parse(got)
|
|
if err != nil || id.String() != got || id.Version() != 5 || id.Variant() != uuid.RFC4122 {
|
|
t.Errorf("StableID(%q) = %q, not an RFC 4122 v5 UUID (err %v)", name, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type tokenFake struct {
|
|
PersonalTokenStore
|
|
token PersonalToken
|
|
touched int
|
|
}
|
|
|
|
func (f *tokenFake) PersonalTokenOf(context.Context, Subject) (PersonalToken, error) {
|
|
return f.token, nil
|
|
}
|
|
|
|
func (f *tokenFake) TouchPersonalToken(context.Context, Subject, time.Time) error {
|
|
f.touched++
|
|
return nil
|
|
}
|
|
|
|
// TestUsePersonalToken pins that an expired token is refused and that last use
|
|
// is written at most once a minute.
|
|
func TestUsePersonalToken(t *testing.T) {
|
|
now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
|
|
at := func(d time.Duration) *time.Time { v := now.Add(d); return &v }
|
|
tests := []struct {
|
|
name string
|
|
token PersonalToken
|
|
wantErr error
|
|
wantTouched int
|
|
}{
|
|
{"valid", PersonalToken{ExpiresAt: at(time.Hour)}, nil, 1},
|
|
{"expired", PersonalToken{ExpiresAt: at(-time.Second)}, ErrIdentityNotFound, 0},
|
|
{"expires now", PersonalToken{ExpiresAt: at(0)}, ErrIdentityNotFound, 0},
|
|
{"no expiry", PersonalToken{}, nil, 1},
|
|
{"used seconds ago", PersonalToken{LastUsedAt: at(-10 * time.Second)}, nil, 0},
|
|
{"used minutes ago", PersonalToken{LastUsedAt: at(-2 * time.Minute)}, nil, 1},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := &tokenFake{token: tt.token}
|
|
err := UsePersonalToken(t.Context(), f, Subject{}, now)
|
|
if !errors.Is(err, tt.wantErr) || f.touched != tt.wantTouched {
|
|
t.Fatalf("err %v touched %d, want %v and %d", err, f.touched, tt.wantErr, tt.wantTouched)
|
|
}
|
|
})
|
|
}
|
|
}
|