# Connected apps

Run the OAuth consent page for outside apps that ask to use the account, and list and disconnect connected apps.

> **In development, coming soon**
>
> Connected apps are in development, coming soon.

`client.connectedApps` connects outside apps, such as an AI assistant, to the account with OAuth ("Sign in with Polyester"), and lists and disconnects them. A connected app uses the account as the signed-in website does, for the scopes the account granted. It never moves money out of the account and never changes its security settings. A connection lasts until the account or the app disconnects it, or until it expires; signing out does not end it.

Every method needs a session: the website's consent page and settings call them, and API keys are refused. Tree-shaken builds reach the same service with `connectedAppsService(core)` from `@polyester/sdk/services/connected-apps`.

## Methods

| Method                     | Summary                                                   |
| -------------------------- | --------------------------------------------------------- |
| `getConnectionRequest`     | Read a pending connection request for the consent page.   |
| `approveConnectionRequest` | Approve a pending request and get the app's redirect URL. |
| `denyConnectionRequest`    | Deny a pending request and get the app's redirect URL.    |
| `list`                     | List the apps connected to the account, newest first.     |
| `revoke`                   | Disconnect an app.                                        |

## Consent page

An app that asks to connect sends the browser to the consent page with a `request` query parameter. Pass it as `requestId` to the three connection request methods. An unknown, expired, or already answered request throws `ResourceNotFoundError`.

### `getConnectionRequest(input, options?)`

Returns the pending request as an `AppConnectionRequest`. Show the app's `client.name`, the `scopes` it asks for, and the `redirectHost` that receives the answer before the user decides.

```ts
const requestId = new URL(window.location.href).searchParams.get("request") ?? "";
const request = await client.connectedApps.getConnectionRequest({ requestId });
console.log(request.client.name, request.scopes, request.redirectHost);
```

```ts
import type { AppConnectionRequest } from "@polyester/sdk";

interface AppConnectionRequest {
	client: { clientId: string; name: string };
	scopes: string[]; // OAuth scopes the app asks for, e.g. "account:read"
	resource: string; // absolute URL of the server the app will use the account through
	redirectHost: string; // host the app receives the answer on
	expiresAt: number | null; // epoch ms; after it the request can no longer be approved or denied
}
```

`"account:read"` lets the app read the account as the signed-in website does: balances, orders, trades, and account settings.

### `approveConnectionRequest(input, options?)`

Approves the request and returns `{ redirectUrl }`. Send the browser there: it is the app's redirect URL carrying a one-time authorization code, valid for 60 seconds. Connecting an app is durable delegated access, so the account needs enrolled MFA and the call a fresh `stepUpToken`; see [MFA](https://testnet.polyester.com/docs/sdk/typescript/reference/mfa).

```ts
const { redirectUrl } = await client.connectedApps.approveConnectionRequest(
	{ requestId },
	{ stepUpToken }
);
window.location.assign(redirectUrl);
```

An account with 20 connected apps throws `PreconditionFailedError` with `detail.code === "AUTH_CONNECTED_APP_LIMIT_REACHED"`: disconnect an app before connecting another.

### `denyConnectionRequest(input, options?)`

Denies the request and returns `{ redirectUrl }`, the app's redirect URL carrying the `access_denied` error. Send the browser there.

```ts
const { redirectUrl } = await client.connectedApps.denyConnectionRequest({ requestId });
window.location.assign(redirectUrl);
```

## Connected apps

### `list(options?)`

Returns the account's `ConnectedApp[]`, newest connection first, at most 20. A disconnected or expired connection is not listed.

```ts
const apps = await client.connectedApps.list();
for (const app of apps) {
	console.log(app.client.name, app.scopes, app.lastUsedAt);
}
```

```ts
import type { ConnectedApp } from "@polyester/sdk";

interface ConnectedApp {
	connectedAppId: string; // pass to revoke
	client: { clientId: string; name: string };
	scopes: string[]; // OAuth scopes the account granted
	resource: string; // absolute URL of the server the app uses the account through
	connectedAt: number | null; // epoch ms
	lastUsedAt: number | null; // last use or refresh, at minute precision; null until first use
	expiresAt: number | null; // the connection ends then unless it is disconnected first
}
```

When a connection expires, the app must be connected again.

### `revoke(input, options?)`

Disconnects the app by `connectedAppId`. Its access and refresh tokens stop working at once. Disconnecting an app that is already disconnected or expired succeeds; an ID that is not one of the account's connected apps throws `ResourceNotFoundError`.

```ts
await client.connectedApps.revoke({ connectedAppId: app.connectedAppId });
```

## Related

- [MFA](https://testnet.polyester.com/docs/sdk/typescript/reference/mfa) for the step-up proof that approving a request needs.
- [Account services](https://testnet.polyester.com/docs/sdk/typescript/reference/account-services) for the other account services.
- [Errors](https://testnet.polyester.com/docs/sdk/typescript/reference/errors) for the error type reference.
