# Turn access off and on

> Stop a user's sign-in and tokens at once with one field, or from a date you set, and give everything back by setting it again.

Two fields on every user decide whether they may sign in: `status` and `accountExpiry`. Set `status` to `inactive`,
or let `accountExpiry` pass, and from that moment the user can't sign in and no app can get a new token for them,
anywhere in the realm. Nothing about them is removed, so setting them active again gives back everything they had.

## Before you begin

- **Privilege:** `RealmOwner` in the realm, or a privilege of your own that may change users. See
  [Who may do what](../../attributes/identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).
- **The user exists.** Both fields are on the shipped `user` type; nothing needs adding.

## How it works

A user may sign in only while their `status` is `active` and their `accountExpiry`, if set, is still in the future.
Once either stops being true:

- their sign-in is refused;
- no app can get a new token for them, and their refresh tokens stop working;
- token introspection reports their tokens as inactive, and the userinfo endpoint stops answering for them.

Set `status` back to `active`, or move `accountExpiry` later, and they can sign in again. Their groups,
entitlements, secrets and agents are where they left them.

**Expiry is checked at the moment of use.** Nothing runs at the expiry time; each sign-in and each token request
compares it with the clock. That makes `accountExpiry` the simplest way to give a contractor or an intern access that
ends by itself.

### What happens to tokens already issued

Setting a user inactive stops every *new* token at once, and every refresh. An access token issued before then is a
signed token that an app can check by itself, so it stays valid, at an app that checks it that way, until it expires.
Access tokens are short-lived (five minutes unless your realm sets otherwise; see
[Access tokens](../../../workforce-identity/applications/tokens/access-tokens/)). An app or API that
[introspects](../../../workforce-identity/applications/revoke-access/introspection/) each token sees it as inactive
at once.

## Set it up

Turn a user's access off:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "status": "inactive" }'
```

Send `{ "status": "active" }` to turn it back on.

End a user's access at a set time, written as an RFC 3339 time with its offset, such as `2027-03-31T18:00:00Z`:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "accountExpiry": "2027-03-31T18:00:00Z" }'
```

Then read the user back and check that `accountExpiry` holds the time you meant. Send `{ "accountExpiry": null }` to
remove the expiry; leave it out altogether for access with no end.

## What the user sees

- **At sign-in,** an inactive or expired user is refused with `401` and the code `INVALID_CREDENTIALS`, the same status
  and code as a wrong password. An app signing them in through OAuth gets `access_denied`. The reason is kept for you,
  on the audit trail (below); tell the user yourself, if they should know.
- **In an app they're already signed in to,** nothing changes until the app needs a new token. Then the app is
  refused with `invalid_grant`, and the user is sent to sign in again, which fails. The userinfo endpoint answers the
  app `401` (`invalid_token`). Sessions already open stay open until they end or you
  end them.
- **An agent acting for them** gets no new token for them.

## Limits and defaults

| What | Default | Range |
|---|---|---|
| `status` | `active`, filled on every write that leaves it out | `active` or `inactive`; anything else is refused `400` |
| `accountExpiry` | Not set: no end | An RFC 3339 time with its offset |
| How soon a change takes effect | At the next sign-in, token request or refresh | |
| An access token already issued | Valid until it expires, at apps that check it themselves | Five minutes by default |

## Audit record

- The change itself is recorded as `PATCHED` (or `UPDATED` for a `PUT`), with `status` or `accountExpiry` before and
  after, in the user's [change history](../../attributes/identity-model/#every-change-is-recorded).
- Each refused sign-in is recorded as `LOGIN_FAILED` under the user's id, with the reason: `AUTH_USER_INACTIVE` for an
  inactive user, `AUTH_ACCOUNT_EXPIRED` for one past their expiry. The reason is on the audit trail only, never in the
  answer to the sign-in. See [What is recorded](../../../audit-reports/audit-trail/what-is-recorded/).
- Each refused token request is recorded as `TOKEN_ISSUED`, with why it was refused.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A user past their `accountExpiry` can still sign in | The time isn't an RFC 3339 time with an offset (for example `2027-03-31` or `31/03/2027`), so the realm can't read it | Write it again as `2027-03-31T18:00:00Z`, and read it back |
| An inactive user is still using an app | The app holds a session or an access token issued before you set them inactive | End their sessions (see [Offboard a leaver](../../guides/offboard-a-leaver/)); the access token ends within its lifetime |
| A user you didn't change can't sign in | Their record has no `status`, which reads as inactive. This happens only to a record written without the realm filling it | Set `{ "status": "active" }` |
| A write of `status` is refused `400` | A value other than `active` or `inactive` | Send one of the two |

## Good to know

- **Sessions already open are not ended.** Sign-in and token issue stop at once, but a session the user already
  holds stays until it ends or you end it. To end it now, see
  [Offboard a leaver](../../guides/offboard-a-leaver/).
- **A missing `status` reads as inactive.** The realm fills `active` on every write that leaves it out, so this only
  matters for a record written some other way: it is refused at sign-in, not let through.
- **Agents follow their user.** An agent acting for an inactive user gets no new token for them. An agent the user
  owns has an identity of its own, and goes on until you stop it: see
  [Stop an agent](../../../workforce-identity/guides/agents/stop-an-agent/).
- **Inactive is reversible; delete isn't.** Deleting a user removes them, every link to them and their personal vault,
  and leaves the agents they owned without an owner. Keep them inactive while you may need them back, or need their
  record for an audit.

## Related

- [Joiners, movers and leavers](../../lifecycle/joiners-movers-leavers/): where turning access off fits.
- [Offboard a leaver](../../guides/offboard-a-leaver/): inactive, sessions ended, groups removed.
- [Add and change users](../add-and-change-users/): every attribute a user has.
- [Receive signals](../../../workforce-identity/signals/receive-signals/): your identity provider turns a user off.
