1 Commits
Author SHA1 Message Date
terminarandClaude Opus 5.5 538a6c8ae6 yaiam: user attributes in SignedIn and User (ADR-0044)
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>
2026-11-17 15:16:32 +01:00
4 changed files with 21 additions and 11 deletions

No files matched your search

+3
View File
@@ -70,6 +70,9 @@ type User struct {
Locale string `json:"locale"`
TimeZone string `json:"time_zone"`
State string `json:"state"`
// Attributes are the user's attributes by name (department,
// cost_center, ...); none with a yaiam before them.
Attributes map[string]string `json:"attributes"`
}
// Users lists the tenant's users.
+2 -2
View File
@@ -57,7 +57,7 @@ func newAdminFake(t *testing.T) *adminFake {
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"}`)
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":
@@ -88,7 +88,7 @@ func TestAdmin(t *testing.T) {
t.Fatalf("users %+v, %v", users, err)
}
u, err := a.User(ctx, "alice")
if err != nil || u.Email != "alice@example.org" || !u.EmailVerified {
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)
+13 -6
View File
@@ -31,9 +31,11 @@ import (
)
// WebScopes are asked for when Web.Scopes is nil: who the user is, their
// address, and their groups and roles at yaiam (the client must be
// allowed "groups" and "roles").
var WebScopes = []string{"openid", "profile", "email", "groups", "roles"}
// address, their groups and roles at yaiam (the client must be allowed
// "groups" and "roles") and the user attributes released to the client
// (yaiam allows "attributes" once some are). A scope the client is not
// allowed is left out, not refused.
var WebScopes = []string{"openid", "profile", "email", "groups", "roles", "attributes"}
// signInLifetime is how long a started sign-in may take.
const signInLifetime = 10 * time.Minute
@@ -88,6 +90,9 @@ type SignedIn struct {
// Groups and Roles with the scopes groups and roles: the names of the
// user's groups (subgroups' members included) and roles in the tenant.
Groups, Roles []string
// Attributes with the scope attributes: the user's attributes yaiam
// releases to the client (department, cost_center, ...), by name.
Attributes map[string]string
// ACR is how strongly the user signed in ("pwd", "mfa", "phr"), AMR
// with what; AuthTime when.
ACR string
@@ -282,6 +287,7 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
return nil, errors.New("yaiam: UserInfo names another user than the ID token")
}
out.Username, out.Name, out.Email, out.EmailVerified, out.Groups, out.Roles = u.Username, u.Name, u.Email, u.EmailVerified, u.Groups, u.Roles
out.Attributes = u.Attributes
}
return out, nil
}
@@ -389,9 +395,10 @@ func (w *Web) checkIDToken(idToken, nonce string) (*idClaims, error) {
// webUserInfo is UserInfo's answer with what a web sign-in also takes.
type webUserInfo struct {
UserInfo
EmailVerified bool `json:"email_verified"`
Groups []string `json:"groups"`
Roles []string `json:"roles"`
EmailVerified bool `json:"email_verified"`
Groups []string `json:"groups"`
Roles []string `json:"roles"`
Attributes map[string]string `json:"attributes"`
}
func (w *Web) userInfo(ctx context.Context, m *metadata, t *Token) (*webUserInfo, error) {
+3 -3
View File
@@ -71,7 +71,7 @@ func newOP(t *testing.T) *op {
o.ts = httptest.NewServer(mux)
t.Cleanup(o.ts.Close)
o.issuer = o.ts.URL + "/t/default"
o.userBody = `{"sub":"u-1","preferred_username":"alice","name":"Alice","email":"alice@example.org","email_verified":true,"groups":["staff","dev"],"roles":["editor"]}`
o.userBody = `{"sub":"u-1","preferred_username":"alice","name":"Alice","email":"alice@example.org","email_verified":true,"groups":["staff","dev"],"roles":["editor"],"attributes":{"department":"IT"}}`
o.claims = func(nonce string) map[string]any {
now := time.Now()
return map[string]any{"iss": o.issuer, "sub": "u-1", "aud": "web", "exp": now.Add(5 * time.Minute).Unix(), "iat": now.Unix(),
@@ -137,7 +137,7 @@ func TestWebSignIn(t *testing.T) {
w := o.web()
q, cookie := o.start(t, w, "/staff?x=1")
if q.Get("response_type") != "code" || q.Get("client_id") != "web" || q.Get("redirect_uri") != w.RedirectURL ||
q.Get("scope") != "openid profile email groups roles" || q.Get("code_challenge_method") != "S256" || q.Get("state") == "" || q.Get("nonce") == "" {
q.Get("scope") != "openid profile email groups roles attributes" || q.Get("code_challenge_method") != "S256" || q.Get("state") == "" || q.Get("nonce") == "" {
t.Errorf("authorization request %v", q)
}
if !cookie.HttpOnly || cookie.SameSite != http.SameSiteLaxMode || cookie.Name != "yaiam_signin" || cookie.Secure || cookie.MaxAge != 600 {
@@ -148,7 +148,7 @@ func TestWebSignIn(t *testing.T) {
t.Fatal(err)
}
if s.Subject != "u-1" || s.Username != "alice" || s.Name != "Alice" || s.Email != "alice@example.org" || !s.EmailVerified ||
strings.Join(s.Groups, ",") != "staff,dev" || strings.Join(s.Roles, ",") != "editor" || s.ACR != "mfa" || s.AuthTime.IsZero() ||
strings.Join(s.Groups, ",") != "staff,dev" || strings.Join(s.Roles, ",") != "editor" || s.Attributes["department"] != "IT" || len(s.Attributes) != 1 || s.ACR != "mfa" || s.AuthTime.IsZero() ||
s.ReturnTo != "/staff?x=1" || s.Token.AccessToken != "at-1" || s.IDToken == "" {
t.Errorf("signed in %+v", s)
}