Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

authentication sync with user db

  • Loading branch information...
commit aa7c95d623082cf06aa743668330926a452fcc4d 1 parent c214136
Yohei Sasaki authored
2  apps/posts/scripts/generator.js
@@ -8,7 +8,7 @@ var cradle = require('cradle');
8 8 var db = new (cradle.Connection)().database('posts');
9 9 var NUM = 30;
10 10 var USER = {
11   - user_id: 'guest', user_name: 'Generator'
  11 + _id: 'guest', display_name: 'Generator'
12 12 };
13 13
14 14 function genString(str, n){
6 apps/system/helpers.js
@@ -41,14 +41,14 @@ module.exports = function(app){
41 41
42 42 linkToUser: function(userObj){
43 43 this.css(app.set('home') + 'css/system_helper.css');
44   - var url = app.set('home') + 'profiles/' + encodeURIComponent(userObj.user_id);
  44 + var url = app.set('home') + 'profiles/' + encodeURIComponent(userObj._id);
45 45 var klass = this.htmlescape(userObj.provider) || 'undefined';
46 46 var label;
47 47 if( klass == 'twitter' ){
48 48 // http://tweetimag.es/
49   - 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));
  49 + 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));
50 50 }else{
51   - label = this.htmlescape(userObj.user_name);
  51 + label = this.htmlescape(userObj.display_name);
52 52 }
53 53 return '<a href="' + url + '" class="system user ' + klass + '">' + label + '</a>';
54 54 }
9 doc/source/ref/helper.rst
Source Rendered
@@ -127,8 +127,13 @@ This section lists view helpers (including dynamicHelpers) defined in sunrise. Y
127 127
128 128 Gets the user object.
129 129
130   - :currentUser.user_name: user name
131   - :currentUser.user_id: user identifier.
  130 + :currentUser.display_name: user display name.
  131 + :currentUser._id: user identifier.
  132 + :currentUser.name: same as ``_id``.
  133 + :currentUser.provider: authentication provider name(such as ``'twitter'``)
  134 + :currentUser.roles: an array of String for user roles.
  135 + :currentUser.lastLogin: last login time (Date object).
  136 +
132 137
133 138 .. attribute:: isLogin
134 139
4 lib/db.js
@@ -137,8 +137,8 @@ function normalizeFixture(doc){
137 137 var field = arguments[i];
138 138 if( doc[field] === undefined ){
139 139 doc[field] = {
140   - user_id: 'guest',
141   - user_name: 'Guest'
  140 + name: 'guest',
  141 + display_name: 'Guest'
142 142 };
143 143 }
144 144 }
63 lib/middleware/auth.js
@@ -4,15 +4,23 @@ var url = require('url'),
4 4 var merge = require('../utils').merge,
5 5 logger = require('../utils').getLogger('auth');
6 6 var OAuth = require('oauth').OAuth;
  7 +var userDB = require('../db').userDB;
  8 +var crypto = require('crypto');
  9 +
  10 +var sha1 = function(str){
  11 + var shasum = crypto.createHash('sha1');
  12 + shasum.update(str);
  13 + return shasum.digest('hex');
  14 +};
7 15
8 16 var GUEST = {
9 17 user_id: 'guest',
10   - user_name: 'Guest'
  18 + display_name: 'Guest'
11 19 };
12 20
13 21 var ADMIN = {
14 22 user_id: 'admin',
15   - user_name: 'Administrator'
  23 + display_name: 'Administrator'
16 24 };
17 25
18 26 // TODO: support more providers
@@ -28,7 +36,7 @@ var Providers = {
28 36 normalizeUser : function(results){
29 37 return {
30 38 user_id: "twitter.com:" + results.user_id,
31   - user_name: results.screen_name,
  39 + display_name: results.screen_name,
32 40 provider: "twitter"
33 41 };
34 42 }
@@ -78,13 +86,52 @@ exports.requiredWith = function(providerName){
78 86 logger.info('recover the failure with redirecion to ' + callbackUrl);
79 87 res.redirect(callbackUrl);
80 88 }else{
81   - req.session.auth = merge({
  89 + var auth = merge({
82 90 providerName: providerName
83 91 }, provider.normalizeUser(results));
84   - logger.info('logged in as ' +
85   - req.session.auth.user_name +
86   - ' (uid = ' + req.session.auth.user_id + ')');
87   - res.redirect(callbackUrl);
  92 + userDB(function(db){
  93 + db.get('org.couchdb.user:' + auth.user_id, function(err, doc){
  94 + if( err ){
  95 + logger.info('Creating the new user object for ' + auth.user_id);
  96 + doc = {};
  97 + db.uuid(function(err, uuid){
  98 + // TODO automatically create internal user data.
  99 + doc._id = 'org.couchdb.user:' + auth.user_id,
  100 + doc.type = 'user';
  101 + doc.name = auth.user_id;
  102 + doc.display_name = auth.display_name;
  103 + doc.password_sha = sha1('password' + uuid);
  104 + doc.roles = [];
  105 + doc.salt = uuid;
  106 + doc.provider = auth.provider;
  107 + doc.lastLogin = new Date();
  108 + merge(auth, doc);
  109 + db.save(doc, function(err, doc2){
  110 + if( err ){
  111 + logger.error('Failed to create te new user object for ' + auth.user_id + ": " + JSON.stringify(err));
  112 + }
  113 + logger.info(doc.display_name + ' has logged in ' +
  114 + ' (uid = ' + doc._id + ')');
  115 + delete(doc.password_sha);
  116 + delete(doc.salt);
  117 + req.session.auth = doc;
  118 + res.redirect(callbackUrl);
  119 + });
  120 + });
  121 + }else{
  122 + logger.info('Updating the user object for ' + auth.user_id);
  123 + doc.lastLogin = new Date();
  124 + db.save(doc, function(err, doc2){
  125 + logger.info(doc.display_name + ' has logged in ' +
  126 + ' (uid = ' + doc._id + ')');
  127 + delete(doc.password_sha);
  128 + delete(doc.salt);
  129 + req.session.auth = doc;
  130 + res.redirect(callbackUrl);
  131 + });
  132 + }
  133 + });
  134 + });
88 135 }
89 136 });
90 137 }else{
2  test/fixtures/site/test_site/_attachments/templates/test_dynamicHelpers/currentUser.ejs
... ... @@ -1 +1 @@
1   -<%- currentUser.user_name %>
  1 +<%- currentUser.display_name %>
8 test/test.db.js
@@ -38,9 +38,9 @@ module.exports = {
38 38 },
39 39
40 40 "test userDB": function(){
41   - setTimeout(function(){
42   - assert.isNotNull(db.userDB);
43   - assert.eql('_users', db.userDB.name);
44   - }, 1000);
  41 + db.userDB(function(userDB){
  42 + assert.isNotNull(userDB);
  43 + assert.eql('_users', userDB.name);
  44 + });
45 45 }
46 46 };

0 comments on commit aa7c95d

Please sign in to comment.
Something went wrong with that request. Please try again.