Web asks for the scope attributes; SignedIn.Attributes carries the attributes yaiam released to the application (from UserInfo), and Admin's User carries a user's attributes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
4.7 KiB
Go
138 lines
4.7 KiB
Go
package yaiam
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// adminFake is the token endpoint and the admin API: a service token for
|
|
// the client credentials of "svc"/"s3cret", lists for it.
|
|
type adminFake struct {
|
|
ts *httptest.Server
|
|
mu sync.Mutex
|
|
tokens int
|
|
reject int // answer this many API calls with 401
|
|
lastPath string
|
|
}
|
|
|
|
func newAdminFake(t *testing.T) *adminFake {
|
|
f := &adminFake{}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /t/default/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
|
|
json.NewEncoder(w).Encode(map[string]string{"issuer": f.ts.URL + "/t/default", "token_endpoint": f.ts.URL + "/token"})
|
|
})
|
|
mux.HandleFunc("POST /token", func(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
if u, p, ok := r.BasicAuth(); !ok || u != "svc" || p != "s3cret" || r.PostForm.Get("grant_type") != "client_credentials" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
fmt.Fprint(w, `{"error":"invalid_client"}`)
|
|
return
|
|
}
|
|
f.mu.Lock()
|
|
f.tokens++
|
|
n := f.tokens
|
|
f.mu.Unlock()
|
|
fmt.Fprintf(w, `{"access_token":"svc-%d","token_type":"Bearer","expires_in":300}`, n)
|
|
})
|
|
mux.HandleFunc("GET /api/v1/tenants/default/", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
f.lastPath = r.URL.Path
|
|
reject := f.reject > 0
|
|
if reject {
|
|
f.reject--
|
|
}
|
|
f.mu.Unlock()
|
|
if reject || !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer svc-") {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch strings.TrimPrefix(r.URL.Path, "/api/v1/tenants/default/") {
|
|
case "users":
|
|
fmt.Fprint(w, `{"items":[{"id":"u-1","ref":"alice","username":"alice","display_name":"Alice","user_state":"active","home":true}]}`)
|
|
case "users/alice":
|
|
fmt.Fprint(w, `{"id":"u-1","username":"alice","display_name":"Alice","email":"alice@example.org","email_verified":true,"state":"active","attributes":{"department":"IT"}}`)
|
|
case "groups":
|
|
fmt.Fprint(w, `{"items":[{"id":"g-1","name":"staff","protected":false}]}`)
|
|
case "groups/staff/members":
|
|
fmt.Fprint(w, `{"items":[{"id":"u-1","ref":"alice","username":"alice"}]}`)
|
|
default:
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(http.StatusNotFound)
|
|
fmt.Fprint(w, `{"title":"Not Found","detail":"no such thing"}`)
|
|
}
|
|
})
|
|
f.ts = httptest.NewServer(mux)
|
|
t.Cleanup(f.ts.Close)
|
|
return f
|
|
}
|
|
|
|
func (f *adminFake) admin() *Admin {
|
|
return &Admin{Client: Client{Issuer: f.ts.URL + "/t/default", ClientID: "svc"}, ClientSecret: "s3cret", API: f.ts.URL + "/api/v1", Tenant: "default"}
|
|
}
|
|
|
|
// TestAdmin: the lists and a user, with one service token for all;
|
|
// a refused token is fetched again once; an error answer says what.
|
|
func TestAdmin(t *testing.T) {
|
|
f := newAdminFake(t)
|
|
a := f.admin()
|
|
ctx := context.Background()
|
|
users, err := a.Users(ctx)
|
|
if err != nil || len(users) != 1 || users[0].Username != "alice" || users[0].State != "active" || !users[0].Home {
|
|
t.Fatalf("users %+v, %v", users, err)
|
|
}
|
|
u, err := a.User(ctx, "alice")
|
|
if err != nil || u.Email != "alice@example.org" || !u.EmailVerified || u.Attributes["department"] != "IT" {
|
|
t.Errorf("user %+v, %v", u, err)
|
|
}
|
|
groups, err := a.Groups(ctx)
|
|
if err != nil || len(groups) != 1 || groups[0].Name != "staff" {
|
|
t.Errorf("groups %+v, %v", groups, err)
|
|
}
|
|
members, err := a.GroupMembers(ctx, "staff")
|
|
if err != nil || len(members) != 1 || members[0].ID != "u-1" {
|
|
t.Errorf("members %+v, %v", members, err)
|
|
}
|
|
if f.tokens != 1 {
|
|
t.Errorf("%d tokens for four calls", f.tokens)
|
|
}
|
|
f.reject = 1
|
|
if _, err := a.Groups(ctx); err != nil || f.tokens != 2 {
|
|
t.Errorf("after a refused token: %v, %d tokens", err, f.tokens)
|
|
}
|
|
f.reject = 2
|
|
var e *Error
|
|
if _, err := a.Groups(ctx); !errors.As(err, &e) || e.Status != http.StatusUnauthorized {
|
|
t.Errorf("refused twice: %v", err)
|
|
}
|
|
if _, err := a.User(ctx, "bob"); !errors.As(err, &e) || e.Status != http.StatusNotFound || e.Description != "no such thing" {
|
|
t.Errorf("not found: %v", err)
|
|
}
|
|
|
|
wrong := f.admin()
|
|
wrong.ClientSecret = "nope"
|
|
if _, err := wrong.Users(ctx); !errors.As(err, &e) || e.Code != "invalid_client" {
|
|
t.Errorf("wrong secret: %v", err)
|
|
}
|
|
insecure := f.admin()
|
|
insecure.API = "http://admin.example.org/api/v1"
|
|
if _, err := insecure.Users(ctx); err == nil || !strings.Contains(err.Error(), "not https") {
|
|
t.Errorf("an http API off loopback: %v", err)
|
|
}
|
|
noTenant := f.admin()
|
|
noTenant.Tenant = ""
|
|
if _, err := noTenant.Users(ctx); err == nil || !strings.Contains(err.Error(), "no tenant") {
|
|
t.Errorf("no tenant: %v", err)
|
|
}
|
|
noTenant.Tenant = "a/b"
|
|
if _, err := noTenant.Users(ctx); err == nil || !strings.Contains(err.Error(), "no tenant") {
|
|
t.Errorf("a tenant with a slash: %v", err)
|
|
}
|
|
}
|