Skip to content
Udibo

Move a PostgreSQL app from oauth2-server 3.x

The Express app in this example issues tokens through oauth2-server 3.x and protects an API at /secret. It stores its users, client registrations and tokens in PostgreSQL. You can replace the authorization server library and keep running sign-in yourself (Phase A). Moving sign-in and users to Udibo Identity is a separate cutover (Phase B). Worked PostgreSQL example

Step tagWhat changes in the appEvidence
step-0-upstreamThe runnable upstream example signs users in, issues tokens and checks them at the API.Baseline tests
step-1-udibo-packageExpress uses @udibo/oauth2 0.45.1 with PostgreSQL stores. The source still owns the users.Library replacement · Database cutover tests
step-2-udibo-idpThe app becomes a confidential OAuth client. Its API checks the configured issuer’s JWT access tokens and audience.Sign-in and API changes
step-3-user-importTwo commands export users for import and retire the source users table.Export change · Retirement change

Compare the first two transitions on GitHub. Library replacement · Issuer change

To compare the export code and tests in your checkout, run git diff step-2-udibo-idp step-3-user-import -- scripts test.

Phase A — replace the library on your own server

Inventory clients that send a username and password to the token endpoint. Step 1 refuses the password grant. Move interactive clients to authorization code with S256 PKCE and state. Give machine clients a confidential registration with the client-credentials grant. For an interactive client, test sign-in, refresh and access to the protected API. For a machine client, request a token with client credentials and call the API with it. Protocol test changes

Rehearse the schema migration on a copy of the source database. At cutover, stop step 0, take a backup, then run the step-1 migration before starting the new server. It replaces stored tokens and client secrets with digests. Existing unexpired access and refresh tokens can still work. Outstanding authorization codes are discarded, and stopping the app clears its in-memory login sessions. Run step 1 in the same time zone as step 0 so the migration interprets stored expiry times correctly. Rolling back requires restoring the backup, which loses tokens issued after cutover. Cutover checklist and exceptions

Phase B — choose the issuer and rehearse the import

Register the web app in a synthetic Udibo development tenant. Register its exact callback URL, enable the authorization-code grant, allow the openid and profile scopes, and configure JWT access tokens for the API’s audience. Set AUTH_ISSUER, AUTH_CLIENT_ID, AUTH_CLIENT_SECRET, AUTH_REDIRECT_URI and AUTH_AUDIENCE in the app’s environment. Give it a fresh SESSION_SECRET. Test sign-in through /login, the signed-in page at /home, and a bearer-token request to /secret. Tenant setup

For a local rehearsal, check out step-3-user-import and use Node.js 22.12 or later. Set DATABASE_URL to a disposable PostgreSQL database. The setup command drops and recreates the example tables. Run the commands in a shell with AUTH_ISSUER unset so the behaviour tests start their own local identity provider.

Shell
npm ci
npm run db:setup
npm test

Before export, identify passwords longer than 72 bytes in the source without logging them. Bcrypt uses only the first 72 bytes. Arrange account recovery for affected users. Freeze source account and password changes before the export, or agree with the import operator how to reconcile them. Set export_dir to a private directory outside the checkout. Create the directory first, exclude it from automatic backups and sync, and choose an unused filename.

Shell
npm run export:users -- "${export_dir:?}/users.ndjson"

The source example stores plaintext passwords. Its exporter generates bcrypt hashes at cost 12 by default and writes a username and legacyCredential for each user. The file carries neither the old user ID nor an email address. Treat it as a credential. Arrange a secure handoff and dry run with Udibo, then rehearse a small cohort. Reconcile imported, skipped and refused records. After verifying the import, delete the export file and any copies made during handoff. A repeat import skips a taken username, so it cannot apply a changed password. Verify the destination subject before mapping an old user ID to it. Plan recovery for users without email. Username-only imports

Move clients before retiring PostgreSQL users

Register each destination client with its own ID and allowed grants. Give a new secret to confidential clients. Public clients use PKCE without a secret. The tagged app checks one issuer. To move clients one at a time, rehearse a transition where the API validates Udibo JWTs for its audience and checks old opaque tokens against the step-1 store. Restrict that old reader to the client IDs this API serves because those tokens carry no audience. If the first check rejects or cannot load its keys, the second must still run. Grant access only after a check succeeds. The two-server sequence is a deployment procedure to test in your environment. Client switch-over

Verify first sign-in and access to the right app data for the imported cohort. Move the remaining clients, including machine clients. The step-1 client-credentials grant still consults the users table. Stop old token issuance and account for outstanding access and refresh tokens before decommissioning their readers and stores. Once the import is verified and every client that needs the users table has moved, the example retires it with npm run db:retire-users -- --confirm. This drops the table. Read the command’s database-maintenance warnings and account for copies in backups and WAL. Retirement procedure

What was verified

  • On Node 22 and 24, CI ran the export command on two seeded users, checked the records, ran confirmed retirement on disposable PostgreSQL 17 and checked that the users table was gone. This command coverage landed after the step-3 tag. Command CI

  • The November 30 hosted development-tenant run exercised sign-in and the protected API with a preconfigured synthetic user. Twenty tests passed. The access-token expiry test skipped because the tenant lifetime exceeded the test’s wait. Hosted run

  • On November 5, one manual rehearsal imported two synthetic users into a local Udibo development server. First sign-in upgraded their export-generated bcrypt hashes. The hosted import handoff and first sign-in after a hosted import still need a tenant rehearsal. Manual rehearsal record

Last verified 2026-11-30.

View Markdown