mikenicholson/passport-jwt

Add Custom extractor which takes JwtFromRequestFunction as argument

Abdelwahab313 opened this issue ยท 1 comments

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.

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