Skip to content

Commit 68d9acc

Browse files
committed
Merge remote-tracking branch 'origin/master' into SAN-973-php-getting-started
* origin/master: 0.5.3 0.5.2 fixing gravatar url retrieval and storage lowercase CodeNow.......... remove log proper gravatar url generation fix 0.5.1 remove escaping fixed missed tests change up regex to only get instance names wip - fixing dependency graph debugging take supplied refresh token whitespace non-overwrite update user props
2 parents 75ba6d4 + 51e17d4 commit 68d9acc

File tree

6 files changed

+102
-72
lines changed

6 files changed

+102
-72
lines changed

lib/middlewares/passport.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
'use strict';
22

3-
var passport = require('passport');
3+
var Boom = require('dat-middleware').Boom;
44
var GitHubStrategy = require('passport-github').Strategy;
55
var GitHubTokenStrategy = require('models/passport-github-token');
6+
var Github = require('models/apis/github');
7+
var User = require('models/mongo/user');
68
var async = require('async');
7-
var Boom = require('dat-middleware').Boom;
9+
var debug = require('debug')('runnable-api|passport');
810
var find = require('101/find');
911
var hasProps = require('101/has-properties');
10-
var User = require('models/mongo/user');
11-
var Github = require('models/apis/github');
1212
var keypather = require('keypather')();
13+
var passport = require('passport');
1314

1415
// Example:
1516
// https://github.com/jaredhanson/passport-github/blob/master/examples/login/app.js
1617

18+
var GITHUB_CALLBACK_URL = process.env.GITHUB_CALLBACK_URL;
1719
var GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID;
1820
var GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET;
19-
var GITHUB_CALLBACK_URL = process.env.GITHUB_CALLBACK_URL;
2021
var GITHUB_SCOPE = process.env.GITHUB_SCOPE;
2122

2223
// Passport session setup.
@@ -34,7 +35,7 @@ passport.deserializeUser(function(userId, done) {
3435
User.findById(userId, done);
3536
});
3637

37-
passport.use(new GitHubTokenStrategy({}, fetchOrCreateUser));
38+
passport.use(new GitHubTokenStrategy({}, manageUser));
3839

3940
// Use the GitHubStrategy within Passport.
4041
// Strategies in Passport require a `verify` function, which accept
@@ -46,10 +47,10 @@ passport.use(new GitHubStrategy({
4647
callbackURL: GITHUB_CALLBACK_URL,
4748
scope: GITHUB_SCOPE
4849
},
49-
fetchOrCreateUser
50+
manageUser
5051
));
5152

52-
function fetchOrCreateUser (accessToken, refreshToken, profile, done) {
53+
function manageUser (accessToken, refreshToken, profile, done) {
5354
async.waterfall([
5455
User.findByGithubId.bind(User, profile.id),
5556
getUserPrimaryEmail,
@@ -76,29 +77,45 @@ function fetchOrCreateUser (accessToken, refreshToken, profile, done) {
7677
});
7778
}
7879

80+
/*jshint maxcomplexity:20*/
7981
function updateOrCreateUser (user, primaryEmail, cb) {
80-
user = user || {};
82+
8183
profile.accessToken = accessToken;
82-
profile.refreshToken = refreshToken || user.refreshToken;
84+
profile.refreshToken = refreshToken;
8385
profile.username = profile.username || profile.login;
84-
var update = {
85-
$set: {
86-
'email': user.email || primaryEmail,
86+
87+
if (user && user._id) {
88+
debug('existing user, updating...');
89+
// found existing user, updating
90+
user.email = primaryEmail;
91+
user.accounts = {
92+
github: profile
93+
};
94+
user.gravatar = getGravatarUrl(profile);
95+
user.save(function (err) {
96+
if (err) {
97+
return cb(err);
98+
}
99+
User.findByGithubId(profile.id, cb);
100+
});
101+
}
102+
else {
103+
debug('new user, inserting...');
104+
// fresh user, inserting
105+
var data = {
106+
'email': primaryEmail,
87107
'accounts': { 'github': profile },
108+
'gravatar': getGravatarUrl(profile),
88109
'permissionLevel': 1,
89-
'gravatar': getGravatarUrl(profile)
90-
}
91-
};
92-
var opts = {
93-
upsert: true
94-
};
95-
async.waterfall([
96-
User.updateByGithubId.bind(User, profile.id, update, opts),
97-
function (docsUpdated, model, cb) {
110+
'created': new Date()
111+
};
112+
User.create(data, function (err) {
113+
if (err) {
114+
return cb(err);
115+
}
98116
User.findByGithubId(profile.id, cb);
99-
}
100-
], cb);
101-
117+
});
118+
}
102119
function getGravatarUrl (profile) {
103120
var gravatar = 'https://gravatar.com/avatar/' + keypather.get(profile, '_json.gravatar_id');
104121
var avatar = keypather.get(profile, '_json.avatar_url');

lib/models/apis/graph.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ Graph.prototype.graphInstanceDeps = function (instance, oldName, cb) {
189189

190190
function getAllInstanceNamesRegex (data, cb) {
191191
var instance = data.instance;
192-
data.nameRegex = new RegExp('([a-z0-9-_]+)\\.[^\\.]+\\.' + process.env.DOMAIN);
192+
data.nameRegex = new RegExp(
193+
'([a-z0-9-_]+)-' +
194+
instance.owner.username.toLowerCase() +
195+
'\\.' +
196+
process.env.USER_CONTENT_DOMAIN);
193197
Instance.find(
194198
{
195199
owner: {

lib/models/mongo/user.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ UserSchema.statics.publicFind = function () {
135135
proxyCallbackToProtectEmail(args);
136136
this.find.apply(this, args);
137137
};
138+
138139
UserSchema.statics.publicFindOne = function () {
139140
var args = Array.prototype.slice.call(arguments);
140141
if (typeof args[1] === 'function') {
@@ -144,6 +145,7 @@ UserSchema.statics.publicFindOne = function () {
144145
proxyCallbackToProtectEmail(args);
145146
this.findOne.apply(this, args);
146147
};
148+
147149
UserSchema.statics.publicFindById = function () {
148150
var args = Array.prototype.slice.call(arguments);
149151
if (typeof args[1] === 'function') {
@@ -153,11 +155,13 @@ UserSchema.statics.publicFindById = function () {
153155
proxyCallbackToProtectEmail(args);
154156
this.findById.apply(this, args);
155157
};
158+
156159
UserSchema.statics.findByGithubId = function (id) {
157160
var args = Array.prototype.slice.call(arguments, 1);
158161
args.unshift({ 'accounts.github.id': id });
159162
this.findOne.apply(this, args);
160163
};
164+
161165
UserSchema.statics.updateByGithubId = function (id) {
162166
var args = Array.prototype.slice.call(arguments, 1);
163167
args.unshift({ 'accounts.github.id': id });

lib/routes/instances/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ app.post('/instances/:id/actions/deploy',
695695
// create container
696696
createSaveAndNetworkContainer,
697697
graph.create(),
698+
instances.model.getGithubUsername('sessionUser'),
698699
graph.model.graphInstanceDeps('instance'))
699700
.catch(
700701
mw.req().setToErr('err'),
@@ -754,6 +755,7 @@ app.post('/instances/:id/actions/redeploy',
754755
// create container
755756
createSaveAndNetworkContainer,
756757
graph.create(),
758+
instances.model.getGithubUsername('sessionUser'),
757759
graph.model.graphInstanceDeps('instance'))
758760
.catch(
759761
mw.req().setToErr('err'),
@@ -935,6 +937,7 @@ app.post('/instances/:id/actions/regraph',
935937
checkFound('user', 'Owner not found'),
936938
mw.req().set('ownerUsername', 'user.login'),
937939
graph.create(),
940+
instances.model.getGithubUsername('sessionUser'),
938941
graph.model.graphInstanceDeps('instance'),
939942
instances.model.populateModels(),
940943
mw.req().set('instance.owner.username', 'ownerUsername'),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "api",
33
"description": "Runnable API Server",
4-
"version": "0.5.0",
4+
"version": "0.5.3",
55
"repository": {
66
"type": "git",
77
"url": "http://github.com/CodeNow/api.git"

0 commit comments

Comments
 (0)