Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@benkroeger
Copy link
Contributor

Add two new options:

  • When enableDoublecheck is true, the middleware will run
    even if a previous middleware has already set req.accessToken
    (possibly to null for anonymous requests)
  • When overwriteExistingToken is true (and enableDoublecheck too),
    the middleware will overwrite req.accessToken set by a previous
    middleware instances.

fixes #2106
For details, please see issue description #2106
@richardpringle

@slnode
Copy link

slnode commented Feb 29, 2016

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@bajtos
Copy link
Member

bajtos commented Feb 29, 2016

@slnode ok to test

@bajtos
Copy link
Member

bajtos commented Feb 29, 2016

@benkroeger thank you for the pull request. The code changes look reasonable, I appreciate you included a unit-test too!

I have one concern though.

Digging in the code, I found that the token middleware skips when req.accessToken !== undefined. Unfortunately, if the first instance of the middleware can not find valid credentials, it assigns req.accessToken = null (which is !== undefined).

I think our original assumption was that all instances of the token middleware use the same auth method and if the first instance of the token middleware does find any valid credentials, then the second and all subsequent instances will not find any valid credentials either, and thus we can skip them to improve performance.

Now your use case is different and our assumption no longer holds.

Can we find a way how to support both cases? For example, what if we added a configuration option for the token middleware that will instruct it to run even if other token middleware was not able to find any valid credentials?

/cc @raymondfeng @ritch thoughts?

@bajtos bajtos removed the #review label Feb 29, 2016
@benkroeger
Copy link
Contributor Author

@bajtos sure, making this a middleware prop shouldn't be a problem. suggestions on the naming? how 'bout enableDoublecheck?

@benkroeger
Copy link
Contributor Author

now that I think about it - I actually don't want a subsequent instance to overwrite any existing req.accessToken that was set by a previous instance. Meaning, if a user provides a token on the "regular" way, that one supersedes any other tokens.

As suggested by @bajtos, token middleware now as new options for loading multiple instances:
* @Property {Boolean} [enableDoublecheck] Execute middleware although an instance mounted earlier in the chain didn't find a token
 * @Property {Boolean} [overwriteExistingToken] only has effect in combination with `enableDoublecheck`. If truthy, will allow to overwrite an existing accessToken.
@bajtos
Copy link
Member

bajtos commented Mar 2, 2016

now that I think about it - I actually don't want a subsequent instance to overwrite any existing req.accessToken that was set by a previous instance. Meaning, if a user provides a token on the "regular" way, that one supersedes any other tokens.

Sure, that makes sense to me as well.

I am thinking about the following rule:

req.accessToken is defined and truthy 
   => return next();
req.accessToken is defined but falsy && the new feature is NOT in effect 
   => return next();
go ahead, extract & verify the access token

As for the option name, now about overwriteTokenNotFound?

@benkroeger
Copy link
Contributor Author

I've implemented a two-step decision now:

It checks for enableDoublecheck to be truthy and, it also a token exists already, overwriteExistingToken must be truthy as well.

if (req.accessToken && req.accessToken.id) {
      rewriteUserLiteral(req, currentUserLiteral);
      return next();
    if (req.accessToken !== undefined) {
      if (!enableDoublecheck) {
        // req.accessToken is defined already (might also be "null" or "false") and enableDoublecheck
        // has not been set --> skip searching for credentials
        rewriteUserLiteral(req, currentUserLiteral);
        return next();
      }
      if (req.accessToken.id && !overwriteExistingToken) {
        // req.accessToken.id is defined, which means that some other middleware has identified a valid user.
        // when overwriteExistingToken is not set to a truthy value, skip searching for credentials.
        rewriteUserLiteral(req, currentUserLiteral);
        return next();
      }
      // continue normal operation (as if req.accessToken was undefined)
    }

@richardpringle
Copy link
Contributor

@slnode test please!

@slnode
Copy link

slnode commented Mar 14, 2016

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@richardpringle
Copy link
Contributor

ok to test

@benkroeger
Copy link
Contributor Author

Since there hasn't been any updates on this issue from the maintainers, I thought it might be because the tests on travis fail. Judging from the indications, it looks like "grunt" does not get installed correctly during the tests for Node.js 5.0. All of them state "grunt: command not found". Thoughts? Any chance we can get this PR moving again?

@richardpringle
Copy link
Contributor

@rmg should your latest CI fix have resolved this?

it looks like "grunt" does not get installed correctly during the tests for Node.js 5.0. All of them state "grunt: command not found". Thoughts?

Node 5 is failing on all platforms.

@rmg
Copy link
Member

rmg commented Mar 16, 2016

The Node 5 failure looks like it happened before I removed shelljs from the cache (which was the source of the source of the problem when installing deps with npm3). The current failure on node-0.12 on Windows looks like a run of the mill flaky test.

@benkroeger
Copy link
Contributor Author

looks like now tests are failing that previously were successful - without further commits / code change. npm-debug.log says something about failing to install phantom.js

@benkroeger
Copy link
Contributor Author

r u guys adding more and more tests? What's about all the dependant tests? Didn't see those in other PRs. And the ones I looked at are failing due to timeout reasons (e.g. dependant strong-arc after > 5 mins).

@richardpringle
Copy link
Contributor

@benkroeger, we have always checked the dependants to make sure that nothing is broken down stream. I believe that the only two dependants that failed that we need to worry about are the angular sdk and the angular live-set.

@bajtos PTAL. Are we still having issues with CI?

// the middleware should load the token from
.expect(401)
.end(done);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like pure spacing/formatting change to me, unrelated to the changes made in this patch. Could you please revert?

@bajtos
Copy link
Member

bajtos commented Apr 1, 2016

@benkroeger please let me know when this patch is ready for another round of review. Note that github does not send any notifications when you add a commit, one has to leave a comment for that (ideally including the GH handle of the reviewer).

@benkroeger
Copy link
Contributor Author

alright @bajtos this PR is ready for another review. I reverted the blank changes and am using a better comparison on the token object in my unit test

@bajtos
Copy link
Member

bajtos commented Apr 6, 2016

@slnode test please

prevents "can not read property id of null"
@bajtos bajtos changed the title changes avail conditions for req.accessToken Allow built-in token middleware to run repeatedly Apr 6, 2016
bajtos added a commit that referenced this pull request Apr 6, 2016
Allow built-in token middleware to run repeatedly
@bajtos
Copy link
Member

bajtos commented Apr 6, 2016

Landed via a9c7801, thank you @benkroeger for your contribution and the patience with us :)

@richardpringle
Copy link
Contributor

Finally! 🎉

Agreed, thanks a lot @benkroeger, awesome job!

@benkroeger
Copy link
Contributor Author

Woohoo!
Thanks guys!
@richardpringle @bajtos I just saw that my last commit didn't make it into the merge. It solves a potential "can not read property of null" error that I occurred during one of my experiments. Any chance to get that one in, too?

@benkroeger
Copy link
Contributor Author

forget my previous comment. it's in a9c7801
sorry for the confusion!

@bajtos
Copy link
Member

bajtos commented Apr 8, 2016

@benkroeger ouch! Could you please add a unit-test to a9c7801 that demonstrates/reproduces the bug in the current implementation and send a new PR? Please mention my GH handle in the PR description, I'll get it landed ASAP.

bajtos pushed a commit that referenced this pull request Jun 13, 2016
subsequent token middleware tries to read `token.id` when `enableDoublecheck: true`. That caused a "Cannot read property `id` of `null`" error when the first middleware didn't actually find a valid accessToken.
bajtos added a commit that referenced this pull request Jun 13, 2016
bajtos pushed a commit that referenced this pull request Jun 13, 2016
Subsequent token middleware tries to read `token.id`
when `enableDoublecheck: true`.
That caused a "Cannot read property `id` of `null`" error
when the first middleware didn't actually find a valid accessToken.

[back-port of #2227]
bajtos added a commit that referenced this pull request Jun 13, 2016
 * add missing unit tests for #2108 (Benjamin Kroeger)
 * Expose `Replace*` methods (Amir Jafarian)
 * Update tests for strong-error-handler (David Cheung)
 * Remove legacy express 3.x middleware getters (Miroslav Bajtoš)
 * Docuemtation for `replace*` methods (Amir Jafarian)
 * Make the doc clear for `findORCreate` cb (Amir Jafarian)
 * Fix JSCS unsupported rule error (Jason)
 * Remove env.json and strong-pm dir (Ritchie Martori)
 * Throw error upon extending unknown model (David Cheung)
 * Remove unused UserModel properties (David Cheung)
 * Remove Change.handleError (Candy)
 * Update user.js (Rik)
 * Separate error-checking and next/done logic from other logic in the test suite (Supasate Choochaisri)
 * Clean up by removing unnecessary comments (Supasate Choochaisri)
 * Add feature to not allow duplicate role name (Supasate Choochaisri)
 * update copyright statements (Ryan Graham)
 * relicense as MIT only (Ryan Graham)
 * Upgrade phantomjs to 2.x (Miroslav Bajtoš)
 * app: send port:0 instead of port:undefined (Miroslav Bajtoš)
 * travis: drop node@5, add node@6 (Miroslav Bajtoš)
 * Disable DEBUG output for eslint on Jenkins CI (Miroslav Bajtoš)
 * Remove "loopback.autoAttach()" (Miroslav Bajtoš)
 * test/rest.middleware: use local registry (Miroslav Bajtoš)
 * Fix role.isOwner to support app-local registry (Miroslav Bajtoš)
 * test/user: use local registry (Miroslav Bajtoš)
 * Resolver support return promise (juehou)
 * remove @Private from jsdoc (Manu Phatak)
 * Fixes for emit `remoteMethodDisabled` PR (Simon Ho)
 * Add new feature to emit a `remoteMethodDisabled` event when disabling a remote method. (Supasate Choochaisri)
 * Fix typo in Model.nestRemoting (Tim Needham)
 * Update loopback.js (Rand McKinney)
 * Allow built-in token middleware to run repeatedly (Benjamin Kröger)
 * Use eslint with loopback config (Miroslav Bajtoš)
 * promise docs (Jue Hou)
 * Update JSDoc ([email protected])
 * Remove constraint making isStatic required (Candy)
 * Fix inconsistencies in JSDoc ([email protected])
 * Improve error message on connector init error (Miroslav Bajtoš)
 * application: correct spelling of "cannont" (Sam Roberts)
 * Remove sl-blip from dependency (Candy)
 * Use new strong-remoting API (Candy)
 * test: remove forgotten console.trace logs (Miroslav Bajtoš)
 * Fix race condition in replication tests (Miroslav Bajtoš)
 * Fix race condition in error handler test (Miroslav Bajtoš)
 * test: remove errant console.log from test (Ryan Graham)
 * Promisify Model Change (Jue Hou)
 * Travis: drop iojs, add v4.x and v5.x (Miroslav Bajtoš)
 * test: use ephemeral port for e2e server (Ryan Graham)
 * test: fail on error instead of crash (Ryan Graham)
 * ensure app is booted before integration tests (Ryan Graham)
 * Remove "loopback.DataModel" (Miroslav Bajtoš)
 * Correct JSDoc findOrCreate() callback in PersistedModel (Chris Coggburn)
 * Fix typo in package.json (publishConfig) (Miroslav Bajtoš)
 * Start development of 3.0 (Candy)
 * Hide verificationToken (Samuel Gaus)
 * Fix description for User.prototype.hasPassword (Jue Hou)
 * Checkpoint speedup (Amir Jafarian)
 * Always use bluebird as promise library Replace `global.Promise` with `bluebird` (Jue Hou)
 * Remove unused code from loopback-testing-helper (Simon Ho)
 * Make juggler a regular dependency (Miroslav Bajtoš)
 * Remove dependency on loopback-testing (Simon Ho)
 * Fix failing tests (Simon Ho)
 * Update persisted-model.js (Rand McKinney)
 * Update persisted-model.js (linguofeng)
bajtos added a commit that referenced this pull request Jul 12, 2016
 * Fix description for User.prototype.hasPassword (Jue Hou)
 * Fix verificationToken bug #2440 (Loay)
 * add missing unit tests for #2108 (Benjamin Kroeger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow built-in token middleware to be run multiple times

6 participants