# Using sculk.network as an authorizer

A closed route on Sculkworm can use a [sculk.network](https://docs.sculk.network/nightly/sculk/oidc-provider.md) instance as its OpenID Connect provider so that **only players who hold `server.play` on a specific server** on that instance are admitted.

> **Provider requirement:** Only use `allow.sculk_server` with an `oidc` client whose `issuer` is a sculk.network instance (where the `sculk_servers` userinfo claim is authoritative and controlled solely by the provider, never editable by end users). Pointing `allow.sculk_server` at a different OIDC provider that lets users populate custom profile claims named `sculk_servers` would allow any user on that provider to admit themselves.

Membership is re-checked live via `/oauth/userinfo` on every login, every ticket mint (`POST /tickets`), and every connect stream `OPEN` older than 60 seconds. Removing a player from the server, dropping their `server.play` permission, revoking their OIDC grant, or blocking their subject on sculk.network denies them on the next check. If the sculk.network instance is unreachable, the relay fails closed (`503 idp_unavailable` / `RESET` code `7`).

## 1. Enable the OIDC provider on sculk.network

On the sculk.network instance, enable the `oidcProvider` feature flag (enabled by default on `dev` and `nightly` builds; forced on stable builds with `SCULK_FEATURES=oidcProvider`) and list at least one administrator user ID in `SCULK_OIDC_ADMINS` (comma-separated):

```sh
SCULK_FEATURES=oidcProvider
SCULK_OIDC_ADMINS=<your-sculk-user-id>
```

Sign in on sculk.network and confirm `GET /api/me` returns your `id` and `GET /.well-known/openid-configuration` returns `200 OK`.

## 2. Register the relay as an OIDC client

Call `POST /api/oidc/clients` with a session belonging to a user in `SCULK_OIDC_ADMINS`. Register `${SCULKWORM_PUBLIC_URL}/login/callback` as the redirect URI (use `firstParty: true` if you want players to skip the consent prompt). Only add loopback redirect URIs (such as `http://127.0.0.1/callback` with `native: true`) if a native client talks to sculk.network directly — `sculkworm-connect` talks to the relay, and the relay is the OIDC client.

```sh
curl -sS -X POST https://sculk.example/api/oidc/clients \
  -H "Cookie: sculk_session=$ADMIN_SESSION" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "sculkworm relay",
    "redirectUris": ["https://relay.example/login/callback"],
    "firstParty": true
  }'
```

Save `client.clientId` and `clientSecret` from the `201 Created` response (`clientSecret` is returned once and never shown again).

## 3. Add the client to the relay secrets file

Add an `oidc` entry to `SCULKWORM_SECRETS_FILE` (mode `0600` or `0400`) pointing `issuer` at the sculk.network base URL (`SCULK_BASE_URL`):

```json
{
  "login_keys": {
    "1": "QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl8"
  },
  "clients": {
    "sculk-main": {
      "tenant": "sculk",
      "kind": "oidc",
      "issuer": "https://sculk.example",
      "scopes": ["openid", "profile", "offline_access", "sculk:servers"],
      "client_id": "vb3efdhxc2sbpqkmvbbd",
      "client_secret_env": "SCULKWORM_SCULK_OIDC_SECRET"
    }
  }
}
```

- Including `offline_access` lets the relay refresh expired 1-hour access tokens silently over the 30-day sealed login lifetime.
- Even if you omit `sculk:servers` from the client's default `scopes` or the route's `scopes`, any route with `allow: {"sculk_server": "<server id>"}` adds `sculk:servers` automatically.
- Reload the relay with `SIGHUP` (or restart it) after editing the secrets file.

## 4. Create the closed route with `allow.sculk_server`

Create or update the route through the relay admin API using the server's sculk.network ID (from `GET /api/servers` or `POST /api/servers`):

```sh
curl -sS -i -X PUT \
  -H "Authorization: Bearer $SCULKWORM_TENANT_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "closed",
    "authorizer": {
      "type": "oidc",
      "client": "sculk-main",
      "allow": {
        "sculk_server": "6uabltcpud3qpiy5"
      }
    }
  }' \
  http://127.0.0.1:8080/v1/routes/friends.play.sculk.network
```

When saving the route, the relay fetches `https://sculk.example/.well-known/openid-configuration`, verifies that `issuer` matches verbatim and that `token_endpoint` and `userinfo_endpoint` share the issuer's origin, and stores the route.

Then mint an agent token (`POST /v1/routes/friends.play.sculk.network/agent-token`) and run `sculkworm-agent` next to the Minecraft server as described in [Configuration](https://docs.sculk.network/nightly/sculkworm/configuration.md#agent-cli-flags-and-environment-variables).

## 5. What the player sees

1. **Vanilla Minecraft client without connect:** pinging `friends.play.sculk.network` shows `Closed server: join it with sculkworm-connect` and trying to join disconnects with instructions to run `sculkworm-connect play friends.play.sculk.network`.
2. **First login with `sculkworm-connect play friends.play.sculk.network`:**
   - Connect opens the player's browser (or falls back to the device code flow if no browser is available) and sends them to the relay, which redirects to `https://sculk.example/oauth/authorize` requesting `openid profile offline_access sculk:servers`.
   - The player signs in to sculk.network (and approves consent once if `firstParty` is `false`).
   - sculk.network redirects back to `https://relay.example/login/callback`. The relay exchanges the code, validates the ID token, calls `/oauth/userinfo`, and checks that `sculk_servers` contains an object with `id == "6uabltcpud3qpiy5"` and `is_player == true`.
   - **Admitted player:** the browser shows login complete, connect stores the sealed login (`swl_...`) in `logins.json` (`0600`), and the player joins `127.0.0.1:25565` in Minecraft.
   - **Non-member or member without `server.play`:** the relay answers `403 Forbidden` ("You are signed in as `<name>`, but `friends.play.sculk.network` does not admit this account.") and issues no login.
3. **Revocation:**
   - **Player loses `server.play` on `6uabltcpud3qpiy5`** (removed from the server or moved to a role without `server.play`): `/oauth/userinfo` no longer includes that server with `is_player: true`, so the next `POST /tickets` fails with `403 not_admitted` (and a new connect stream `OPEN` past the 60-second check window fails with stream `RESET` code `6`).
   - **OIDC grant revoked (`DELETE /api/oidc/grants/:grantId`) or subject blocked (`POST /api/oidc/subjects/:userId/block`):** sculk.network rejects `/oauth/userinfo` with `401` and rejects the subsequent token refresh with `invalid_grant`, so the next `POST /tickets` fails with `401 login_expired` (and a new connect stream `OPEN` past the 60-second check window fails with stream `RESET` code `6`).
