Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

authentication sync with user db

  • Loading branch information...
commit aa7c95d623082cf06aa743668330926a452fcc4d 1 parent c214136
@yssk22 authored
View
2  apps/posts/scripts/generator.js
@@ -8,7 +8,7 @@ var cradle = require('cradle');
var db = new (cradle.Connection)().database('posts');
var NUM = 30;
var USER = {
- user_id: 'guest', user_name: 'Generator'
+ _id: 'guest', display_name: 'Generator'
};
function genString(str, n){
View
6 apps/system/helpers.js
@@ -41,14 +41,14 @@ module.exports = function(app){
linkToUser: function(userObj){
this.css(app.set('home') + 'css/system_helper.css');
- var url = app.set('home') + 'profiles/' + encodeURIComponent(userObj.user_id);
+ var url = app.set('home') + 'profiles/' + encodeURIComponent(userObj._id);
var klass = this.htmlescape(userObj.provider) || 'undefined';
var label;
if( klass == 'twitter' ){
// http://tweetimag.es/
- label = this.sprintf('<img width="18" height="18" src="http://img.tweetimag.es/i/%s_m" /><span>%s</span>', userObj.user_name, this.htmlescape(userObj.user_name));
+ label = this.sprintf('<img width="18" height="18" src="http://img.tweetimag.es/i/%s_m" /><span>%s</span>', userObj.display_name, this.htmlescape(userObj.display_name));
}else{
- label = this.htmlescape(userObj.user_name);
+ label = this.htmlescape(userObj.display_name);
}
return '<a href="' + url + '" class="system user ' + klass + '">' + label + '</a>';
}
View
9 doc/source/ref/helper.rst
@@ -127,8 +127,13 @@ This section lists view helpers (including dynamicHelpers) defined in sunrise. Y
Gets the user object.
- :currentUser.user_name: user name
- :currentUser.user_id: user identifier.
+ :currentUser.display_name: user display name.
+ :currentUser._id: user identifier.
+ :currentUser.name: same as ``_id``.
+ :currentUser.provider: authentication provider name(such as ``'twitter'``)
+ :currentUser.roles: an array of String for user roles.
+ :currentUser.lastLogin: last login time (Date object).
+
.. attribute:: isLogin
View
4 lib/db.js
@@ -137,8 +137,8 @@ function normalizeFixture(doc){
var field = arguments[i];
if( doc[field] === undefined ){
doc[field] = {
- user_id: 'guest',
- user_name: 'Guest'
+ name: 'guest',
+ display_name: 'Guest'
};
}
}
View
63 lib/middleware/auth.js
@@ -4,15 +4,23 @@ var url = require('url'),
var merge = require('../utils').merge,
logger = require('../utils').getLogger('auth');
var OAuth = require('oauth').OAuth;
+var userDB = require('../db').userDB;
+var crypto = require('crypto');
+
+var sha1 = function(str){
+ var shasum = crypto.createHash('sha1');
+ shasum.update(str);
+ return shasum.digest('hex');
+};
var GUEST = {
user_id: 'guest',
- user_name: 'Guest'
+ display_name: 'Guest'
};
var ADMIN = {
user_id: 'admin',
- user_name: 'Administrator'
+ display_name: 'Administrator'
};
// TODO: support more providers
@@ -28,7 +36,7 @@ var Providers = {
normalizeUser : function(results){
return {
user_id: "twitter.com:" + results.user_id,
- user_name: results.screen_name,
+ display_name: results.screen_name,
provider: "twitter"
};
}
@@ -78,13 +86,52 @@ exports.requiredWith = function(providerName){
logger.info('recover the failure with redirecion to ' + callbackUrl);
res.redirect(callbackUrl);
}else{
- req.session.auth = merge({
+ var auth = merge({
providerName: providerName
}, provider.normalizeUser(results));
- logger.info('logged in as ' +
- req.session.auth.user_name +
- ' (uid = ' + req.session.auth.user_id + ')');
- res.redirect(callbackUrl);
+ userDB(function(db){
+ db.get('org.couchdb.user:' + auth.user_id, function(err, doc){
+ if( err ){
+ logger.info('Creating the new user object for ' + auth.user_id);
+ doc = {};
+ db.uuid(function(err, uuid){
+ // TODO automatically create internal user data.
+ doc._id = 'org.couchdb.user:' + auth.user_id,
+ doc.type = 'user';
+ doc.name = auth.user_id;
+ doc.display_name = auth.display_name;
+ doc.password_sha = sha1('password' + uuid);
+ doc.roles = [];
+ doc.salt = uuid;
+ doc.provider = auth.provider;
+ doc.lastLogin = new Date();
+ merge(auth, doc);
+ db.save(doc, function(err, doc2){
+ if( err ){
+ logger.error('Failed to create te new user object for ' + auth.user_id + ": " + JSON.stringify(err));
+ }
+ logger.info(doc.display_name + ' has logged in ' +
+ ' (uid = ' + doc._id + ')');
+ delete(doc.password_sha);
+ delete(doc.salt);
+ req.session.auth = doc;
+ res.redirect(callbackUrl);
+ });
+ });
+ }else{
+ logger.info('Updating the user object for ' + auth.user_id);
+ doc.lastLogin = new Date();
+ db.save(doc, function(err, doc2){
+ logger.info(doc.display_name + ' has logged in ' +
+ ' (uid = ' + doc._id + ')');
+ delete(doc.password_sha);
+ delete(doc.salt);
+ req.session.auth = doc;
+ res.redirect(callbackUrl);
+ });
+ }
+ });
+ });
}
});
}else{
View
2  test/fixtures/site/test_site/_attachments/templates/test_dynamicHelpers/currentUser.ejs
@@ -1 +1 @@
-<%- currentUser.user_name %>
+<%- currentUser.display_name %>
View
8 test/test.db.js
@@ -38,9 +38,9 @@ module.exports = {
},
"test userDB": function(){
- setTimeout(function(){
- assert.isNotNull(db.userDB);
- assert.eql('_users', db.userDB.name);
- }, 1000);
+ db.userDB(function(userDB){
+ assert.isNotNull(userDB);
+ assert.eql('_users', userDB.name);
+ });
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.