Gathering detailed insights and metrics for passport-jwt
Gathering detailed insights and metrics for passport-jwt
Gathering detailed insights and metrics for passport-jwt
Gathering detailed insights and metrics for passport-jwt
@types/passport-jwt
TypeScript definitions for passport-jwt
@mestrak/passport-multi-jwt
Passport authentication strategy using JSON Web Tokens with multiple providers. Fully backwards compatible with passport-jwt.
passport-jwt.socketio
A passport-jwt middleware for socket.io
@goodrequest/passport-jwt-wrapper
[![Build and run tests](https://github.com/Slonik923/passport-jwt-wrapper/actions/workflows/test.yaml/badge.svg)](https://github.com/Slonik923/passport-jwt-wrapper/actions/workflows/test.yaml) [![codecov](https://codecov.io/gh/Slonik923/passport-jwt-wrapp
Passport authentication using JSON Web Tokens
npm install passport-jwt
99
Supply Chain
100
Quality
74.4
Maintenance
100
Vulnerability
98.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,966 Stars
172 Commits
214 Forks
20 Watching
11 Branches
29 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-10.9%
249,172
Compared to previous day
Last week
0.8%
1,339,966
Compared to previous week
Last month
10.2%
5,638,514
Compared to previous month
Last year
27.9%
56,494,114
Compared to previous year
2
5
A Passport strategy for authenticating with a JSON Web Token.
This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.
If you want to quickly add secure token-based authentication to Node.js apps, feel free to check out Auth0's Node.js SDK and free plan at auth0.com/developers
npm install passport-jwt
The JWT authentication strategy is constructed as follows:
new JwtStrategy(options, verify)
options
is an object literal containing options to control how the token is
extracted from the request or verified.
secretOrKey
is a string or buffer containing the secret
(symmetric) or PEM-encoded public key (asymmetric) for verifying the token's
signature. REQUIRED unless secretOrKeyProvider
is provided.secretOrKeyProvider
is a callback in the format function secretOrKeyProvider(request, rawJwtToken, done)
,
which should call done
with a secret or PEM-encoded public key (asymmetric) for the given key and request combination.
done
accepts arguments in the format function done(err, secret)
. Note it is up to the implementer to decode rawJwtToken.
REQUIRED unless secretOrKey
is provided.jwtFromRequest
(REQUIRED) Function that accepts a request as the only
parameter and returns either the JWT as a string or null. See
Extracting the JWT from the request for
more details.issuer
: If defined the token issuer (iss) will be verified against this
value.audience
: If defined, the token audience (aud) will be verified against
this value.algorithms
: List of strings with the names of the allowed algorithms. For instance, ["HS256", "HS384"].ignoreExpiration
: if true do not validate the expiration of the token.passReqToCallback
: If true the request will be passed to the verify
callback. i.e. verify(request, jwt_payload, done_callback).jsonWebTokenOptions
: passport-jwt is verifying the token using jsonwebtoken.
Pass here an options object for any other option you can pass the jsonwebtoken verifier. (i.e maxAge)verify
is a function with the parameters verify(jwt_payload, done)
jwt_payload
is an object literal containing the decoded JWT payload.done
is a passport error first callback accepting arguments
done(error, user, info)An example configuration which reads the JWT from the http Authorization header with the scheme 'bearer':
1var JwtStrategy = require('passport-jwt').Strategy, 2 ExtractJwt = require('passport-jwt').ExtractJwt; 3var opts = {} 4opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); 5opts.secretOrKey = 'secret'; 6opts.issuer = 'accounts.examplesoft.com'; 7opts.audience = 'yoursite.net'; 8passport.use(new JwtStrategy(opts, function(jwt_payload, done) { 9 User.findOne({id: jwt_payload.sub}, function(err, user) { 10 if (err) { 11 return done(err, false); 12 } 13 if (user) { 14 return done(null, user); 15 } else { 16 return done(null, false); 17 // or you could create a new account 18 } 19 }); 20}));
There are a number of ways the JWT may be included in a request. In order to remain as flexible as
possible the JWT is parsed from the request by a user-supplied callback passed in as the
jwtFromRequest
parameter. This callback, from now on referred to as an extractor,
accepts a request object as an argument and returns the encoded JWT string or null.
A number of extractor factory functions are provided in passport-jwt.ExtractJwt. These factory functions return a new extractor configured with the given parameters.
fromHeader(header_name)
creates a new extractor that looks for the JWT in the given http
headerfromBodyField(field_name)
creates a new extractor that looks for the JWT in the given body
field. You must have a body parser configured in order to use this method.fromUrlQueryParameter(param_name)
creates a new extractor that looks for the JWT in the given
URL query parameter.fromAuthHeaderWithScheme(auth_scheme)
creates a new extractor that looks for the JWT in the
authorization header, expecting the scheme to match auth_scheme.fromAuthHeaderAsBearerToken()
creates a new extractor that looks for the JWT in the authorization header
with the scheme 'bearer'fromExtractors([array of extractor functions])
creates a new extractor using an array of
extractors provided. Each extractor is attempted in order until one returns a token.If the supplied extractors don't meet your needs you can easily provide your own callback. For example, if you are using the cookie-parser middleware and want to extract the JWT in a cookie you could use the following function as the argument to the jwtFromRequest option:
1var cookieExtractor = function(req) { 2 var token = null; 3 if (req && req.cookies) { 4 token = req.cookies['jwt']; 5 } 6 return token; 7}; 8// ... 9opts.jwtFromRequest = cookieExtractor;
Use passport.authenticate()
specifying 'JWT'
as the strategy.
1app.post('/profile', passport.authenticate('jwt', { session: false }), 2 function(req, res) { 3 res.send(req.user.profile); 4 } 5);
The method of including a JWT in a request depends entirely on the extractor
function you choose. For example, if you use the fromAuthHeaderAsBearerToken
extractor, you would include an Authorization
header in your request with the
scheme set to bearer
. e.g.
Authorization: bearer JSON_WEB_TOKEN_STRING.....
Read the Migration Guide for help upgrading to the latest major version of passport-jwt.
npm install
npm test
To generate test-coverage reports:
npm install -g istanbul
npm run-script testcov
istanbul report
The MIT License
Copyright (c) 2015 Mike Nicholson
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 6/24 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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-18
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