# Sign in with Sandcovery

Let people sign in to your product with Sandcovery and give it the sandboxes and rights they choose.

## How it works

Sign in with Sandcovery is OAuth 2.0 with PKCE. Your product sends a person to Sandcovery, they choose an organization, the permissions your product gets there and the sandboxes it reaches, and your product gets an access token that calls the [Sandcovery API](/docs/api) within that choice.

## Register your app

Admins of your organization register apps on the **OAuth Apps** page of the [dashboard](/app/apps). Choose **Create App**, give it a name of up to 64 characters and up to 10 redirect URLs your product receives the code at, and the page shows its client ID and token URL. People see that name when they sign in. Each redirect URL is an `https` address, or `http` on `localhost` or `127.0.0.1`, with no `#` fragment. An organization holds up to 20 apps; delete one to add another.

Apps are public clients, so there is no client secret: every sign-in proves itself with a PKCE `code_verifier`.

The CLI and the API do the same with the `oauthApps` operations:

```bash
sandcovery oauthApps createOAuthApp --name 'My Product' --redirect_uris '["https://example.com/callback"]'
sandcovery oauthApps listOAuthApps
sandcovery oauthApps updateOAuthApp --client_id <client_id> --redirect_uris '["https://example.com/callback","http://localhost:3000/callback"]'
sandcovery oauthApps deleteOAuthApp --client_id <client_id>
```

Deleting an app ends every grant people gave it.

## Send people to Sandcovery

Send the person to `https://www.sandcovery.com/authorize` with these query parameters:

| Parameter | Value |
| --- | --- |
| `client_id` | Your app's client ID |
| `redirect_uri` | One of your app's redirect URLs |
| `response_type` | `code` |
| `scope` | The permissions you ask for, and `profile` and `email` when you want them, separated by spaces |
| `state` | A random value you check when the person comes back |
| `code_challenge` | The base64url SHA-256 of your `code_verifier` |
| `code_challenge_method` | `S256` |

The permissions are:

| Permission | Lets your product |
| --- | --- |
| `sandboxes:read` | See sandboxes, their files and processes |
| `sandboxes:run` | Run commands and processes, upload files, pause, resume and snapshot |
| `sandboxes:create` | Create sandboxes, offered only when the person is an admin of the organization |
| `sandboxes:delete` | Destroy sandboxes |

Sandcovery signs the person in if they aren't, and asks one with no organization to create one. It refuses an unknown client ID, or a redirect URL your app did not register, before it shows the choice. The person picks the organization, the permissions from those you asked for, and the sandboxes your product reaches, and chooses **Allow**. **Deny** sends them back to your redirect URL with `error=access_denied` and your `state`.

After **Allow**, the person confirms once more on the next screen, and comes back to your redirect URL with `code` and `state`.

## Get a token

Exchange the code at your app's token URL with `grant_type=authorization_code`, the `code`, the same `redirect_uri`, your `client_id` and the `code_verifier`. The answer holds an `access_token`.

Send the access token to the Sandcovery API as a bearer token:

```bash
curl https://www.sandcovery.com/api/v1/sandboxes -H "Authorization: Bearer $ACCESS_TOKEN"
```

## What the token reaches

The token acts in the organization the person chose, with the permissions they chose. It reaches the sandboxes they picked and every sandbox your product creates under the grant, and nothing else: no keys, usage, billing or other sandboxes. It stops reaching anything once the person leaves the organization or an admin deletes the app.

When the same person signs in to your app again and chooses the same organization, the grant keeps its ID and takes the new choice. Choosing another organization replaces it with a new grant.
