mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 forks source link

Add Custom extractor which takes JwtFromRequestFunction as argument #235

Open Abdelwahab313 opened 2 years ago

Abdelwahab313 commented 2 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch passport-jwt@4.0.0 for the project I'm working on.

I think we needed a way to add custom extractor for jwt, as we are using passport for authenticating on third party webhook which sends the token inside nested field in body, another reason is to authenticate for websocket handshake as mentioned in this Issue.

So my approach is to use a custom extractor which takes JwtFromRequestFunction as argument.

Here is the diff that solved my problem:

diff --git a/node_modules/passport-jwt/lib/extract_jwt.js b/node_modules/passport-jwt/lib/extract_jwt.js
index 7f112ab..392fa82 100644
--- a/node_modules/passport-jwt/lib/extract_jwt.js
+++ b/node_modules/passport-jwt/lib/extract_jwt.js
@@ -71,6 +71,10 @@ extractors.fromAuthHeaderAsBearerToken = function () {
     return extractors.fromAuthHeaderWithScheme(BEARER_AUTH_SCHEME);
 };

+extractors.fromCustomExtractor = function (extractor_callback) {
+  return extractor_callback;
+};
+

 extractors.fromExtractors = function(extractors) {
     if (!Array.isArray(extractors)) {

This issue body was partially generated by patch-package.

Outternet commented 1 year ago

even though the patch library works conveniently, this functionality is already in the code. Just pass the callback directly to jwtFromRequest.