# Access and tokens

> How branchd decides what a key, a bot or a sign-in may do, and which credential to use for which job.
Every request to branchd, whether through the API, git over HTTPS, the
container registry, generic and npm packages or Go modules, is allowed only when **all** of these
allow it:

1. **Scopes: which features, at which level.** Every credential carries
   scopes such as `forge:contents:read` (clone) or `forge:package:edit`
   (push images). See [Scopes and levels](https://docs.branchd.run/branchd/access/scopes).
2. **Resources: which repositories and groups.** A credential either
   reaches everything its owner can, or is limited to named repositories
   and groups. A group covers everything under it.
3. **The owner's current role.** A key or token never does more than the
   person or bot it belongs to may do today. Lose a role, and every
   credential you hold loses it at once.

A level such as _read_ or _write_ is only a shortcut for a set of scopes.
What a credential stores, and what branchd checks, is always the scopes.

## Which credential to use

| You want to…                                                                                         | Use                                                                                           |
| ---------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| Work from your own machine with the `branchd` CLI, git and Docker                                    | [`branchd auth login`](https://docs.branchd.run/branchd/access/cli-login). It signs you in and keeps the token fresh. |
| Give one script or tool a narrow login that reaches only some repositories                           | A [scoped login](https://docs.branchd.run/branchd/access/cli-login#scoped-logins) (`--repo`, `--group`, `--scopes`)   |
| Give a script, CI system or registry client a long-lived credential that acts as **you**             | A [personal API key](https://docs.branchd.run/branchd/access/personal-keys) (`xk_…`)                                  |
| Give automation a credential that belongs to a **team**, not a person                                | A [bot](https://docs.branchd.run/branchd/access/bots) and one of its keys                                             |
| Let another application with its own Xeonr Auth service account call branchd, with no branchd secret | Its [own token](https://docs.branchd.run/branchd/access/services), and grants to it                                   |
| Let another company's application act for branchd users                                              | [OAuth, with the least scopes it needs](https://docs.branchd.run/branchd/access/third-party-apps)                     |
| Run a job in a branchd pipeline                                                                      | The [job token](https://docs.branchd.run/branchd/pipelines/job-token), already in `$BRANCHD_TOKEN`                    |

## Sending a credential

| Where                                             | How                                                                                                                                                  |
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| API                                               | `Authorization: Bearer <token or xk_ key>`                                                                                                           |
| git over HTTPS                                    | The key or token as the password. The username is ignored.                                                                                           |
| Container registry (`registry.branchd.run`)       | `docker login registry.branchd.run -u token --password-stdin`, with the key on stdin. The username is ignored.                                       |
| Generic and npm packages (`registry.branchd.run`) | The key or token as the password with `curl --user`, or as `_authToken` in `.npmrc`. See [Access to packages](https://docs.branchd.run/branchd/packages/access#which-token). |

Source: https://docs.branchd.run/branchd/access
