# Authentication

> Choose between browser sign-in and an API key, request the access your client needs and keep a session alive.

Source: https://docs.corfedo.ai/connect/authentication/

A client connects to one workspace. A person approves it in the browser, or the client holds an API key that a person created. Either way the client gets at most that person's access, limited to the repositories chosen for it.

## Choose a credential

| Client | Credential |
| --- | --- |
| Coding agent over MCP | Browser sign-in started by the agent. See [Connect over MCP](https://docs.corfedo.ai/connect/mcp/) |
| CLI used by a person | Browser sign-in with `corfedo auth login`. See [Sign in and profiles](https://docs.corfedo.ai/connect/cli/sign-in-and-profiles/) |
| CLI in CI and scripts | API key file |
| Application run by a person | Browser sign-in through an SDK, with session renewal |
| Service or headless agent | API key file |

### Browser sign-in

The client opens the workspace's **Authorize** page, where the person chooses repositories and selects **Allow**. Under **Permissions**, every [permission](#permissions) the client asks for is a checked box the person can clear. See [Approve access](https://docs.corfedo.ai/connect/mcp/clients/#approve-access). Signed-in clients appear under **Connect** › **Manage connections**, where the person can revoke them.

### API keys

A person creates a key in **Connect** › **Manage connections** with **Create API key** and downloads the key file once. Keep it out of source code and pass its path to the SDK's key-file constructor, shown on each [SDK page](https://docs.corfedo.ai/connect/sdks/typescript/), or to [`corfedo auth configure-api-key`](https://docs.corfedo.ai/connect/cli/sign-in-and-profiles/#use-an-api-key-file).

A key keeps the permissions and repositories chosen when it was created. **Ask Corfedo AI** needs **Search and read code**. A key can select a repository where you hold at least one of its permissions.

A key can also have its own allowed networks, within the [networks the workspace allows](https://docs.corfedo.ai/workspace/client-access/#limit-an-api-key-to-networks). From another network, requests fail with `network_not_allowed` (HTTP 403) and token requests with `unauthorized_client` or `invalid_grant`.

## Permissions

The permissions are **Search and read code**, **Dependencies**, **Security findings**, **Ask Corfedo AI**, **Read saved contexts**, **Manage your saved contexts** and **Manage workspace contexts**. [Permissions and scopes](https://docs.corfedo.ai/reference/permissions-and-scopes/#client-scopes) lists each with its OAuth scope. On each repository chosen for the client, it reads only what you can read there.

## Request access to Corfedo AI

A workspace administrator turns Corfedo AI on per kind of client in [Client access](https://docs.corfedo.ai/workspace/client-access/), and the client keeps **Ask Corfedo AI** and **Search and read code**:

| Client | Turned on with |
| --- | --- |
| CLI, SDKs and API keys | **Corfedo AI** under **SDK and API** |
| Coding agents | **Corfedo AI** under **MCP clients**, see [MCP tools](https://docs.corfedo.ai/connect/mcp/tools/#ask-corfedo-ai) |

While it is off, asking returns `permission_denied` (HTTP 403) and keys cannot be created with **Ask Corfedo AI**.

An SDK requests the `corfedo:ai:invoke` scope next to repository access. For TypeScript:

```ts
import { CorfedoAuthentication, CorfedoClient } from '@corfedo/client';

const credentials = await CorfedoAuthentication.signIn({
  apiBaseUri: 'https://corfedo.example.com/',
  scopes: [
    'corfedo:repositories:code:read',
    'corfedo:repositories:dependencies:read',
    'corfedo:ai:invoke',
    'offline_access',
  ],
});
const client = new CorfedoClient(credentials);
```

For Python, set `HumanAuthenticationOptions.scopes` to a tuple with the same values. For .NET, set `HumanAuthenticationOptions.Scopes`. `offline_access` lets the session be renewed.

SDK sign-in requests code and dependency read by default. Add `corfedo:repositories:security:read` for security findings. Asking also needs `corfedo:repositories:code:read`. A missing scope returns `missing_scope` (HTTP 403), `AccessDenied` with the missing `scope` in the SDKs.

## Keep a human session alive

Access tokens are short-lived. Without renewal, a client built from human credentials fails with `AuthenticationRequired` when they expire. Then create a client with fresh credentials and [reattach](https://docs.corfedo.ai/connect/api/ai/streaming/#recover-a-subscription) to the run.

For long-lived use, pass the SDK a renewal callback. Your application stores the credentials. Refresh tokens rotate and cannot be reused, so the callback must lock your session store, return a replacement another process already stored, or else refresh once and store the result:

```python
from corfedo import CorfedoAuthentication, CorfedoClient, CorfedoCredentials

async def renew(current: CorfedoCredentials) -> CorfedoCredentials:
    async with store.lock():
        stored = await store.load()
        if stored.access_token != current.access_token:
            return stored
        replacement = await CorfedoAuthentication.refresh(current)
        await store.save(replacement)
        return replacement

client = CorfedoClient(credentials, renew=renew)
```

If the callback fails, that and every later call fail with `ReauthorizationRequired`. Sign in again and create a new client. To survive a process restart, store the proof key with the credentials, because the default one lives only in memory. With renewal, a stream that ends with `credential_expired` reattaches on its own.
