feat(auth): add scoped JSON account and session queries for CLI clients
Build and publish container images / images (push) Successful in 18m18s
Build and publish container images / images (push) Successful in 18m18s
This commit is contained in:
1 parent
6f9752ea3a
commit
b650fe50d7
10 files changed
+640
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAccountBackendAuthQueries(t *testing.T) {
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL, storeMySQL} {
|
||||
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, backend)
|
||||
ctx := context.Background()
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic auth query password", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := s.CreateAuthSession(d, p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current, err := s.CurrentAuthSession(ctx, d, p.UUID, token)
|
||||
if err != nil || current.UUID == "" {
|
||||
t.Fatal(current, err)
|
||||
}
|
||||
for _, kind := range []string{"expired", "revoked"} {
|
||||
extra, e := s.CreateAuthSession(d, p.UUID)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
v, e := s.CurrentAuthSession(ctx, d, p.UUID, extra)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if kind == "expired" {
|
||||
_, e = s.DB.Exec(`UPDATE auth_sessions SET expires_at_ms=1 WHERE uuid=`+s.sqlParam(1), v.UUID)
|
||||
} else {
|
||||
e = s.RevokeAuthSession(extra)
|
||||
}
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
// A retained session in another domain must never appear in either scope.
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,1,`+s.sqlParam(5)+`,1,0)`, "foreign-session", "synthetic-foreign-hash", p.UUID, "foreign-domain", time.Now().Add(time.Hour).UnixMilli())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, principal := range []string{p.UUID, ""} {
|
||||
items, truncated, e := s.QueryAuthSessions(ctx, d, principal, current.UUID, false, 100)
|
||||
if e != nil || truncated || len(items) != 1 || !items[0].IsCurrent || items[0].Status != "active" {
|
||||
t.Fatal(items, truncated, e)
|
||||
}
|
||||
items, truncated, e = s.QueryAuthSessions(ctx, d, principal, current.UUID, true, 100)
|
||||
if e != nil || truncated || len(items) != 3 {
|
||||
t.Fatal(items, truncated, e)
|
||||
}
|
||||
statuses := map[string]int{}
|
||||
for _, v := range items {
|
||||
statuses[v.Status]++
|
||||
}
|
||||
if statuses["active"] != 1 || statuses["revoked"] != 1 || statuses["expired"] != 1 {
|
||||
t.Fatal(statuses)
|
||||
}
|
||||
items, truncated, e = s.QueryAuthSessions(ctx, d, principal, current.UUID, true, 1)
|
||||
if e != nil || !truncated || len(items) != 1 {
|
||||
t.Fatal(items, truncated, e)
|
||||
}
|
||||
}
|
||||
if _, e := s.CurrentAuthSession(ctx, "foreign-domain", p.UUID, token); e == nil {
|
||||
t.Fatal("cross-domain current session")
|
||||
}
|
||||
roles, groups, truncated, e := s.AuthMembershipNames(ctx, d, p.UUID)
|
||||
if e != nil || roles == nil || truncated || len(groups) < 2 {
|
||||
t.Fatal(roles, groups, truncated, e)
|
||||
}
|
||||
roles, groups, _, e = s.AuthMembershipNames(ctx, "foreign-domain", p.UUID)
|
||||
if e != nil || len(roles) != 0 || len(groups) != 0 {
|
||||
t.Fatal("cross-domain memberships", e)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user