jaredhanson / passport-openidconnect

OpenID Connect authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-openidconnect/?utm_source=github&utm_medium=referral&utm_campaign=passport-openidconnect&utm_content=about
MIT License
188 stars 173 forks source link

[Question/Feature Request] Allow to pass additional custom function for parsing UserInfo #93

Open bkupidura opened 2 years ago

bkupidura commented 2 years ago

Im using passport-openidconnect with Authelia. With current implementation, only well defined list of UserInfo data (https://github.com/jaredhanson/passport-openidconnect/blob/master/lib/profile.js) can be passed to verify function.

It would be nice to be able pass additional parsing function from Strategy options.

It can work like this:

new OpenIDConnectStrategy({
  ...,
  customParse: function(profile, json){
    if (json.groups) { profile.groups = json.groups; }
  }
})
            var profile = Profile.parse(json);
            if (typeof options.customParse === 'function') {
                options.customParse(profile, json)
            }
            loaded(profile, json, body);

This way passport-openidconnect will allow users to parse any UserInfo. If this can be already achieved somehow, can you please clarify how?

(Sorry for my pure JS code example ;))

regnete commented 2 years ago

In an earlier version of this startegy, the verify callback had access to the claims. So we were able to acces some custom properties in the claim and apply them to the profile. In the current version, claims is not passed in anymore. A custom parse function seems to be the most elegnat solution for this requirement. Please think about adding a custom parse function for the context too.

We are currently working arround this issue with a very uggly hack/workarround.

Overwrite the strategies private _shouldLoadUserProfile method, as it has access to claims. Then remember claims as a property of request.

// HACK: sadly the basic impl doesn't pass the claims to openIDVerifyCallback anymore
this._shouldLoadUserProfile = (req, claims, done) => {
     req['passport_' + this.name + "_claims"] = claims;
     done(null, false);
}

In your verify callback, get the claims from the request. Must set passReqToCallback=true in the strategy options!

const claims = req['passport_' + this.name + "_claims"];
delete req['passport_' + this.name + "_claims"];