简体   繁体   中英

How I can use Pre and post-middlewares | oidc-provider panva

The documentation is not clear for me and I need to validate the parameters data to define if the user is allowed to get his login or not, I am using express too, so I'm so confused with expressApp.use () and provider.use(). Simply, I need to mix this functions and that it serve because I can't use those functions separated due to the.use() method from express that avoid it to me.

    // Ruta principal para la conexión y obtener el documento de descubrimiento
    expressApp.use("/oidc", async (err, req, res, next) => {

      console.log('PRE-MIDDLEWARE');
      console.log(req, res)

      if (err instanceof SessionNotFound)
        return res.render('expiredSession', {})

      if (err) {
        console.log(err);
        return next(err);
      }

    }, oidc.callback());

    oidc.use(async (ctx, next) => {
      console.log('pre middleware', ctx.method, ctx.path);
    });

I'll appreciate your help

You can use Pre and Post middlewares like this:

// app.js (in your express js app)

/**
 * Pre and Post middleware to execute before and after the OIDC provider
 * 
 * 
 * @see https://github.com/panva/node-oidc-provider/tree/main/docs#pre--and-post-middlewares
 */
provider.use(async (ctx, next) => {
    await oidcServices.OIDCPreMiddlewareService(ctx);
    await next();
    await oidcServices.OIDCPostMiddlewareService(ctx);
});

Where pre or post midlle can be like below:

OidcServices.OIDCPreMiddlewareService can be like:


// Oidc Pre Midlleware Service
module.exports = async (ctx) => {
  // Do anything you want with ctx in each case
    switch (ctx.path) {
        case 'authorization':
            break;
        case 'backchannel_authentication':
            break;
        case 'client_delete':
            break;
        case 'client_update':
            break;
        case 'client':
            break;
        case 'code_verification':
            break;
        case 'cors.device_authorization':
            break;
        case 'cors.discovery':
            break;
        case 'cors.introspection':
            break;
        case 'cors.jwks':
            break;
        case 'cors.pushed_authorization_request':
            break;
        case 'cors.revocation':
            break;
        case 'cors.token':
            break;
        case 'cors.userinfo':
            break;
        case 'device_authorization':
            break;
        case 'device_resume':
            break;
        case 'discovery':
            break;
        case 'end_session_confirm':
            break;
        case 'end_session_success':
            break;
        case 'end_session':
            break;
        case 'introspection':
            break;
        case 'jwks':
            break;
        case 'pushed_authorization_request':
            break;
        case 'registration':
            break;
        case 'resume':
            break;
        case 'revocation':
            break;

        case 'token':
            break;
        case 'userinfo':


            break;
        default:
            break;
    }
}

OidcServices.OIDCPostMiddlewareServicecan be like:


 module.exports = async (ctx) => {
// You can do whatever you want with ctx: eg: send notification to user, etc.
    if (ctx.oidc) {
        switch (ctx.oidc.route) {
            case 'authorization':
                break;
            case 'backchannel_authentication':
                break;
            case 'client_delete':
                break;
            case 'client_update':
                break;
            case 'client':
                break;
            case 'code_verification':
                break;
            case 'cors.device_authorization':
                break;
            case 'cors.discovery':
                break;
            case 'cors.introspection':
                break;
            case 'cors.jwks':
                break;
            case 'cors.pushed_authorization_request':
                break;
            case 'cors.revocation':
                break;
            case 'cors.token':
                break;
            case 'cors.userinfo':
                break;
            case 'device_authorization':
                break;
            case 'device_resume':
                break;
            case 'discovery':
                break;
            case 'end_session_confirm':
                break;
            case 'end_session_success':
                break;
            case 'end_session':
                break;
            case 'introspection':
                break;
            case 'jwks':
                break;
            case 'pushed_authorization_request':
                break;
            case 'registration':
                break;
            case 'resume':
                break;
            case 'revocation':
                break;

            case 'token':
                break;
            case 'userinfo':
                break;
            default:
                break;
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM