Skip to content

Scope middleware to a subpath #794

Closed

Description

@bajtos

In order to support loopback-component-oauth2, we need to allow middleware to be executed only for a subset of application paths. By extension, such solution will allow to use different configuration of the same middleware for different paths.

Example: authenticate paths /restricted and /api using the scope demo, authenticate paths /admin using the scope administration.

Activity

  1. self-assigned this
    on Nov 12, 2014
  2. added this to the #Epic: Components milestone on Nov 12, 2014
  3. bajtos commented on Nov 12, 2014

    @bajtos
    MemberAuthor

    Proposed solutions:

    @bajtos

    app.middleware('auth', ['/restricted', '/api'], authenticator)
    
    app.middlewareFromConfig(authenticatorFactory, {
      phase: 'auth',
      paths: ['/restricted', '/api']
      config: {
        // etc.
      }
    });
    

    @ritch

    app.middleware('auth', authenticators, {
      condition: function(req, res, cb) {
        if(getBasePath(req.url) === paths[i]) {
          cb(null, true);
        } else {
          cb(null, false);
        }
      }
    });
    
    app.middleware('auth', authenticators, {
      routes: [paths[i]]
    });
    
  4. raymondfeng commented on Nov 12, 2014

    @raymondfeng
    Member

    In addition to paths, I would like to see support for sub phases too. For example,

    app.middleware('auth:passport', passport.initialize());
    

    It will create a passport phase and register the PhasedRouter using app.middleware('auth', phasedRouterForPassport).

    See the middleware/route graph for loopback-gateway: https://github.com/strongloop/loopback-gateway/wiki/Middleware-routes-in-loopback-gateway. I would like to use it to validate our api design.

  5. raymondfeng commented on Nov 13, 2014

    @raymondfeng
    Member

    For route matching, I'm fine with @ritch's proposal with minor tweaks:

    app.middleware('auth', authenticators, {
      when: function(req, res) { // Not sure if we need async at all
        return getBasePath(req.url) === paths[i]); 
      }
    });
    
    app.middleware('auth', authenticators, {
      routes: [paths[i]] // not sure if we need to deal with verb too
    });
    
  6. bajtos commented on Nov 13, 2014

    @bajtos
    MemberAuthor

    I want to keep this task simple and focused on the paths only. Conditional functions and sub-phases are out of scope, please open a new issue for each of them if you feel they are "a must have" for the initial release.

    As for the route matching, I believe my proposal is better, because it is more consistent with the express convention of specifying the path as the first arg and the handler as the second arg, and also with the Node convention of passing the handler function as the last argument.

    app.middleware('auth', ['/restricted', '/api'], authenticator)
    // compare to express
    app.use(['/restricted', '/api'], authenticator)
    // compare to Node
    emitter.on('auth', handler)

    Keeping in mind the request for a generic condition function, the API can be expanded in the future to support different types of the second argument:

    app.middleware('auth', { when: function(req, res) { ... } }, authenticator);
  7. ritch commented on Nov 13, 2014

    @ritch
    Member

    I'm fine with @bajtos proposal.

    app.middleware('auth', ['/restricted', '/api'], authenticator);

    Eventually it would be nice to add:

    app.middleware('auth', { when: function(req, res, cb) { cb(null, true); } }, authenticator);
  8. raymondfeng commented on Nov 13, 2014

    @raymondfeng
    Member

    I'm ok with @bajtos's proposal. For paths, I assume the argument is optional and both 'path' and ['path'] are accepted.

  9. bajtos commented on Nov 14, 2014

    @bajtos
    MemberAuthor

    The path-to-regexp conversion used by Express is implemented in a standalone module - see https://www.npmjs.org/package/path-to-regexp

  10. added a commit that references this issue on Nov 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions