# OAuth Discovery and Token Exchange

Use this guide to connect a client to SIMcloud through OAuth. The user grants access in their browser, and the client exchanges the returned code for a token tied to the chosen SIMcloud resource. The guide covers discovery, consent, token exchange, refresh and revocation.

[Web guide](https://simcloud.co.za/docs/guide/oauth-api/)

GET /.well-known/\* · POST /oauth/register.php · GET /oauth/authorize.php · POST /oauth/token.php

[API reference](https://simcloud.co.za/.well-known/oauth-authorization-server)

## Compare the authorization and resource metadata

Read the authorization-server document for endpoints, PKCE and the two supported scopes. Match the resource metadata to the MCP URL. The reporting resource advertises reports:read. The transactions resource advertises api. Tokens are bound to their requested resource and scope.

### GET /.well-known/oauth-authorization-server

```bash
curl -4 -sS -i "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/.well-known/oauth-authorization-server"
```

### GET /.well-known/oauth-protected-resource

```bash
curl -4 -sS -i "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/.well-known/oauth-protected-resource"
```

HTTP 200 response excerpt

```json
{
  "scopes_supported": [
    "api"
  ]
}
```

### GET /.well-known/oauth-protected-resource/api/reporting-mcp.php

```bash
curl -4 -sS -i "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/.well-known/oauth-protected-resource/api/reporting-mcp.php"
```

HTTP 200 response excerpt

```json
{
  "scopes_supported": [
    "reports:read"
  ]
}
```

## Register a desktop client

Provide a supported callback you control. This example uses a local desktop callback and requests reports:read. HTTP 201 returns the client_id. Set SIMCLOUD_OAUTH_CLIENT_ID and SIMCLOUD_OAUTH_REDIRECT_URI to your client and callback. Use the published callback rules for other clients.

### POST /oauth/register.php

```bash
curl -4 -sS -i -X POST "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/oauth/register.php" \
  -H 'Content-Type: application/json' \
  --data '{
  "client_name": "Example SIMcloud reporting client",
  "redirect_uris": [
    "http://127.0.0.1:43210/callback"
  ],
  "scope": "reports:read",
  "token_endpoint_auth_method": "none"
}'
```

HTTP 201 response excerpt

```json
{
  "scope": "reports:read",
  "token_endpoint_auth_method": "none"
}
```

## Open the authorization request in the user’s browser

Generate a fresh high-entropy state and PKCE verifier. Set SIMCLOUD_PKCE_CHALLENGE to the unpadded base64url SHA-256 digest of the verifier. Set SIMCLOUD_OAUTH_RESOURCE to the full reporting endpoint URL, including /api/reporting-mcp.php. Use the request below to construct the browser URL. Sign in and approve only the required permission. Validate state and issuer on the return before setting SIMCLOUD_OAUTH_CODE to the returned code.

### GET /oauth/authorize.php

```bash
curl -4 -sS -i "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/oauth/authorize.php" \
  --get \
  --data-urlencode 'response_type=code' \
  --data-urlencode "client_id=$SIMCLOUD_OAUTH_CLIENT_ID" \
  --data-urlencode "redirect_uri=$SIMCLOUD_OAUTH_REDIRECT_URI" \
  --data-urlencode "resource=$SIMCLOUD_OAUTH_RESOURCE" \
  --data-urlencode 'scope=reports:read' \
  --data-urlencode "state=$SIMCLOUD_OAUTH_STATE" \
  --data-urlencode "code_challenge=$SIMCLOUD_PKCE_CHALLENGE" \
  --data-urlencode 'code_challenge_method=S256'
```

> The curl command shows the initial request. The browser handles the SIMcloud login and consent screen. Keep the verifier, code and refresh token private.

## Exchange the code for a resource-bound token

Send the authorization code, the same client, callback and resource, and the original verifier in a form-encoded POST. The response includes access_token, refresh_token, token_type, scope and expires_in. Use the reporting access token for requests to that resource.

### POST /oauth/token.php

```bash
curl -4 -sS -i -X POST "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/oauth/token.php" \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "client_id=$SIMCLOUD_OAUTH_CLIENT_ID" \
  --data-urlencode "code=$SIMCLOUD_OAUTH_CODE" \
  --data-urlencode "redirect_uri=$SIMCLOUD_OAUTH_REDIRECT_URI" \
  --data-urlencode "resource=$SIMCLOUD_OAUTH_RESOURCE" \
  --data-urlencode "code_verifier=$SIMCLOUD_PKCE_VERIFIER"
```

HTTP 200 response excerpt

```json
{
  "token_type": "Bearer",
  "scope": "reports:read"
}
```

## Refresh an access token

Use the current refresh token with the same client and resource. Save the newly returned tokens securely. If the connection was revoked, restart browser consent rather than trying to reuse its refresh token.

### POST /oauth/token.php

```bash
curl -4 -sS -i -X POST "${SIMCLOUD_API_BASE:-https://simcloud.co.za}/oauth/token.php" \
  --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode "client_id=$SIMCLOUD_OAUTH_CLIENT_ID" \
  --data-urlencode "resource=$SIMCLOUD_OAUTH_RESOURCE" \
  --data-urlencode "refresh_token=$SIMCLOUD_OAUTH_REFRESH_TOKEN"
```

HTTP 200 response excerpt

```json
{
  "token_type": "Bearer",
  "scope": "reports:read"
}
```

## Remove a connection

Open Connected Apps using the link from the published MCP setup guide, sign in to the owning account and select Revoke access next to the app. It is a signed-in browser action. Existing orders keep their status.

- [Open Connected Apps](https://simcloud.co.za/oauth/revoke.php)
