|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Scope |
| 4 | + |
| 5 | +These instructions apply only to the `node-oauth2-server` repository, the framework-agnostic OAuth 2.0 server library |
| 6 | +published as `@node-oauth/oauth2-server`. |
| 7 | + |
| 8 | +The sibling `express-oauth-server` repository is outside this file’s scope. Release CI checks out the Express adapter |
| 9 | +as an external integration-test target; it is not a runtime dependency of this package. Changes to the adapter’s code, |
| 10 | +documentation, security policy, and release process are outside this scope. |
| 11 | + |
| 12 | +Run package commands from the `node-oauth2-server` repository root; do not expect a parent workspace-level install or |
| 13 | +test command. Before changing the package, check `README.md`, `CONTRIBUTING.md`, `SECURITY.md`, and `package.json`. |
| 14 | + |
| 15 | +## AI context exclusions |
| 16 | + |
| 17 | +Before exploring the repository, read [`.aiignore`](./.aiignore) and apply its patterns using gitignore syntax, |
| 18 | +relative to this repository root. Agents must follow these exclusions explicitly: exclude matching paths from routine |
| 19 | +file listings, searches, reads, and manual edits, including when using tools that do not honor `.aiignore` themselves. |
| 20 | +Access an excluded path only when the user’s task specifically requires it. Build, test, coverage, and documentation |
| 21 | +commands may create or update excluded output; use the source files when making changes to generated content. |
| 22 | + |
| 23 | +## Project priorities |
| 24 | + |
| 25 | +- Preserve OAuth 2.0 interoperability and security. Review behavior against the relevant standards, especially RFC 6749, |
| 26 | + RFC 6750, RFC 7636, RFC 8252, and the threat-model guidance in RFC 6819. |
| 27 | +- Keep `node-oauth2-server` framework-agnostic. Framework-specific behavior belongs in adapters maintained separately |
| 28 | + from this repository. |
| 29 | +- Treat authentication, authorization, token, grant, PKCE, parsing, and error-handling changes as security-sensitive. |
| 30 | + Avoid weakening validation or disclosure of credentials and tokens in logs, errors, tests, or documentation. |
| 31 | +- The public API includes JavaScript entry points and TypeScript declarations. Update `index.d.ts` when a public API or |
| 32 | + type contract changes. |
| 33 | +- The package uses CommonJS entry points and Promise/`async`-oriented APIs. Preserve existing public behavior unless a |
| 34 | + change is intentional, documented, and tested. |
| 35 | + |
| 36 | +## Repository layout |
| 37 | + |
| 38 | +- Runtime code: `index.js` and `lib/` |
| 39 | +- Type declarations: `index.d.ts` |
| 40 | +- Tests: `test/` (Mocha files matching `test/**/*_test.js`) |
| 41 | +- Test suites: `test/unit/`, `test/integration/`, and `test/compliance/`; shared fixtures/helpers: `test/helpers/` |
| 42 | +- Mocha configuration: `.mocharc.yml`; shared Chai assertions: `test/assertions.js` |
| 43 | +- Hand-authored documentation: `docs/guide/` and the VitePress home/configuration files |
| 44 | +- Generated API documentation: `docs/api/` |
| 45 | +- Formatting and linting: Biome, configured in `biome.json` |
| 46 | +- CI and release validation: `.github/workflows/` |
| 47 | + |
| 48 | +## Library architecture |
| 49 | + |
| 50 | +The library implements OAuth workflows without owning HTTP routing, a database, user sessions, or a consent UI. |
| 51 | +Applications and framework adapters supply those pieces. The main flow is: adapter creates `Request`/`Response` |
| 52 | +instances, calls `OAuth2Server`, and sends the populated response; handlers delegate persistence and policy to the |
| 53 | +application’s model. |
| 54 | + |
| 55 | +- **Public facade:** `index.js` exports the `OAuth2Server` constructor, `Request`, `Response`, `AbstractGrantType`, and |
| 56 | + error classes. `lib/server.js` exposes `authenticate()`, `authorize()`, and `token()`. Each call merges method |
| 57 | + defaults, constructor options, and per-call options in that order, creates a handler, and returns its Promise. |
| 58 | + `index.d.ts` describes the public TypeScript contracts. |
| 59 | +- **HTTP boundary:** `lib/request.js` wraps already-parsed headers, method, query, and body; `lib/response.js` holds |
| 60 | + status, headers, and body. Headers are normalized to lowercase. Handlers require these wrapper instances and mutate |
| 61 | + the response without sending it. The caller must handle both the resolved result or rejected error and the response |
| 62 | + state, including redirects and authentication headers. |
| 63 | +- **Resource authentication:** `lib/handlers/authenticate-handler.js` extracts a bearer token, rejects conflicting |
| 64 | + token sources, calls `model.getAccessToken()`, checks expiry, and calls `model.verifyScope()` when scopes are |
| 65 | + requested. It returns the stored token object and manages scope and `WWW-Authenticate` headers. |
| 66 | +- **Authorization endpoint:** `lib/handlers/authorize-handler.js` validates the client, redirect URI, state, and scope, |
| 67 | + obtains the user through a supplied `authenticateHandler` (or bearer authentication by default), and saves an |
| 68 | + authorization code through the model. `lib/response-types/code-response-type.js` builds the code redirect. Only the |
| 69 | + `code` response type is registered; `token-response-type.js` is an unimplemented placeholder. |
| 70 | +- **Token endpoint:** `lib/handlers/token-handler.js` validates form-encoded POST requests, obtains client credentials, |
| 71 | + delegates client authentication to `model.getClient()`, checks the client’s allowed grants, and dispatches by |
| 72 | + `grant_type`. The saved token data is returned to the caller; `lib/models/token-model.js` validates and prepares it |
| 73 | + for `lib/token-types/bearer-token-type.js` to serialize into the OAuth response, with cache-prevention headers. |
| 74 | + Internal scopes are arrays; the token response converts them to a space-delimited string. Bearer is the issued token |
| 75 | + type; `mac-token-type.js` is an unimplemented placeholder. |
| 76 | +- **Grant implementations:** `lib/grant-types/` contains authorization-code, client-credentials, password, and |
| 77 | + refresh-token workflows. `AbstractGrantType` shares token generation, expiration, and scope handling. Grants obtain |
| 78 | + users and credentials through the model and persist results through `saveToken()`. Custom grant constructors are |
| 79 | + registered through `extendedGrantTypes` and implement `handle(request, client)`; the exported base class provides |
| 80 | + reusable helpers. Client-specific token lifetimes override server defaults. |
| 81 | +- **Storage and policy boundary:** `options.model` is an application-supplied object whose required methods depend on |
| 82 | + the selected workflow. Hooks cover client/user lookup, token/code lookup and persistence, revocation, scope policy, |
| 83 | + redirect validation, and optional token/code generation. Hooks may return plain values or Promises. `lib/model.js` |
| 84 | + documents this contract and provides throwing stubs and a `Model.from()` helper; it is not a persistence layer and |
| 85 | + is not exported by the package entry point. In particular, `getClient()` must enforce per-client secret policy; |
| 86 | + PKCE and grant-level secret-presence options do not replace confidential-client authentication. |
| 87 | +- **Security-sensitive sequencing:** authorization-code exchange validates the stored code and client, revokes the |
| 88 | + code, then checks PKCE and redirect binding before issuing tokens. A failed PKCE attempt therefore consumes the code. |
| 89 | + Refresh exchange validates token ownership, expiry, and requested scope before revocation; scopes cannot expand, |
| 90 | + and refresh-token rotation is enabled by default. Preserve these orderings when modifying grant logic. |
| 91 | +- **Shared protocol support:** `lib/pkce/pkce.js` and `lib/utils/` provide PKCE, cryptographic token generation, scope |
| 92 | + parsing, and date/string helpers. Default token generation uses Node’s cryptographic random bytes; model hooks may |
| 93 | + override it. PKCE defaults to `S256`, allows `plain` only when explicitly enabled, and can be required through |
| 94 | + `requirePKCE`. `lib/errors/` defines the `OAuthError` hierarchy with OAuth names and HTTP status codes; handlers |
| 95 | + translate errors into endpoint-specific bodies, redirects, or headers and reject the operation. Token and |
| 96 | + authorization handlers normalize caught programming/configuration errors to `ServerError`. |
| 97 | + |
| 98 | +## Setup and validation |
| 99 | + |
| 100 | +Use the lockfile and install dependencies from the repository root: |
| 101 | + |
| 102 | +```bash |
| 103 | +npm ci |
| 104 | +``` |
| 105 | + |
| 106 | +If you change dependencies, regenerate `package-lock.json` with npm 11 (the version CI uses), including when resolving merge conflicts. Older npm versions drop the cross-platform optional dependencies, and `npm ci` then fails in CI even though it works locally. |
| 107 | + |
| 108 | +Validation commands: |
| 109 | + |
| 110 | +```bash |
| 111 | +npm test # pretest runs Biome check, then Mocha runs all three test suites |
| 112 | +npm run lint |
| 113 | +npm run format |
| 114 | +npm run test:coverage |
| 115 | +npm run docs:build # for documentation or public JSDoc changes |
| 116 | +``` |
| 117 | + |
| 118 | +Code style is enforced by the Biome linter and formatter configured in `biome.json`. Both `npm run lint` and |
| 119 | +`npm run format` must pass before every commit, along with `npm test`. Use `npm run lint:fix` and `npm run format:fix` |
| 120 | +only when you intend to apply automated changes. The configured formatter uses single quotes, spaces, trailing |
| 121 | +commas, and a 120-column line width for JavaScript. |
| 122 | + |
| 123 | +Biome currently checks `index.js`, `lib/**/*.js`, and `test/**/*.js`. A passing Biome check does not validate Markdown, |
| 124 | +TypeScript declarations, documentation tooling, or workflow files; review those changes separately. |
| 125 | + |
| 126 | +Write tests with Mocha and Chai, using Chai’s `should` style assertions. Use Sinon for stubbing, and restore stubs and |
| 127 | +fake timers after each test so they cannot affect other tests. Reuse the existing test helpers and shared assertions. |
| 128 | + |
| 129 | +Run focused tests during iteration, but run the full package test, lint, and format commands before submitting a |
| 130 | +change. Add or update tests for every bug fix and feature. Include regression coverage for security-sensitive |
| 131 | +behavior and boundary cases. |
| 132 | + |
| 133 | +For a focused run, use `npm test -- --grep 'suite or test name'`; this retains the Biome `pretest` check. |
| 134 | + |
| 135 | +Measure test coverage with nyc (Istanbul) via `npm run test:coverage`. Strive for the highest possible test coverage |
| 136 | +across statements, branches, functions, and lines, including error paths and boundary cases. The test workflows |
| 137 | +currently enforce a minimum coverage of 98% using `coverage/lcov.info`. The coverage command does not run the |
| 138 | +`pretest` hook, so it does not replace the required Biome checks. |
| 139 | + |
| 140 | +`package.json` is the source of truth for supported Node.js versions. Follow its `engines.node` declaration when |
| 141 | +developing, rather than prescribing a separate version policy here. If a change relies on a newer Node.js feature, |
| 142 | +verify it against that declaration and document any intentional compatibility change. Development tools may require a |
| 143 | +newer Node.js version than the library’s runtime minimum; check their engine requirements if setup fails without |
| 144 | +changing the library’s support policy merely to match the local toolchain. |
| 145 | + |
| 146 | +## Source and documentation workflow |
| 147 | + |
| 148 | +- Add or update JSDoc for new or changed public APIs and keep `index.d.ts` in sync. |
| 149 | +- Do not manually edit files under `docs/api/`. Update source JSDoc in `lib/`, then run `npm run docs:api` from the |
| 150 | + repository root. |
| 151 | +- Edit the guide under `docs/guide/` directly. `npm run docs:setup` creates the contribution-guide link used by the docs |
| 152 | + build; edit the root `CONTRIBUTING.md` rather than `docs/guide/contributing.md`. |
| 153 | +- Use the `docs:*` scripts in `package.json`; the `npm run docs` example in `CONTRIBUTING.md` is outdated. |
| 154 | +- Keep README, API documentation, migration notes, and type declarations aligned with behavior. Do not update generated |
| 155 | + output without checking whether the corresponding source change is present. |
| 156 | +- Do not edit `CHANGELOG.md` or bump release versions for ordinary feature or bug-fix pull requests; maintainers handle |
| 157 | + release bookkeeping. |
| 158 | + |
| 159 | +## Contributions and Git workflow |
| 160 | + |
| 161 | +Follow [`CONTRIBUTING.md`](./CONTRIBUTING.md) for the established contribution process, including opening or discussing |
| 162 | +an issue before an ordinary feature or bug-fix pull request. Follow the private reporting process below for security |
| 163 | +issues. The branching policy below takes precedence over the guide’s outdated `development` instructions. |
| 164 | + |
| 165 | +Use trunk-based development with `master` as the trunk. The `development` branch is deprecated. Create feature or fix |
| 166 | +branches from `master`, keep them closely aligned with it, and target pull requests to `master`. The `master` name is |
| 167 | +retained for historical continuity. |
| 168 | + |
| 169 | +Follow the commit message convention in [`CONTRIBUTING.md`](./CONTRIBUTING.md). |
| 170 | + |
| 171 | +Keep commits focused, avoid unrelated dependency updates, describe test/reproduction steps in pull requests, and do not |
| 172 | +include secrets or real credentials in fixtures, examples, logs, or documentation. |
| 173 | + |
| 174 | +## Security reporting |
| 175 | + |
| 176 | +Never report a suspected vulnerability in a public issue, pull request, or discussion. Follow |
| 177 | +[`SECURITY.md`](./SECURITY.md), which directs reports to GitHub private vulnerability reporting. Before releasing a |
| 178 | +security fix, the policy requires the full test suite and dependency audits to pass. |
0 commit comments