basic-auth icon indicating copy to clipboard operation
basic-auth copied to clipboard

Protect / but specific exceptions

Open quape opened this issue 9 years ago • 2 comments

Hi there, I want to protect "/" but certain paths shall be accessible without a passwort. How can I realize that?

quape avatar Feb 16 '16 12:02 quape

You would need to change this package, here is a quick hack you could try...

basic-auth.js:21> handle: basicAuth(this.callback, this.realm)
  1. This line is calling the Connect basicAuth module.

  2. The return value from executing basicAuth(this.callback, this.realm) is a function that takes (req, res, next)

  3. Create a handler function that takes (req, res, next) and wraps the call to basicAuth(this.callback, this.realm). It will be something like this, although my JS is a little rusty...

    basic-auth.js:21> handle: function(req, res, next) {
    // inspect request logic goes here, then do this...
      if(excluded)  {
          next();
      } else {
          (basicAuth(this.callback, this.realm))(req, res, next);
      }
    }
    
  4. Inspect the req object in your function to determine whether it should execute the basic auth logic

  5. Please send me a pull request if you develop something flexible.

Jabbslad avatar Feb 23 '16 00:02 Jabbslad

I have tried your suggestion with the code, without any excluding logic, which should work same as unmodified package before

      for(var i=0; i<routes.length; i++) {
        WebApp.connectHandlers.stack.splice(i, 0, {
          route: routes[i],
          handle: function (req, res, next) {
            console.log(req.url);
            (basicAuth(this.callback, this.realm))(req, res, next)
          }
        });
      }

but I have this error then:

W20180530-22:00:41.215(2)? (STDERR) TypeError: Cannot read property 'length' of undefined
W20180530-22:00:41.216(2)? (STDERR)     at /Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/basic-auth-connect/index.js:82:18
W20180530-22:00:41.216(2)? (STDERR)     at Object.handle (packages/jabbslad_basic-auth.js:41:53)
W20180530-22:00:41.216(2)? (STDERR)     at next (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:174:15)
W20180530-22:00:41.216(2)? (STDERR)     at Function.app.handle (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:182:3)
W20180530-22:00:41.216(2)? (STDERR)     at Object.fn [as handle] (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:79:14)
W20180530-22:00:41.216(2)? (STDERR)     at next (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:174:15)
W20180530-22:00:41.217(2)? (STDERR)     at Object.WebAppInternals.staticFilesMiddleware (packages/webapp/webapp_server.js:419:5)
W20180530-22:00:41.217(2)? (STDERR)     at Promise.resolve.then (packages/webapp/webapp_server.js:702:23)
W20180530-22:00:41.217(2)? (STDERR)     at /Users/jirkakrepl/.meteor/packages/promise/.0.10.0.wb3q6c.q34++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40

I don't have more time to play with it, but maybe it would be helpful for someone:)

jirikrepl avatar May 30 '18 20:05 jirikrepl