139 lines
6.3 KiB
Go
139 lines
6.3 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
|
)
|
|
|
|
type accountBoundaryStore struct {
|
|
identity.Accounts
|
|
identity.AccountSecurity
|
|
fresh bool
|
|
}
|
|
|
|
func (accountBoundaryStore) AccountSetupRequired(context.Context) (bool, error) { return false, nil }
|
|
|
|
func (accountBoundaryStore) ResolveAccountSession(_ context.Context, token string) (identity.Identity, error) {
|
|
if token != "valid" {
|
|
return identity.Identity{}, identity.ErrCredentialRejected
|
|
}
|
|
return identity.Identity{ID: "alice", Kind: identity.KindUser, DisplayName: "Alice", Lifecycle: identity.LifecycleActive}, nil
|
|
}
|
|
|
|
func (accountBoundaryStore) SignInAccount(_ context.Context, email, password string) (identity.AccountSession, error) {
|
|
if email != "alice@example.com" || password != "a sufficiently long password" {
|
|
return identity.AccountSession{}, identity.ErrCredentialRejected
|
|
}
|
|
return identity.AccountSession{Identity: identity.Identity{ID: "alice", Kind: identity.KindUser, Lifecycle: identity.LifecycleActive}, Token: "new-session", Expires: time.Now().Add(time.Hour)}, nil
|
|
}
|
|
|
|
func (s accountBoundaryStore) RequireFreshAccountSession(context.Context, string) error {
|
|
if !s.fresh {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (accountBoundaryStore) CreateAccountRecovery(context.Context) (string, error) {
|
|
return "one-use-recovery-code", nil
|
|
}
|
|
|
|
func TestAccountBoundary(t *testing.T) {
|
|
principal := access.Principal{IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: org.RoleViewer}}}
|
|
for _, tc := range []struct {
|
|
name, method, path, body, cookie, csrf string
|
|
fresh bool
|
|
want int
|
|
}{
|
|
{name: "shared token cannot authenticate", method: "GET", path: "/api/summary", cookie: "legacy", want: 401},
|
|
{name: "API reads reach authentication", method: "GET", path: "/api/chat/sessions", want: 401},
|
|
{name: "valid cookie reaches protected API", method: "GET", path: "/api/chat/sessions", cookie: "valid", want: 501},
|
|
{name: "login requires CSRF", method: "POST", path: "/api/auth/login", body: `{"email":"alice@example.com","password":"a sufficiently long password"}`, want: 403},
|
|
{name: "incorrect credentials use generic refusal", method: "POST", path: "/api/auth/login", csrf: "1", body: `{"email":"unknown@example.com","password":"wrong"}`, want: 401},
|
|
{name: "successful login", method: "POST", path: "/api/auth/login", csrf: "1", body: `{"email":"alice@example.com","password":"a sufficiently long password"}`, want: 200},
|
|
{name: "anonymous cannot manage personal security", method: "POST", path: "/api/account/recovery", csrf: "1", body: `{}`, want: 401},
|
|
{name: "viewer manages own security after recent sign in", method: "POST", path: "/api/account/recovery", csrf: "1", body: `{}`, cookie: "valid", fresh: true, want: 200},
|
|
{name: "stale session cannot replace recovery", method: "POST", path: "/api/account/recovery", csrf: "1", body: `{}`, cookie: "valid", want: 403},
|
|
{name: "viewer cannot create accounts", method: "POST", path: "/api/accounts", csrf: "1", body: `{}`, cookie: "valid", fresh: true, want: 403},
|
|
{name: "viewer cannot edit instance authentication", method: "PUT", path: "/api/authentication", csrf: "1", body: `{}`, cookie: "valid", fresh: true, want: 403},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
srv := &Server{Accounts: accountBoundaryStore{fresh: tc.fresh}, Access: infraaccess.New(access.ShippedOrgPolicies("acme")), Principals: principalOf(principal)}
|
|
req := httptest.NewRequestWithContext(t.Context(), tc.method, "https://archie.example"+tc.path, strings.NewReader(tc.body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Archie-CSRF", tc.csrf)
|
|
if tc.cookie != "" {
|
|
req.AddCookie(&http.Cookie{Name: "archie_session", Value: tc.cookie})
|
|
}
|
|
recorder := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(recorder, req)
|
|
if recorder.Code != tc.want {
|
|
t.Fatalf("status=%d want=%d body=%s", recorder.Code, tc.want, recorder.Body.String())
|
|
}
|
|
if tc.name == "successful login" {
|
|
var found bool
|
|
for _, cookie := range recorder.Result().Cookies() {
|
|
if cookie.Name == "archie_session" {
|
|
found = true
|
|
if !cookie.HttpOnly || !cookie.Secure || cookie.SameSite != http.SameSiteStrictMode || cookie.Value != "new-session" {
|
|
t.Fatalf("unsafe session cookie: %+v", cookie)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("login did not set a session")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuthenticationTransport(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, url, origin, csrf string
|
|
want bool
|
|
}{
|
|
{"TLS", "https://archie.example/api/auth/login", "https://archie.example", "1", true},
|
|
{"localhost development", "http://localhost:8484/api/auth/login", "http://localhost:8484", "1", true},
|
|
{"network cleartext", "http://archie.example/api/auth/login", "http://archie.example", "1", false},
|
|
{"cross origin", "https://archie.example/api/auth/login", "https://attacker.example", "1", false},
|
|
{"missing CSRF", "https://archie.example/api/auth/login", "https://archie.example", "", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequestWithContext(t.Context(), "POST", tc.url, strings.NewReader(`{}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Archie-CSRF", tc.csrf)
|
|
req.Header.Set("Origin", tc.origin)
|
|
if got := (&Server{}).authorizeAuthenticationMutation(httptest.NewRecorder(), req); got != tc.want {
|
|
t.Fatalf("allowed=%v want=%v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAccountSessionFailureIsClosed(t *testing.T) {
|
|
srv := &Server{Accounts: unavailableAccounts{}}
|
|
req := httptest.NewRequestWithContext(t.Context(), "GET", "https://archie.example/api/auth/status", nil)
|
|
rec := httptest.NewRecorder()
|
|
srv.handleAccountStatus(rec, req)
|
|
if rec.Code != 503 {
|
|
t.Fatalf("account outage status=%d, want 503", rec.Code)
|
|
}
|
|
}
|
|
|
|
type unavailableAccounts struct{ identity.Accounts }
|
|
|
|
func (unavailableAccounts) AccountSetupRequired(context.Context) (bool, error) {
|
|
return false, errors.New("store unavailable")
|
|
}
|