-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.js
79 lines (72 loc) · 2.47 KB
/
webapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var codeplex = require('./lib/codeplex')
, CodeplexStrategy = require('./lib/passport-codeplex');
module.exports = {
appConfig: {
hostname: 'http://localhost:3000',
clientId: 'feecb876f9044bca8a86ab9089fba8b0',
clientSecret: 'efc1e8c120c24b59b12ad0f4f7d32d02'
},
// oauth global routes
globalRoutes: function (app, context) {
app.get('/oauth', context.passport.authenticate('codeplex'));
app.get(
'/oauth/callback',
context.passport.authenticate('codeplex', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/projects');
});
},
listRepos: function (account, next) {
codeplex.getRepos(account, function(err, data) {
if (err) return next(err)
next(null, data.map(function(repo) { return codeplex.parseRepo(account, repo); }).filter(function (repo) {
return repo.config.scm === 'Git' ||
repo.config.scm === 'Mercurial';
}))
});
},
// register the passport auth strategy
auth: function (passport, context) {
var config = this.appConfig
passport.use(new CodeplexStrategy({
clientID: this.appConfig.clientId,
clientSecret: this.appConfig.clientSecret,
callbackURL: this.appConfig.hostname + '/ext/codeplex/oauth/callback',
passReqToCallback: true
}, validateAuth));
}
}
function validateAuth(req, token, tokenSecret, profile, done) {
if (!req.user) {
console.warn('Codeplex OAuth but no logged-in user')
req.flash('account', "Cannot link a codeplex account if you aren't logged in")
return done()
}
var account = req.user.account('codeplex', profile.UserName)
if (account) {
console.warn("Trying to attach a codeplex account that's already attached...")
req.flash('account', 'That codeplex account is already linked. <a href="https://codeplex.com/site/signout/" target="_blank">Sign out of codeplex</a> before you click "Add Account".')
return done(null, req.user)
}
req.user.accounts.push(makeAccount(token, tokenSecret, profile))
req.user.save(function (err) {
done(err, req.user);
})
}
function makeAccount(token, tokenSecret, profile) {
var username = profile.UserName;
return {
provider: 'codeplex',
id: username,
display_url: 'https://www.codeplex.com/site/users/view/' + username,
title: username,
config: {
accessToken: token,
tokenSecret: tokenSecret,
login: username,
name: username,
avatar: profile.Avatar,
}
}
}