# Services

> An application with its own Xeonr Auth service account calls branchd with its own token, reaching only what teams grant it.
An application that already has an **Xeonr Auth service account** can call
branchd with that account's own token, instead of holding a bot key. It
holds no branchd secret at all: it asks Xeonr Auth for a short-lived token
whenever it needs one.

A service reaches nothing until it is granted access. Each team that wants
it grants it **read** or **write** on its root, a group or a repository,
and an administrator of a company can grant it **read** on every team the
company owns, now and later. Its token's scopes still cap it, and branch
rules still apply: a service is never a person, so it cannot approve a pull
request or push past a protected branch.

## 1. Let the service account ask for branchd's scopes

In Xeonr Auth, give the service account an approved binding to the
**branchd** application with the scopes it needs, for example
`forge:repository:read` and `forge:contents:read` to read code, and
`forge:package:read` to pull images and packages. A service that opens pull
requests also needs `forge:contents:edit` and `forge:pull-request:edit`.
See [Scopes and levels](https://docs.branchd.run/branchd/access/scopes).

## 2. Get a token for branchd

Exchange the service account's assertion for a token whose audience is
branchd (`resource=https://branchd.run`), with the jwt-bearer grant:

```go
ts, err := authn.NewTokenSource(authn.TokenSourceConfig{
	TokenURL:  "https://auth.xeonr.io/api/v1/oauth/token",
	Grant:     authn.GrantJWTBearer,
	Assertion: serviceAccountToken,
	Resource:  "https://branchd.run",
	Scopes:    []string{"forge:repository:read", "forge:contents:read"},
})
client := ts.Client(http.DefaultClient) // every request carries the token
```

The token works everywhere a key does: as `Authorization: Bearer` on the
API, and as the password for git over HTTPS, the container registry, generic
and npm packages and Go modules.

## 3. Grant it access

Name the service by its service account's URN:

```sh
# One team gives it read on its whole team
branchd access grant acme --to service:urn:xeonr:auth:uid:service_account:48 --role read

# Write on one repository, to push branches and open pull requests
branchd access grant acme/tools/fixer --to service:urn:xeonr:auth:uid:service_account:48 --role write

# A company administrator gives it read on every team the company owns
branchd access grant company:acme --to service:urn:xeonr:auth:uid:service_account:48 --role read
```

A team keeps a company grant out of its own repositories with a limit on
the service at its root:

```sh
branchd access limit acme --to service:urn:xeonr:auth:uid:service_account:48 --role none
```

`branchd access explain <path> --for service:<URN>` says why the service
reaches what it does, `branchd access list company:<slug>` lists a
company's grants, and every grant and revocation is in the audit log, with
the service as the actor of everything it does.

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