Using the auth package on its own

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

Edit on GitHub

@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.

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

AreaBehaviour
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
HealthhealthCheckCredential(tenantId, provider, probe) re-runs a probe and marks the credential invalid when it fails. health(connectionId), connectionStatus and listConnections(tenantId, ownerId?) report connections
Revokerevoke(connectionId) deletes the stored passwords and sets the connection disconnected
Key rotationrotateKeys() re-encrypts every credential and connection not under the first key of encryptionKeys, so older entries can be removed from the list

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

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.

On this page