-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heavy-ish refactor. API changes. Read on.
- no more middleware nonsense, now getRoute does all - new feature (not yet tested) for attaching a custom context to a method - internal refactor
- Loading branch information
Paolo Scanferla
committed
Apr 8, 2015
1 parent
962648d
commit e899d3f
Showing
19 changed files
with
305 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var BPromise = require("bluebird"); | ||
var t = require("tcomb"); | ||
|
||
module.exports = t.irreducible("BPromise", function (p) { | ||
return p instanceof BPromise; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var BPromise = require("bluebird"); | ||
|
||
var getLastValidDate = require("./get-last-valid-date.js"); | ||
var hashLoginToken = require("./hash-login-token.js"); | ||
|
||
module.exports = function getUserFromToken (mwInstance, loginToken) { | ||
var users = mwInstance.db.collection("users"); | ||
return BPromise.promisify(users.findOne, users)({ | ||
"services.resume.loginTokens": { | ||
$elemMatch: { | ||
hashedToken: hashLoginToken(loginToken), | ||
when: { | ||
$gt: getLastValidDate() | ||
} | ||
} | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
var BPromise = require("bluebird"); | ||
var R = require("ramda"); | ||
var t = require("tcomb-validation"); | ||
|
||
var BodyType = require("./lib/body-type.js"); | ||
var errorHandler = require("./lib/error-handler.js"); | ||
var getUserFromToken = require("./lib/get-user-from-token.js"); | ||
var MWError = require("./lib/mw-error.js"); | ||
|
||
/* | ||
* To keep a consistent API between middleware (i.e., each property of the | ||
* exported object is a getter for the actual middleware function) we "wrap" | ||
* `bodyValidation` and `context` functions in `R.always`. | ||
*/ | ||
module.exports = { | ||
|
||
bodyValidation: R.always(function (req, res, next) { | ||
var validation = t.validate(req.body, BodyType); | ||
if (validation.isValid()) { | ||
next(); | ||
} else { | ||
errorHandler(res, new MWError(400, validation.firstError())); | ||
} | ||
}), | ||
|
||
context: R.always(function (req, res, next) { | ||
req.context = { | ||
userId: null | ||
}; | ||
next(); | ||
}), | ||
|
||
user: function (mwInstance) { | ||
return function (req, res, next) { | ||
if (R.isNil(req.body.loginToken)) { | ||
return next(); | ||
} | ||
getUserFromToken(mwInstance, req.body.loginToken) | ||
.then(function (user) { | ||
if (R.isNil(user)) { | ||
throw new MWError(401, "Invalid loginToken"); | ||
} | ||
req.context.userId = user._id; | ||
req.context.user = user; | ||
next(); | ||
}) | ||
.catch(errorHandler(res)); | ||
}; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.