# Using the auth package on its own (https://docs.opensequence.dev/sdks/auth-package)

How to use @opensequence/auth on its own: store API keys and mailbox passwords encrypted, hand them to adapters, and rotate keys.

`@opensequence/auth` is the part of OpenSequence that stores provider secrets: a tenant's API keys (Allo, Attio) and each user's mailbox settings (SMTP and IMAP passwords). It has no idea what a sequence is. Tenants and owners are opaque strings, so it works for any application that keeps third-party secrets encrypted at rest and hands them only to the code that calls the provider.

```ts
import { createAuth, runAuthMigrations } from '@opensequence/auth';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const db = drizzle(postgres(process.env.DATABASE_URL!));
await runAuthMigrations(db);                                       // creates the `seq_auth` schema

const auth = createAuth({
  db,
  encryptionKeys: process.env.SEQ_ENCRYPTION_KEYS!,                // "id:base64key[,id:base64key…]"
});

// once per tenant: a workspace API key, probed before it is stored
await auth.registerCredential({ tenantId: 'acme', provider: 'attio', method: 'api_key', secrets: { api_key }, probe });

// per user: their mailbox, after your code has checked the logins work
const connection = await auth.savePasswordConnection({
  tenantId: 'acme',
  ownerId: 'u_42',
  provider: 'smtp',
  account: 'max@acme.com',
  config: { smtp: { host: 'smtp.gmail.com', port: 465 }, imap: { host: 'imap.gmail.com', port: 993 } },
  secrets: { smtp_password, imap_password },
});

// only in the adapter that talks to the mail server
const { account, config, secrets } = await auth.connectionSecrets(connection.id);
```

## What it does [#what-it-does]

| Area | Behaviour |
|---|---|
| Credentials (tenant level) | `registerCredential({ tenantId, provider, method: 'api_key', secrets, probe? })` stores the key encrypted. An optional `probe` checks it against the provider first and refuses it with `INVALID_CREDENTIAL` when it fails. `credentialStatus` and `listCredentials` return the status, key id, last four characters and timestamps, never the secret. `credentialsFor(tenantId, provider)` returns the decrypted map, for adapters only |
| Connections (owner level) | `savePasswordConnection` stores one connection per owner and provider: the passwords encrypted together, the rest as plain config. Saving again replaces the settings. `connectionSecrets(connectionId)` returns them for adapters only, and answers `CREDENTIALS_NEEDED` once the connection is disconnected |
| Health | `healthCheckCredential(tenantId, provider, probe)` re-runs a probe and marks the credential `invalid` when it fails. `health(connectionId)`, `connectionStatus` and `listConnections(tenantId, ownerId?)` report connections |
| Revoke | `revoke(connectionId)` deletes the stored passwords and sets the connection `disconnected` |
| Key rotation | `rotateKeys()` re-encrypts every credential and connection not under the first key of `encryptionKeys`, so older entries can be removed from the list |

## Errors [#errors]

`AuthError` with a stable `code` and an HTTP status: `INVALID_CREDENTIAL` and `MISSING_SECRET` (422), `CREDENTIAL_INVALID`, `CREDENTIALS_NEEDED` and `CONFLICT` (409), `NOT_FOUND` (404).

## Storage [#storage]

Two tables in the `seq_auth` schema, `credentials` and `connections`, created by `runAuthMigrations(db)` (idempotent). Secrets are AES-256-GCM under the first key; each row carries its key id. The exported `authSchema`, `credentialsTable` and `connectionsTable` let you join from your own Drizzle code. `AesGcmCipher` and `parseEncryptionKeys` are exported for tests and custom wiring; pass your own `clock` to `createAuth` to control time.

The package is destined for its own repository (`open-sequence/auth`); it never imports the engine.
