node-oauth / express-oauth-server

🔒 Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with express in node.js
https://www.npmjs.com/package/@node-oauth/express-oauth-server
MIT License
27 stars 8 forks source link
authorization-code-grant client-credentials-grant express express-js express-middleware node nodejs npm-package oauth oauth2 password-grant token-grant

Express OAuth Server

Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with express in Node.js.

[![Tests](https://github.com/node-oauth/express-oauth-server/actions/workflows/tests.yml/badge.svg)](https://github.com/node-oauth/express-oauth-server/actions/workflows/tests.yml) [![CodeQL](https://github.com/node-oauth/express-oauth-server/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/node-oauth/express-oauth-server/actions/workflows/github-code-scanning/codeql) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![npm Version](https://img.shields.io/npm/v/@node-oauth/express-oauth-server?label=version)](https://www.npmjs.com/package/@node-oauth/oauth2-server) [![npm Downloads/Week](https://img.shields.io/npm/dw/@node-oauth/express-oauth-server)](https://www.npmjs.com/package/@node-oauth/oauth2-server) ![GitHub](https://img.shields.io/github/license/node-oauth/express-oauth-server)
[API Docs](https://node-oauth.github.io/express-oauth-server/) · [NPM Link](https://www.npmjs.com/package/@node-oauth/express-oauth-server) · [Node OAuth2 Server](https://github.com/node-oauth/node-oauth2-server)

About

This package wraps the @node-oauth/oauth2-server into an express compatible middleware. It's a maintained and up-to-date fork from the former oauthjs/express-oauth-server.

Installation

$ npm install @node-oauth/express-oauth-server

Quick Start

The module provides two middlewares - one for granting tokens and another to authorize them. @node-oauth/express-oauth-server and, consequently @node-oauth/oauth2-server, expect the request body to be parsed already. The following example uses body-parser but you may opt for an alternative library.

const bodyParser = require('body-parser');
const express = require('express');
const OAuthServer = require('@node-oauth/express-oauth-server');

const app = express();

app.oauth = new OAuthServer({
  model: {}, // See https://github.com/node-oauth/node-oauth2-server for specification
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(app.oauth.authorize());

app.use(function(req, res) {
  res.send('Secret area');
});

app.listen(3000);

Options

Note: The following options extend the default options from @node-oauth/oauth2-server! You can read more about all possible options in the @node-oauth/oauth2-server documentation

const options = { 
  useErrorHandler: false, 
  continueMiddleware: false,
}

Migration notes

Beginning with version 4.0 this package brings some potentially breaking changes:

More Examples

For more examples, please visit our dedicated "examples" repo , which also contains express examples.

License

MIT, see license file.