oauth2-server is a complete, framework-agnostic, and well-tested module for implementing an OAuth2 Authorization Server in Node.js. It adheres to RFC 6749 (OAuth 2.0 Authorization Framework) and RFC 6750 (Bearer Token Usage), providing the core logic for handling various OAuth 2.0 grant types including `authorization_code`, `client_credentials`, `refresh_token`, and `password` grants, as well as support for custom extension grants and scopes. The library is currently at version 3.1.1 and is under active maintenance, with recent releases focusing on bug fixes and dependency updates after a period of hiatus. It distinguishes itself by offering a robust, compliant foundation that can be integrated with any Node.js HTTP framework (like Express or Koa via official wrappers), supporting promises, Node-style callbacks, and async/await for model interactions. It doesn't dictate a specific storage mechanism, allowing developers to plug in their preferred database (e.g., PostgreSQL, MongoDB, Redis).
npm install oauth2-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic OAuth2 server using Express.js and `oauth2-server`, demonstrating `password`, `client_credentials`, `refresh_token`, and `authorization_code` grants with an in-memory data model. It includes token and authorization endpoints, and a protected resource. This minimal model is for illustration; a real application requires persistent storage.
Consult the official 'Migrating from 2.x to 3.x' guide in the `oauth2-server` documentation. Re-evaluate server options and adapt your model's method signatures and return types to align with the v3 specification.
Design your application to comply with OAuth 2.1 best practices: use Authorization Code Grant with PKCE for all client types, avoid ROPC and Implicit grants, enforce strict redirect URI validation, and transmit tokens only in the `Authorization` header. `oauth2-server` provides the building blocks, but you must configure your implementation to follow these security standards.
Always enforce strict, exact matching for registered `redirect_uri` values. Ensure all redirect URIs are pre-registered and validated against an allow-list on your authorization server. Never accept arbitrary redirect URIs.
Generate a unique, cryptographically secure random string for the `state` parameter for each authorization request. Store this state securely (e.g., in a session or cookie) and verify it upon receiving the callback from the authorization server to ensure the response belongs to the original request.
Thoroughly review the `oauth2-server` model specification and ensure all required methods for your chosen grant types are implemented correctly, returning the expected data formats. Use console logging and unit tests for your model implementation to catch issues early.
Ensure your `model` object passed to `new OAuth2Server()` includes an asynchronous function `getAccessToken(accessToken)` that returns a token object if valid, or `null` otherwise. Review the model specification for all required methods based on your enabled grants.
For authorization codes, ensure they are single-use and quickly invalidated after first use. Verify expiry times for all tokens. Double-check that the `client_id` and `client_secret` used for token exchange match the client that initiated the authorization flow. Inspect logs for more specific reasons (e.g., 'code expired').
Ensure the `redirect_uri` parameter in the client's authorization request is an exact string match for one of the `redirectUris` associated with the `client_id` in your OAuth2 model. All valid `redirectUris` should be explicitly listed and strictly validated.
If your project is ES Module-based, use `import OAuth2Server from 'oauth2-server';`. If your project is CommonJS, use `const OAuth2Server = require('oauth2-server');`. Ensure your `package.json`'s `type` field and module syntax are consistent. Consider using a bundler like Webpack or Rollup for complex module environments.No dependency data recorded yet.