feat(mysql): port atomic CLI authentication handoff persistence
This commit is contained in:
1 parent
cbcb8a24b4
commit
3e8a0b6a99
11 files changed
+234
-30
No files matched your search
@@ -0,0 +1,91 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccountBackendCLIBoundaries(t *testing.T) {
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL, storeMySQL} {
|
||||
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
||||
for _, mode := range []string{"domain", "origin", "verifier", "device", "expired", "denied", "suspended", "membership", "browser-revoked", "attempts", "read-only", "cancelled"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
s, domain := accountBackendStore(t, backend)
|
||||
ctx := context.Background()
|
||||
p, err := s.BootstrapOwner(domain, "owner", "synthetic CLI boundary password", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
browser, err := s.CreateBrowserAuthSession(domain, p.UUID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
verifier := strings.Repeat("b", 43)
|
||||
id, device, code, err := s.StartCLIAuth(ctx, domain, cliTestOrigin, hashAuthToken(verifier))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.BindCLIAuth(ctx, domain, cliTestOrigin, id, browser); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mode == "attempts" {
|
||||
for i := 0; i < 5; i++ {
|
||||
if e := s.DecideCLIAuth(ctx, domain, cliTestOrigin, id, browser, "wrong", "", true); !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if e := s.DecideCLIAuth(ctx, domain, cliTestOrigin, id, browser, code, "", true); !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal("attempt limit bypassed", e)
|
||||
}
|
||||
var attempts int
|
||||
if e := s.DB.QueryRow(`SELECT attempts FROM auth_cli_requests`).Scan(&attempts); e != nil || attempts != 5 {
|
||||
t.Fatal("failed attempts not retained", attempts, e)
|
||||
}
|
||||
} else if err = s.DecideCLIAuth(ctx, domain, cliTestOrigin, id, browser, code, "", mode != "denied"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
origin, query := cliTestOrigin, ""
|
||||
switch mode {
|
||||
case "domain":
|
||||
domain = "outside"
|
||||
case "origin":
|
||||
origin = "https://alias.example.test"
|
||||
case "verifier":
|
||||
verifier = strings.Repeat("c", 43)
|
||||
case "device":
|
||||
device = strings.Repeat("d", 43)
|
||||
case "expired":
|
||||
query = `UPDATE auth_cli_requests SET expires_ms=1`
|
||||
case "suspended":
|
||||
query = `UPDATE principals SET state='suspended'`
|
||||
case "membership":
|
||||
query = `UPDATE domain_memberships SET state='suspended'`
|
||||
case "browser-revoked":
|
||||
query = `UPDATE auth_sessions SET revoked_at_ms=1`
|
||||
case "read-only":
|
||||
s.ReadOnly = true
|
||||
case "cancelled":
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithCancel(ctx)
|
||||
cancel()
|
||||
}
|
||||
if query != "" {
|
||||
if _, err = s.DB.Exec(query); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if token, _, _, e := s.ExchangeCLIAuth(ctx, domain, origin, device, verifier); e == nil || token != "" {
|
||||
t.Fatal("invalid grant issued session", e)
|
||||
}
|
||||
var sessions int
|
||||
if e := s.DB.QueryRow(`SELECT COUNT(*) FROM auth_sessions`).Scan(&sessions); e != nil || sessions != 1 {
|
||||
t.Fatal("session leaked", sessions, e)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user