# Connect a service

We are going to move a credential from its provider into your broker—not through
a hub and not through us. Three of the four paths below hold to that exactly;
the fourth says plainly where it does not.

```sh
stk add github          # add the service, with safe default policy
stk connect github      # the credential, read from stdin — never a flag
```

A flag would land in shell history and in the process table, so the credential
is read from stdin only.

After those two commands, the service exists in policy and its credential is
sealed in the store. Nothing secret was placed in an argument.

## 1. Paste it

```sh
pbpaste | stk connect github        # macOS
stk connect github                  # then paste, then ctrl-D
```

For a token you already hold. Nothing is printed back but the number of fields
sealed and where.

That count is your payoff: it confirms what was sealed without echoing any of
the material back to the terminal.

## 2. A browser on this machine — OAuth 2.0 with PKCE

```sh
stk connect x --browser
```

The broker opens the provider's authorization page, receives the redirect on
loopback, and exchanges the code. The **refresh token** is stored; the access
token is not — it is acquired on demand, so a stolen store yields something that
must be exchanged with the provider before it is worth anything.

You register your own OAuth app and name its client id in `supertoolkit.yaml`:

```yaml
services:
  x:
    oauth2:
      client_id: <your app's client id>
      # only if the app is already registered with a different loopback callback
      redirect_uri: http://localhost:8080/callback
```

SuperToolkit ships no client id. A shared one would make every installation look
like the same app to the provider, and be a single point of revocation for
everyone. A client id is public by OAuth's design — it names an app and
authorises nothing — so it is safe in a committed file. A client secret is not,
and is read from stdin.

The redirect must be loopback: the broker receives it itself, so a callback
anywhere else never arrives, and one reachable from the network would let anyone
on it deliver a code to you.

When the browser flow returns, the broker holds the refresh token and can acquire
access tokens on demand. The browser never has to hand a token back through your
shell.

A self-hosted instance speaks the same protocol at other addresses. Name both
endpoints, or neither: exchanging a code at a server that did not issue it never
succeeds, so the file does not load with only one.

```yaml
    oauth2:
      client_id: <your app's client id>
      authorize_url: https://auth.example.com/oauth2/authorize
      token_url: https://auth.example.com/oauth2/token
```

## 3. No browser on this machine — the device grant

```sh
stk connect github --device
```

As for the browser flow, the client id is your own OAuth app's, named in
`supertoolkit.yaml` (the device grant exists so a public client needs no
secret). For a self-hosted instance such as GitHub Enterprise Server, name its
two endpoints too, both or neither:

```yaml
services:
  github:
    device:
      client_id: <your app's client id>
      # only for a self-hosted instance
      authorization_url: https://github.example.com/login/device/code
      token_url: https://github.example.com/login/oauth/access_token
```

The provider issues a short code. You approve it in a browser **anywhere** — a
phone is fine — and the token comes back to the broker directly, without ever
touching the machine you approved from.

```
code  9688-D9DD
open  https://github.com/login/device
enter the code to approve it; it is useless to anyone who does not
```

The **user code** above may be read aloud, screenshotted or sent through a
webhook: it does nothing without the person who approves it. The **device code**
— the value the poll redeems — never appears in any output.

Where the provider issues a refresh token, it is kept, and the connection renews
itself. Where it does not, the command says so rather than leaving you to find
out at the expiry.

Once you approve the short code, the broker receives the resulting token
directly. The machine with the browser has approved access without becoming the
credential courier.

Configure notifications so the code reaches you when nobody is watching the
terminal:

```yaml
notify:
  webhook: https://ntfy.sh/your-topic
  file: /var/log/stk-notify.log
```

With either destination configured, the approval prompt has somewhere to land
even when the broker's terminal is unattended.

## 4. Last resort — encrypted hand-over

For a machine with no browser whose provider offers no device grant.

```sh
# on the broker
stk handover key

# on a machine that has a browser, with the credential
stk handover seal github --to '<the key>'

# back on the broker
stk handover open github
```

The broker publishes a key that can only seal. Only its private half opens the
envelope, and a tampered one fails rather than yielding partial material.

Opening the envelope leaves the broker with the sealed credential and proves
that this broker—not the sealing machine—held the private half needed to read it.

**This path costs something the others do not**, and the commands say so: the
credential exists on the sealing machine while it is sealed, and still exists
there afterwards. Prefer the device grant or the web interface whenever either
is possible.

## Proving it works

```sh
stk verify github
stk verify github --expect-login your-login --json
```

`stk verify` makes one read call as the stored credential and prints the login
it acts as, or the reason it failed, and exits non-zero on failure. It never
prints a token.

You now have the useful proof: the provider says which account this credential
acts as, while the credential itself remains absent from the output.

## Withdrawing a credential

```sh
stk revoke github
stk revoke github --local     # only the local copy
```

Deleting our copy does not invalidate the token at the provider. Where the
provider offers revocation we call it; where it does not, the output says the
credential may still be valid and gives you the page that can withdraw it. It
never claims more than it did.

At the end, you know whether only the local copy is gone or the provider has
invalidated the credential too. That distinction is too important for a victory
message to blur.
