# User Attributes

> Declare typed attributes per organization, choose the scopes that release each one, and decide what users and granted applications may change.

An organization declares the attributes its users carry beside their account fields: a
department, an employee number, a loyalty tier, a newsletter preference. Each one is
typed, validated and labelled per language, and every writer is held to its
declaration. Three settings of a declaration decide three separate things, and none of
them implies another:

| Setting | Decides | Default |
|---|---|---|
| `scopes` | Which clients **receive** the value, and under which claim | Released nowhere |
| `userAccess` | What the **user** may do with it on the account page and at sign-up | `none` |
| `applicationWrite` | Whether a **granted application** may write it | No application may |

```mermaid
flowchart LR
  subgraph W["Who writes"]
    A["Administrators<br/>dashboard, admin API, CLI"]
    U["The user<br/>account page, sign-up<br/>(userAccess: write)"]
    S["A granted application<br/>service attribute API<br/>(applicationWrite + grant)"]
  end
  V[("The user's values,<br/>held to the declaration")]
  subgraph R["Who receives"]
    UI["UserInfo<br/>under a granted scope"]
    IT["ID token<br/>issued without an access token"]
    AT["Access token<br/>for a ProtectedResource<br/>that selects it"]
  end
  A --> V
  U --> V
  S --> V
  V --> UI
  V --> IT
  V --> AT
```

To carry attributes in the access tokens of an API, and to let a backend service update
them, follow [Protect an API with a ProtectedResource](https://www.nauthera.io/docs/v1/guides/protected-resources).

## Declaring attributes

An [`Organization`](https://www.nauthera.io/docs/v1/organization-resource) declares them in
`spec.userAttributes`, in the order forms show them:

```yaml
apiVersion: nauthera.io/v1alpha1
kind: Organization
metadata:
  name: acme
spec:
  slug: acme
  displayName: Acme
  namespaces: [acme]
  userAttributes:
    - name: department
      type: enum
      required: true
      userAccess: read
      scopes: [hr]
      accessToken: true
      labels: {en: Department, de: Abteilung}
      values:
        - {value: sales, labels: {en: Sales, de: Vertrieb}}
        - {value: engineering, labels: {en: Engineering, de: Entwicklung}}
    - name: employee_number
      type: integer
      minimum: 1
      scopes: [hr]
    - name: loyalty_tier
      type: enum
      userAccess: read
      applicationWrite: true
      values: [{value: bronze}, {value: silver}, {value: gold}]
    - name: newsletter
      type: boolean
      userAccess: write
      labels: {en: Send me the newsletter}
```

The main organization declares the same list, as YAML text, in
[`orgs.main.user_attributes`](https://www.nauthera.io/docs/v1/configuration); a declaration it refuses stops
startup, naming the field. `nauthera-cli create org` and `update org` take one with
`--user-attributes-file` (see the [CLI reference](https://www.nauthera.io/docs/v1/cli#organizations)). The admin
API and the dashboard only read it: `user_attributes` on the organization.

### Types and constraints

| `type` | A value | Constraints |
|---|---|---|
| `string`, the default | Text of at most 1024 bytes | `minLength`, `maxLength` (characters, up to 1024), `pattern` (RE2, matched as a whole) |
| `boolean` | `true` or `false` | |
| `integer` | A whole number within ±(2^53−1) | `minimum`, `maximum` |
| `date` | A calendar date, `YYYY-MM-DD` | `earliest`, `latest` |
| `enum` | One of `values` | `values`: up to 100 members, each with its own `labels` |

`multiValued: true` makes a value a list of up to 32 distinct members of the type, bounded
by `minItems` and `maxItems`. `required: true` refuses an administrator's create of a user
without a value, and any write that removes it. `labels` and `descriptions` are keyed by
BCP 47 tag of a language and an optional script, region and variants, such as `de-CH`:
up to 16 of each, a label of at most 100 characters and a description of at most 500.

A name is a lower-case letter followed by up to 63 lower-case letters, digits and
underscores. A claim the server sets itself or has not implemented, such as `sub`,
`email` or `groups`, and any name beginning `nauthera`, are refused. A standard profile
claim (`name`, `given_name`, `family_name`, `middle_name`, `nickname`, `profile`,
`picture`, `website`, `gender`, `birthdate`, `zoneinfo`, `locale`) may be declared to
constrain it: single-valued, a string or an enum, or a date for `birthdate`.

| Limit | Value |
|---|---|
| Declarations per organization | 64 |
| Keys on one user, declared or not | 64 |
| One user's attributes, encoded | 16 KiB |
| Members of a list | 32 |
| Scopes that release one attribute | 4 |

The `Organization` resource's own rules catch a constraint on the wrong type, or a lower
bound above its upper one, at `kubectl apply`. The server checks the rest (reserved
names, patterns, language tags, scopes) and refuses the resource as `Refused`, naming
`user_attributes[i].<field>` and the attribute.

## Who receives an attribute

`scopes` names up to four scopes that release the attribute, under the claim `claim`
names, or under its own name when `claim` is absent. A claim is a name as an attribute's
is, or an `https` URI with a host and no `#`, of up to 256 characters.

| Where | When it carries the attribute |
|---|---|
| UserInfo | The client was granted one of its scopes. The value is typed as declared: a number, a boolean, a list. |
| ID token | It was issued without an access token (`response_type=id_token`, which needs [`issuer.implicit_flow`](https://www.nauthera.io/docs/v1/configuration)) and the grant holds one of its scopes. An ID token issued beside an access token carries none, and stays small. |
| Access token | The token is for a [`ProtectedResource`](https://www.nauthera.io/docs/v1/protected-resource-resource) that selects the attribute, the declaration sets `accessToken: true`, and the grant holds one of its scopes. See [Attributes in access tokens](https://www.nauthera.io/docs/v1/guides/protected-resources#4-carry-attributes-in-its-tokens). |
| Introspection | Never. |

- **Scopes.** A release scope is printable ASCII without space, `"` or `\`. `openid`,
  `offline_access`, `profile`, `email`, `groups`, `address`, `phone`, `profile:write` and
  any scope beginning `nauthera.` are refused: a user who consents to `profile` is told it
  releases their name and username, not a salary. A standard claim is released under
  `profile` whatever it declares.
- **The client asks for the scope.** It must hold it in its
  [`OIDCClient`](https://www.nauthera.io/docs/v1/oidc-client)'s `spec.scopes` and request it, and the user consents
  to it. A claims request (OpenID Connect Core 1.0 §5.5) that names a declared claim asks
  for the first of its scopes the client is registered for, unless the request holds one
  already; `essential`, `value` and `values` constrain nothing.
- **Discovery.** The organization's `claims_supported` and `scopes_supported` list the
  claims its declaration releases and the scopes that release them, read on every request.
- **Only conforming values.** A value that does not conform to its declaration is
  withheld, a declared standard claim's included. A broker releases none of a member
  organization's attributes.
- **From the next token.** The declaration in force applies from the next token and the
  next UserInfo answer. A change that releases an attribute under a new scope or claim,
  sets `accessToken` or raises `userAccess` is recorded at warning, with
  `user_attributes_widened` on `organization.updated`.

## What the user may do

`userAccess` is what the user may do with an attribute: `none`, the default, `read` it, or
`write` it, which also lets them read it. It decides nothing about who receives the value;
that is `scopes`.

- **The account page.** `/account` beneath the organization's issuer
  (`https://auth.example.com/account` for the main organization,
  `https://auth.example.com/orgs/acme/account` for `acme`) lists every attribute declared
  `read` or `write`, with a control that fits its type: a switch for a boolean, a select
  for an enum, a date picker for a date, and one input per member of a list. A `read`
  attribute shows as text, under what the organization manages. A visitor without a session signs in first and comes back. The page reads and
  writes through `GET` and `PATCH /v1/auth/account/profile`.
- **Sign-up.** [Self-service registration](https://www.nauthera.io/docs/v1/sign-in-flows/sign-up) asks for every
  `required` attribute declared `write`, refuses the account without one, and refuses
  any attribute the user may not write. A required attribute of another access is left
  for an administrator to fill.
- **Never more.** A user never changes an attribute declared `none` or `read`, a standard
  claim the organization does not declare, or an undeclared name.
- **The display name.** Registration's name field writes `name`: taken where `name` is
  undeclared or `write`, refused under another access. The name an identity provider
  sends when its first sign-in creates an account is held to the rest of `name`'s
  declaration but not to `userAccess`, since a social provider's users choose their own.

<StoryScreenshot id="screens-account--default" alt="The account page: each declared attribute with a control that fits its type, and Save" />
<StoryScreenshot id="screens-register--with-attributes" alt="Sign-up asking for the organization's required attributes below the password" />

## Which applications may write

`applicationWrite: true` lets the organization's `spec.attributeWriteGrants` name the
attribute. It grants nothing by itself: a grant names one confidential `OIDCClient`, the
attributes it may write and the users it may write them on, and the client then writes
through the service attribute API with a `client_credentials` token, without any
user-admin role. Only the `Organization` resource grants; no client can grant itself.
The main organization's grants are
[`orgs.main.attribute_write_grants`](https://www.nauthera.io/docs/v1/configuration). The whole flow is in
[Let an application update attributes](https://www.nauthera.io/docs/v1/guides/protected-resources#7-let-an-application-update-attributes).

## Values stored before a declaration

An organization that declares nothing stores whatever an administrator writes, as
strings. Once it declares one attribute:

- every writer may add or change only a declared attribute or a standard profile claim,
  held to its declaration, and is refused any other with `undeclared`;
- a value stored before stays until an administrator removes it, and a value resent as it
  is stored is never refused, so an unrelated edit never fails over old data;
- a value reads through its declaration: text stored before a type was declared reads as
  its typed value when it converts (`"42"` as `42` for an integer), and a single value of
  a multi-valued attribute reads as a one-member list.

The admin API's read of one user lists what its values break under
`attribute_violations`: an undeclared name, a value that does not conform, a required
attribute with no value.

## For administrators

- **The dashboard's user page** edits declared attributes with typed inputs, and the
  create-user dialog asks for every required attribute, since the server refuses a new
  account without one. See [Admin Dashboard](https://www.nauthera.io/docs/v1/admin-dashboard).
- **The token claims page** previews the tokens and the UserInfo answer a user would get
  from a client, and names each declared attribute it withholds and why: no release
  scope, a scope the client did not register or did not request, no value, or a value
  that breaks its declaration.
- **The admin API** writes `attributes` on a user's `POST` and `PATCH`; a `PATCH`
  replaces the whole map. A refused value is a `400` `validation_error` whose field
  errors name `attributes.<name>` (or `attributes.<name>[i]` for one member of a list)
  with the rule it breaks, such as `not_allowed`, `too_long`, `required` or `undeclared`.
  See [Admin API & Users](https://www.nauthera.io/docs/v1/user-management#users).
- **Audit.** Every change is `user.updated`: an administrator's names `attributes` in
  `changed`; the user's (`self_service`) and a granted client's name each
  `attributes.<name>`, with the client as the actor. Records carry names, never values.

<ProductScreenshot name="dashboard-user-attributes" caption="A user's declared attributes on the dashboard, edited with inputs that fit each type." />

## Related

- [Protect an API with a ProtectedResource](https://www.nauthera.io/docs/v1/guides/protected-resources): attributes
  in access tokens, and applications that update them
- [Organization](https://www.nauthera.io/docs/v1/organization-resource): `spec.userAttributes` and
  `spec.attributeWriteGrants`
- [ProtectedResource](https://www.nauthera.io/docs/v1/protected-resource-resource): `spec.attributes`
- [Tokens & Sender Constraint](https://www.nauthera.io/docs/v1/tokens#profile-claims): the standard profile claims
