Run from source
Running from source is the usual path for development, for reading the code alongside a live server, and for any deployment that wants to build its own image.
Prerequisites: Bun v1.3 or newer, and a database you can reach: MongoDB or PostgreSQL, whichever you would rather run.
-
Clone and install:
Terminal window git clone https://github.com/RedFox-Soft/OAuth-server.ts.gitcd OAuth-server.tsbun install -
Create a
.envfile with the required variables:ISSUER=http://localhost:3000MONGODB_URI=mongodb://localhost:27017DATABASE_NAME=OAuthFor PostgreSQL instead, set
POSTGRES_URLand leave the other two out. The database name is part of that URL, soDATABASE_NAMEis unused:ISSUER=http://localhost:3000POSTGRES_URL=postgres://user:password@localhost:5432/oauthIf you set both connection strings, the server refuses to start instead of picking one; see PostgreSQL. Bun loads
.envautomatically; nothing else needs configuring. Each variable is described in the environment variables reference.ISSUERis the canonical public URL of this server: theissof every token, the base of every endpoint in discovery, and the redirect target of the admin console client. Set it to its final value before the next step. -
Provision the database:
Terminal window bun run db:setup # MongoDBbun run db:setup:pg # PostgreSQL -
Create the keyring directory:
Terminal window mkdir keyringThe server encrypts the secrets it stores with a keyring. When this directory exists, first-run setup writes the keyring it generates here and uses it at once. The keyring below has the other ways to supply one.
-
Bundle the console and the sign-in screens into
public/:Terminal window bun run build -
Start the server:
Terminal window bun startThe server listens on port 3000. Open
http://localhost:3000/adminfor the console, orhttp://localhost:3000/.well-known/openid-configurationfor the discovery document.
What bun run db:setup provisions
Section titled “What bun run db:setup provisions”db:setup runs database/mongodb.ts against MONGODB_URI / DATABASE_NAME, and it
turns an empty database into a working deployment:
- Collections, one per storage area the server writes to.
- Indexes: the TTL indexes that expire tokens, codes, sessions and interactions, and the unique
e-mail index on each project’s end-user collection.
db:setupalso drops stale expiry indexes left on collections that no longer write an expiry field. - The initial RS256 signing key, stored with every issuer’s keys in the
bucketKeysarea, which is why keys are not an environment variable here. (If the server ever starts against an empty key store it generates and persists one itself.) - The admin panel seed: the reserved “Administrators” project with registration closed, the
first-party
admin-panelOAuth client whose redirect URI is$ISSUER/admin/callback, the reservedadmin-mcpagent client, the System team that holds projects no administrator manages, and the defaultredfoxend-user project.
It is idempotent: every write is an upsert or a create-if-absent, so running it again changes
nothing that already exists. Re-run it after upgrading an existing install, since a release that
adds a collection, an index or a seeded document applies it here and nowhere else. Then run
bun run db:migrate, which applies whatever a release has to reshape, as opposed to merely add; the
server refuses to start until the migrations are applied. Some migrations cannot be undone, so take a backup when the
release notes name one.
On PostgreSQL, bun run db:setup:pg does the same job with tables in place of collections. The only
structural difference is expiry: PostgreSQL has no TTL index, so the running server sweeps expired
rows itself. bun run db:migrate is the same command on either backend.
The keyring
Section titled “The keyring”The server encrypts the secrets it stores, such as application secrets and private signing keys, with a keyring: a small JSON document that you hold outside the database. It reaches the server in one of three ways:
-
A keyring file in
keyring/. With the directory from step 4 in place, first-run setup generates the keyring, writes it tokeyring/foxauth.keyringand uses it. Setup shows it to you once, so save a copy then. The repository ignoreskeyring/, so git never commits it. -
The
KEYRINGvariable. Runbun run keyring newand put its output inKEYRING. -
A file elsewhere. Set
KEYRING_FILEto its path. Under systemd, load the keyring withLoadCredential=and pointKEYRING_FILEat it under$CREDENTIALS_DIRECTORY:[Service]LoadCredential=foxauth.keyring:/etc/foxauth/foxauth.keyringEnvironment=KEYRING_FILE=%d/foxauth.keyring%dis systemd’s name for the credentials directory, the same path as$CREDENTIALS_DIRECTORY.
Use one of these. With both KEYRING and a keyring file, the server refuses to start. Without either,
it runs and stores secrets unencrypted, and the console warns about it. Keep a copy of the keyring apart
from your database backups, because the secrets in a backup cannot be read without it.
The keyring explains rotation and what to do if a key is lost.
What the console needs, and when settings apply
Section titled “What the console needs, and when settings apply”- The admin console needs a database-backed deployment. Setting
NODE_ENV=testswaps in the in-memory adapter, which does not persist the seed across restarts. - Server settings are read at boot from the config store, and a change saved in the console applies at once on the instance that saved it; other instances take it at their next restart. Every setting is listed in the settings reference.