diff --git a/yaiam/admin.go b/yaiam/admin.go index 28c177d..0c96df7 100644 --- a/yaiam/admin.go +++ b/yaiam/admin.go @@ -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. diff --git a/yaiam/admin_test.go b/yaiam/admin_test.go index 6ae6532..8e92534 100644 --- a/yaiam/admin_test.go +++ b/yaiam/admin_test.go @@ -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) diff --git a/yaiam/web.go b/yaiam/web.go index b97bf49..ad5dc0d 100644 --- a/yaiam/web.go +++ b/yaiam/web.go @@ -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) { diff --git a/yaiam/web_test.go b/yaiam/web_test.go index 1c6dd87..7d5a687 100644 --- a/yaiam/web_test.go +++ b/yaiam/web_test.go @@ -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) }