# Access to packages

> Who may read, publish and delete packages. Scopes and roles, which token to use, access to an npm scope or package, public packages and anonymous reads, and CI grants for another repository's jobs.
A repository's container images and generic packages share **one** set of
rules: if you can pull a repository's images, you can download its generic
packages, and the same goes for publishing and deleting.

**npm packages are different: they belong to a scope, not to a repository.**
The levels and tokens on this page are the same, but they are held on the
scope's group, on the scope or on one package. See
[npm scopes and packages](https://docs.branchd.run/branchd/packages/access#npm-scopes-and-packages).

## What you need

Three things decide a request, and all of them must allow it (see
[Access and tokens](https://docs.branchd.run/branchd/access)): the credential's scopes, the
repositories it reaches, and its owner's role today.

| To…                                                                                     | Scope                 | Role on the repository (for npm, on the scope or package) |
| --------------------------------------------------------------------------------------- | --------------------- | --------------------------------------------------------- |
| Download a generic package, install an npm package, list packages and versions          | `forge:package:read`  | read                                                      |
| Publish a file or an npm version; move an alias or a dist-tag                           | `forge:package:edit`  | write                                                     |
| Delete a file, version or package; deprecate or unpublish an npm version; set retention | `forge:package:admin` | maintain                                                  |
| Claim or release an npm scope                                                           | `forge:package:admin` | admin, on the group                                       |

A package you may not read answers as one that does not exist: `404` with a
credential, `401` without one.

## Which token

| Where                             | Use                                                                                                                                                                        |
| --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Your own machine                  | `export BRANCHD_TOKEN=$(branchd auth token)`, after [`branchd auth login`](https://docs.branchd.run/branchd/access/cli-login)                                                                      |
| A script or a server              | A [personal API key](https://docs.branchd.run/branchd/access/personal-keys), bound to the repositories it needs: `branchd token create ci-pull --repo acme/tools/core --scopes forge:package:read` |
| Automation that belongs to a team | A [bot](https://docs.branchd.run/branchd/access/bots) key                                                                                                                                          |
| A branchd pipeline job            | Its [job token](https://docs.branchd.run/branchd/pipelines/job-token), already in `$BRANCHD_TOKEN`, with `permissions: packages: read` or `write`                                                  |

How each client sends it:

| Client                       | How                                                                                                       |
| ---------------------------- | --------------------------------------------------------------------------------------------------------- |
| `curl`, for generic packages | `--user "token:$BRANCHD_TOKEN"` (the username is ignored), or `-H "Authorization: Bearer $BRANCHD_TOKEN"` |
| npm and pnpm                 | `//registry.branchd.run/npm/:_authToken=${BRANCHD_TOKEN}` in `.npmrc` (for pnpm, in your own `~/.npmrc`)  |
| Docker                       | `docker login registry.branchd.run -u token --password-stdin`                                             |
| The `branchd` CLI            | Its own sign-in, or `BRANCHD_TOKEN` in a job                                                              |

With `curl -L`, prefer `--user` to a bearer header: curl does not send
`--user` on to the storage host a download redirects to.

## A pipeline job

A job's token reaches **its own repository's** images and generic packages,
at the level its `permissions:` ask for. npm packages it reaches through a
[CI grant](https://docs.branchd.run/branchd/packages/access#npm-scopes-and-packages) on their scope or on the package:

- **`packages: read`** downloads and installs.
- **`packages: write`** also publishes, and moves aliases and dist-tags. It
  needs a run of a **protected branch**, started by a push or by hand. On a
  pull request's run and on unprotected branches a job only reads.
- **A job never deletes, deprecates or unpublishes**, whatever it declares.

Every download and publish a job makes is recorded against its run.

## Another repository's jobs

A job's token reaches no other repository's private packages. To let the
jobs of one repository **read** another's, a maintainer of the repository
that owns the packages makes a **CI grant**:

```sh
branchd repo grant create acme/tools/core \
  --title "Core for the app's builds" \
  --to repo:acme/app --may packages=read --when protected
```

- **`--to`** is who: `repo:<path>`, `group:<path>` (every repository under
  it) or `team:<slug>`.
- **`--may packages=read`** is what. `packages=write` lets them publish too;
  a grant never lets a job delete.
  The same grant can carry other areas, such as `terraform-state=outputs`
  for [Terraform state](https://docs.branchd.run/branchd/terraform-state#let-another-repositorys-jobs-in).
- **`--when`** is on which runs: `protected` (runs of the grantee's protected
  branches), `any` (pull requests too), or `owner`, the default: whoever the
  run answers to must have that access to this repository themselves.
- **A person with maintain access makes a grant**, or an agent acting for
  them (an [agent session](https://docs.branchd.run/branchd/access/cli-login#agent-sessions)),
  which the grant records as its grantor. A bot or a job cannot.
- `branchd group grant create <group> …` grants on every repository under a
  group at once. `branchd repo grant list` shows a repository's grants,
  `branchd repo grant revoke <id>` ends one, and
  `branchd repo grant explain acme/tools/core --from acme/app` says what a
  job of `acme/app` would get, and why.

The job in `acme/app` then names the other repository: the `repository:`
input of the [package steps](https://docs.branchd.run/branchd/packages/generic#in-a-pipeline), or the other
repository's path in a generic package URL. It still declares
`permissions: packages: read`.

A repository's CI grants cover its images and generic packages. They do not
cover npm packages, which no repository owns.

## npm scopes and packages

An npm package belongs to its scope, and the scope to the group that
[claimed it](https://docs.branchd.run/branchd/packages/npm#claim-a-scope). Access is granted on the scope (every
package under it) or on one package, with the same two commands as above
and an npm name in place of a repository:

```sh
# pipelines: a CI grant
branchd npm grant create @acme --to team:partners --may read --when any
branchd npm grant create @acme/sdk --to repo:partner/sdk --may write

# people: an access grant
branchd npm access grant @acme --to @ana --role write
```

| Who                               | Reaches a scope's packages by                               |
| --------------------------------- | ----------------------------------------------------------- |
| A member of the group's team      | Their team role, as for anything in that group              |
| Anyone else                       | An access grant on the scope or on one package              |
| A pipeline job, of any repository | A CI grant on the scope or on one package, and nothing else |
| Anyone at all                     | The group showing packages publicly: read only              |

- **Claiming a scope makes the first CI grant**, to the claiming group: its
  repositories' jobs publish and install.
- **A job has no access through people.** The person who started a run may
  be able to publish; the job still needs the grant.
- **A job publishes only from a protected run**, with `permissions:
  packages: write`. It never deprecates or unpublishes.
- **Only a person makes a grant.**

[Who may install and publish](https://docs.branchd.run/branchd/packages/npm#who-may-install-and-publish) has every
option, and how to list, revoke and explain grants.

## Public packages

A repository can show its images and generic packages to people who are not
members. Two settings make that so:

1. the repository is **public** (its own setting, or inherited from its
   group), and
2. its **Packages** feature is public. Packages are members-only by default,
   even in a public repository.

In the console, both are on the repository's **Settings › General**, in the
visibility card. With the CLI:

```sh
branchd repo visibility acme/tools/core            # show it
branchd repo visibility acme/tools/core --set public --confirm-public-path acme/tools/core
branchd repo visibility acme/tools/core --feature packages=public --confirm-public-path acme/tools/core
```

A group's setting is inherited by every repository under it that inherits,
so a whole group can be made public at once:

```sh
branchd group visibility acme/tools                 # show it
branchd group visibility acme/tools --set public --feature packages=public --confirm-public-path acme/tools
```

- **A person with maintain access may make something public**, or an agent
  acting for them: through the branchd CLI in its
  [session](https://docs.branchd.run/branchd/access/cli-login#agent-sessions), or the Xeonr MCP
  gateway's `forge_update_repository` and `forge_update_group` with _write_
  ticked (with `confirm_public_path`). A bot, a job or an API key cannot.
- **It applies to the repository's images and generic packages together.**
- **It covers every version already published**, not only what is published
  from now on.

Once packages are public, anyone can read them with **no credential**:

```sh
curl -fL -O https://registry.branchd.run/generic/acme/tools/core/core/latest/core.so
```

- **Reads only.** Publishing, aliases, dist-tags and deletes always need a
  credential with access.
- **Anonymous reads are rate limited.** Use a token in CI.
- **A visitor sees less.** Who published a file or a version is shown to
  members only, and so are retention settings.
- **A credential that is wrong is refused**, not treated as anonymous: an
  expired token gets `401` even on a public package.

A repository can also show its packages to everyone signed in to its
company or its organisation without being public (`--set company`,
`--set organisation`).

### Public npm packages

npm packages read as the **group that holds their scope** shows packages:
there is no repository to ask. Make that group public, with its Packages
feature public, in the visibility card on its **Settings › General** tab.

Anyone then installs every package under the scopes it holds with the
registry line alone:

```ini
@acme:registry=https://registry.branchd.run/npm/
```

- **It is per group, so per scope.** To publish some packages publicly and
  keep others private, hold two scopes in two groups.
- **A repository's visibility does not make an npm package public**, even
  the repository that published it.
- Publishing, dist-tags and unpublishing still need a credential with
  access.

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