Gathering detailed insights and metrics for oauth2orize
Gathering detailed insights and metrics for oauth2orize
Gathering detailed insights and metrics for oauth2orize
Gathering detailed insights and metrics for oauth2orize
OAuth 2.0 authorization server toolkit for Node.js.
npm install oauth2orize
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,473 Stars
497 Commits
470 Forks
101 Watching
12 Branches
21 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
JavaScript (99.93%)
Makefile (0.07%)
Cumulative downloads
Total Downloads
Last day
-15.4%
15,918
Compared to previous day
Last week
-5.6%
105,027
Compared to previous week
Last month
27.1%
432,041
Compared to previous month
Last year
8.4%
3,689,336
Compared to previous year
3
5
OAuth2orize is an authorization server toolkit for Node.js. It provides a suite of middleware that, combined with Passport authentication strategies and application-specific route handlers, can be used to assemble a server that implements the OAuth 2.0 protocol.
Advertisement
Node.js API Masterclass With Express & MongoDB
Create a real world backend for a bootcamp directory app
$ npm install oauth2orize
OAuth 2.0 defines an authorization framework, allowing an extensible set of authorization grants to be exchanged for access tokens. Implementations are free to choose what grant types to support, by using bundled middleware to support common types or plugins to support extension types.
Call createServer()
to create a new OAuth 2.0 server. This instance exposes
middleware that will be mounted in routes, as well as configuration options.
1var server = oauth2orize.createServer();
A client must obtain permission from a user before it is issued an access token. This permission is known as a grant, the most common type of which is an authorization code.
1server.grant(oauth2orize.grant.code(function(client, redirectURI, user, ares, done) { 2 var code = utils.uid(16); 3 4 var ac = new AuthorizationCode(code, client.id, redirectURI, user.id, ares.scope); 5 ac.save(function(err) { 6 if (err) { return done(err); } 7 return done(null, code); 8 }); 9}));
OAuth2orize also bundles support for implicit token grants.
After a client has obtained an authorization grant from the user, that grant can be exchanged for an access token.
1server.exchange(oauth2orize.exchange.code(function(client, code, redirectURI, done) { 2 AuthorizationCode.findOne(code, function(err, code) { 3 if (err) { return done(err); } 4 if (client.id !== code.clientId) { return done(null, false); } 5 if (redirectURI !== code.redirectUri) { return done(null, false); } 6 7 var token = utils.uid(256); 8 var at = new AccessToken(token, code.userId, code.clientId, code.scope); 9 at.save(function(err) { 10 if (err) { return done(err); } 11 return done(null, token); 12 }); 13 }); 14}));
OAuth2orize also bundles support for password and client credential grants. Additionally, bundled refresh token support allows expired access tokens to be renewed.
When a client requests authorization, it will redirect the user to an authorization endpoint. The server must authenticate the user and obtain their permission.
1app.get('/dialog/authorize', 2 login.ensureLoggedIn(), 3 server.authorize(function(clientID, redirectURI, done) { 4 Clients.findOne(clientID, function(err, client) { 5 if (err) { return done(err); } 6 if (!client) { return done(null, false); } 7 if (client.redirectUri != redirectURI) { return done(null, false); } 8 return done(null, client, client.redirectURI); 9 }); 10 }), 11 function(req, res) { 12 res.render('dialog', { transactionID: req.oauth2.transactionID, 13 user: req.user, client: req.oauth2.client }); 14 });
In this example, connect-ensure-login
middleware is being used to make sure a user is authenticated before
authorization proceeds. At that point, the application renders a dialog
asking the user to grant access. The resulting form submission is processed
using decision
middleware.
1app.post('/dialog/authorize/decision', 2 login.ensureLoggedIn(), 3 server.decision());
Based on the grant type requested by the client, the appropriate grant module registered above will be invoked to issue an authorization code.
Obtaining the user's authorization involves multiple request/response pairs. During this time, an OAuth 2.0 transaction will be serialized to the session. Client serialization functions are registered to customize this process, which will typically be as simple as serializing the client ID, and finding the client by ID when deserializing.
1server.serializeClient(function(client, done) { 2 return done(null, client.id); 3}); 4 5server.deserializeClient(function(id, done) { 6 Clients.findOne(id, function(err, client) { 7 if (err) { return done(err); } 8 return done(null, client); 9 }); 10});
Once a user has approved access, the authorization grant can be exchanged by the client for an access token.
1app.post('/token', 2 passport.authenticate(['basic', 'oauth2-client-password'], { session: false }), 3 server.token(), 4 server.errorHandler());
Passport strategies are used to authenticate the client, in this case using either an HTTP Basic authentication header (as provided by passport-http) or client credentials in the request body (as provided by passport-oauth2-client-password).
Based on the grant type issued to the client, the appropriate exchange module
registered above will be invoked to issue an access token. If an error occurs,
errorHandler
middleware will format an error response.
Once an access token has been issued, a client will use it to make API requests on behalf of the user.
1app.get('/api/userinfo', 2 passport.authenticate('bearer', { session: false }), 3 function(req, res) { 4 res.json(req.user); 5 });
In this example, bearer tokens are issued, which are then authenticated using an HTTP Bearer authentication header (as provided by passport-http-bearer)
This example demonstrates how to implement an OAuth service provider, complete with protected API access.
oauth2orize uses the debug module. You can enable debugging messages on the console by doing export DEBUG=oauth2orize
before running your application.
Copyright (c) 2012-2021 Jared Hanson <https://www.jaredhanson.me/>
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
8 existing vulnerabilities detected
Details
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More