Permalink
Browse files

getting auth and user population in session working

  • Loading branch information...
Warner Onstine
Warner Onstine committed Jan 9, 2015
1 parent de712b2 commit a8ec1ce91a921de13869095f0731415908c99f72
Showing with 289 additions and 48 deletions.
  1. +5 −3 app.js
  2. +0 −1 config/index.js
  3. +6 −6 config/redis.js
  4. +0 −1 config/routes.js
  5. +2 −2 config/server.js
  6. +7 −1 config/settings.js
  7. +2 −1 config/test_config.json
  8. +24 −15 controllers/auth.js
  9. +1 −0 controllers/users.js
  10. +31 −16 gulpfile.js
  11. +28 −0 models/todo.js
  12. +2 −1 models/user.js
  13. +11 −0 test/data/users.data.js
  14. +123 −0 test/specs/user.test.js
  15. +46 −0 test/utils/data.js
  16. +1 −1 utils/auth.js
View
8 app.js
@@ -1,7 +1,7 @@
var cluster = require('cluster'),
numCPUs = Math.min(2,require('os').cpus().length);
-if (cluster.isMaster && process.env.NODE_ENV != "testing") {
+if (cluster.isMaster && process.env.NODE_ENV != "test") {
// Fork workers
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
@@ -54,7 +54,9 @@ var
var
mongoose = require("mongoose");
+ServerConf = require("./config")(server);
+console.log("env port %s, conf port %s",process.env.PORT, conf.get('port'));
server.listen(conf.get('port'), function(){
console.log("Restify listening on port " + conf.get('port'));
});
@@ -78,6 +80,6 @@ process.on('uncaughtException', function (err) {
}, 30000);
killtimer.unref();
- if(process.env.NODE_ENV != "testing") cluster.worker.disconnect();
- if(process.env.NODE_ENV == "testing") process.exit(1);
+ if(process.env.NODE_ENV != "test") cluster.worker.disconnect();
+ if(process.env.NODE_ENV == "test") process.exit(1);
});
View
@@ -3,7 +3,6 @@ var Config = function(server) {
var server_config = require("./server")(server);
var routes = require("./routes")(server);
- return require("./settings");
};
module.exports = Config;
View
@@ -1,16 +1,16 @@
var Redis = function(){
var
- Settings = require("./settings"),
+ conf = require("./settings"),
redis = require("redis"),
rclient = redis.createClient(
- Settings.redis.options.port,
- Settings.redis.options.host,
- Settings.redis.options
+ conf.get('redis.options.port'),
+ conf.get('redis.options.host'),
+ conf.get('redis.options')
);
- redis.debug_mode = Settings.redis.debug;
+ redis.debug_mode = conf.get('redis.debug');
- if(Settings.redis.options.pass) rclient.auth(Settings.redis.options.pass);
+ if(conf.get('redis.options.pass')) rclient.auth(conf.get('redis.options.pass'));
return rclient;
}();
View
@@ -1,7 +1,6 @@
var Routes = function(server) {
var
- Settings = require("./settings"),
auth = require("../controllers/auth"),
users = require("../controllers/users");
View
@@ -1,7 +1,7 @@
var ServerConfig = function(server) {
var
- Settings = require("./settings"),
+ conf = require("./settings"),
restify = require("restify"),
restifyOAuth2 = require("restify-oauth2"),
auth = require("../controllers/auth"),
@@ -22,7 +22,7 @@ var ServerConfig = function(server) {
restifyOAuth2.ropc(server, {
tokenEndpoint: "/token",
hooks: auth.oauthHooks,
- tokenExpirationTime: Settings.auth.token.maxAge
+ tokenExpirationTime: conf.get('auth.token.maxAge')
});
/* Start CORS: All of this is required to get this working. */
View
@@ -34,7 +34,11 @@ var conf = convict({
},
redis: {
url: "redis://localhost:6379",
- options: {},
+ options: {
+ host: "",
+ port: "",
+ pass: ""
+ },
debug: false,
prefix: "todo:",
env: "REDISTOGO_URL"
@@ -43,11 +47,13 @@ var conf = convict({
var env = conf.get('env');
+console.log("port before loading env specifics %s, env port %s", conf.get('port'),process.env.PORT);
conf.loadFile (__dirname + '/'+ env + '_config.json');
conf.validate();
var saveRedisDetails = function () {
+ console.log("calling redis details");
var redis_url = require("url").parse(conf.get('redis.url'));
conf.set('redis.options.host', redis_url.hostname);
conf.set('redis.options.port', redis_url.port);
View
@@ -1,3 +1,4 @@
{
-
+ "port": 5536,
+ "mongo": "mongodb://localhost/todo-test"
}
View
@@ -1,7 +1,7 @@
var AuthController = function(){
var
- Settings = require("../config/settings"),
+ conf = require("../config/settings"),
User = require("../models/user"),
AuthUtils = require("../utils/auth"),
JSONUtils = require("../utils/json"),
@@ -31,25 +31,27 @@ var AuthController = function(){
.findOne({
_id: user._id || user
})
- .populate('services.clientService')
.lean()
.exec(function(err, user){
if(err) return done(err);
+ delete user.hashedPassword;
+ delete user.__v;
+
return done(err, user);
});
};
var _generateToken = function(data){
var random = Math.floor(Math.random() * 100001);
var timestamp = (new Date()).getTime();
- var sha256 = crypto.createHmac("sha256", random + Settings.auth.token.salt + timestamp);
+ var sha256 = crypto.createHmac("sha256", random + conf.get('auth.token.salt') + timestamp);
return sha256.update(data).digest("base64");
};
var _saveToken = function(token, user, done) {
var
- id = Settings.redis.prefix + token,
- maxAge = Settings.auth.token.maxAge;
+ id = conf.get('redis.prefix') + token,
+ maxAge = conf.get('auth.token.maxAge');
user = JSON.stringify(user);
@@ -59,7 +61,8 @@ var AuthController = function(){
};
var _getTokenData = function(token, done) {
- var id = Settings.redis.prefix + token;
+ console.log("getting token data");
+ var id = conf.get('redis.prefix') + token;
rclient.get(id, function(err, data){
if (err || !data) return done(err);
@@ -68,31 +71,32 @@ var AuthController = function(){
};
var _clearTokenData = function(token, done) {
- var id = Settings.redis.prefix + token;
+ var id = conf.get('redis.prefix') + token;
rclient.del(id, function(err, data){
if (err || !data) return done(err);
return done();
});
};
- var _validateClient = function(id, secret, done){
- console.log("validateClient %s %s", id, secret);
+ var _validateClient = function(client, req, done){
+ // console.log("validateClient", client.clientId, client.clientSecret);
+ // TODO: eventually we want to validate who this is coming from, but this is fine for the moment
return done(null,true);
};
- var _grantUserToken = function(email, password, done){
- console.log("grantUserToken %s, %s", email, password);
- var hashedPassword = AuthUtils.hashPassword(password);
+ var _grantUserToken = function(grant, req, done){
+ var hashedPassword = AuthUtils.hashPassword(grant.password);
User.Model
.findOne({
- email: email.toLowerCase(),
+ email: grant.username.toLowerCase(),
hashedPassword: hashedPassword
})
.exec(function(err, user){
+ console.log("user ", user);
if(err || !user) return done(err, false);
- var token = _generateToken(email + ":" + password);
+ var token = _generateToken(grant.username + ":" + grant.password);
_saveToken(token, user);
@@ -102,8 +106,13 @@ var AuthController = function(){
};
- var _authenticateToken = function(token, done) {
+ var _authenticateToken = function(token, req, done) {
_getTokenData(token, function(err, data){
+ delete data.hashedPassword;
+ delete data.__v;
+
+ req.user = data;
+ console.log("setting user %j", data);
return done(err, data);
});
};
View
@@ -11,6 +11,7 @@ var UsersController = function(){
var _current = function(req, res, next) {
var
user = req.user;
+ console.log("user %j", user);
return res.send(user);
};
View
@@ -3,30 +3,45 @@ var
mocha = require('gulp-mocha'),
jshint = require('gulp-jshint'),
exit = require('gulp-exit'),
- Q = require('q');
+ Q = require('q'),
+ TestData = require("./test/utils/data");
-process.env.PORT = 4000;
-process.env.MONGO_DEBUG = true;
-
-gulp.task('setup-e2e-data', function(callback){
- var E2EData = require(__dirname + '/./e2e/data');
- E2EData.createTestData(function(err) {
+gulp.task('setup-test-data',['clear-test-data'], function(callback){
+ TestData.createTestData(function(err){
return callback(err);
});
});
-gulp.task('clear-e2e-data', function(callback){
- var E2EData = require(__dirname + '/./e2e/data');
- E2EData.clearDatabase(function(err){
- return callback(err).pipe(exit());
+gulp.task('clear-test-data', function(callback){
+ TestData.clearDatabase(function(err){
+ return callback(err);
});
});
-gulp.task('start-server', function() {
+gulp.task('start-test-server', function() {
+
+ var conf = require("./config/settings");
+ GLOBAL.server = "http://localhost:" + conf.get('port');
+ var
+ app = require(__dirname + '/./app');
+});
+
+gulp.task('run-tests',['start-test-server'], function() {
+ return gulp.src(['test/specs/*.test.js'], { read: false })
+ .pipe(mocha({
+ reporter: 'spec',
+ globals: {
+ should: require('should')
+ }
+ }))
+ .pipe(exit());
+});
- var
- app = require(__dirname + '/./app');
+gulp.task('lint', function(done) {
+ return gulp.src(['./lib/*.js', 'test/*.test.js'])
+ .pipe(jshint())
+ .pipe(jshint.reporter('default'));
});
-gulp.task('clean-populate', ['clear-e2e-data','setup-e2e-data']);
-gulp.task('default', ['clear-e2e-data','setup-e2e-data', 'start-server']);
+gulp.task('clean-populate', ['clear-test-data','setup-test-data']);
+gulp.task('default', ['clean-populate', 'run-tests']);
View
@@ -0,0 +1,28 @@
+var TodoModel = function(){
+
+ var
+ mongoose = require("mongoose"),
+ Schema = mongoose.Schema;
+
+ var _jsonSchema = {
+ _id: {type:Schema.Types.ObjectId, "default":mongoose.Types.ObjectId},
+ text: String,
+ created: {type: Date, "default": Date.now},
+ completed: {type:Boolean, "default": false},
+ completedOn: Date,
+ position: {type:Number, "default":0}
+ };
+
+ var _schema = mongoose.Schema(_jsonSchema);
+
+ var _model = mongoose.model("Todo", _schema);
+
+ return {
+ Schema: _schema,
+ Model: _model,
+ jsonSchema: _jsonSchema
+ };
+
+}();
+
+module.exports = TodoModel;
View
@@ -10,7 +10,8 @@ var UserModel = function(){
_id: {type:Schema.Types.ObjectId, "default":mongoose.Types.ObjectId},
name: String,
email: String,
- hashedPassword: String
+ hashedPassword: String,
+ created: {type: Date, "default": Date.now}
};
var _schema = mongoose.Schema(_jsonSchema);
View
@@ -0,0 +1,11 @@
+module.exports = [
+ {
+ name: "Warner Onstine",
+ email: "warner@test.com"
+ },
+ {
+ name: "Warner Test2",
+ email: "warner2@test.com"
+ }
+
+];
Oops, something went wrong.

0 comments on commit a8ec1ce

Please sign in to comment.