3 var env = require('gitter-web-env');
4 var logger = env.logger;
5 var config = env.config;
6 var identifyRoute = env.middlewares.identifyRoute;
8 var jwt = require('jwt-simple');
9 var uuid = require('uuid/v4');
10 var url = require('url');
11 var express = require('express');
12 var GithubMeService = require('gitter-web-github').GitHubMeService;
13 var oauth2 = require('../web/oauth2');
14 var ensureLoggedIn = require('../web/middlewares/ensure-logged-in');
15 var resolveUserAvatarUrl = require('gitter-web-shared/avatars/resolve-user-avatar-srcset');
17 var gitlab = require('./auth-providers/gitlab');
18 var github = require('./auth-providers/github');
19 var google = require('./auth-providers/google');
20 var twitter = require('./auth-providers/twitter');
21 var linkedin = require('./auth-providers/linkedin');
22 var validatedMessage = require('../web/validated-message');
24 var router = express.Router({ caseSensitive: true, mergeParams: true });
26 router.get('/*', function(req, res, next) {
27 // Fix for Windows Phone
28 req.nonApiRoute = true;
32 router.get('/', identifyRoute('login'), function(req, res) {
34 source: req.query.source || 'login_page-login',
35 returnTo: req.query.returnTo,
36 bootScriptName: 'router-login',
37 cssFileName: 'styles/login.css',
38 // TODO: remove this and just show it anyway
43 // ----------------------------------------------------------
44 // Common across different providers
45 // ----------------------------------------------------------
47 router.get('/upgrade-failed', identifyRoute('login-upgrade-failed'), function(req, res) {
48 res.render('upgrade-failed');
51 router.get('/failed', identifyRoute('login-failed'), function(req, res) {
52 var message = validatedMessage.validate(
55 'An error occurred. Please try again'
58 res.render('login-failed', {
63 router.get('/token-revoked', identifyRoute('token-revoked'), function(req, res) {
65 res.render('token-revoked', {
66 appsLink: config.get('web:basepath') + '/apps'
70 router.get('/user-agent-revoked', identifyRoute('user-agent-revoked'), function(req, res) {
72 res.render('user-agent-revoked', {
73 appsLink: config.get('web:basepath') + '/apps'
77 // ----------------------------------------------------------
79 // ----------------------------------------------------------
81 router.get('/github', github.login);
82 router.get('/invited', github.invited);
83 router.get('/upgrade', github.upgradeLandingPage);
84 router.post('/upgrade', github.upgrade);
86 // alias the old /callback to the new /github/callback for backwards
87 // compatibility and so we can switch over without downtime
88 ['/github/callback', '/callback'].forEach(function(path) {
89 router.get(path, github.callback);
92 // ----------------------------------------------------------
94 // ----------------------------------------------------------
96 router.get('/gitlab', gitlab.login);
97 router.get('/gitlab/callback', gitlab.callback);
99 // ----------------------------------------------------------
101 // ----------------------------------------------------------
103 router.get('/google', google.login);
104 router.get('/google/callback', google.callback);
106 // ----------------------------------------------------------
108 // ----------------------------------------------------------
110 router.get('/twitter', twitter.login);
111 router.get('/twitter/callback', twitter.callback);
113 // ----------------------------------------------------------
115 // ----------------------------------------------------------
117 router.get('/linkedin', linkedin.login);
118 router.get('/linkedin/callback', linkedin.callback);
120 // ----------------------------------------------------------
121 // OAuth for our own clients
122 // ----------------------------------------------------------
125 router.get('/oauth/authorize', identifyRoute('login-oauth-authorize'), oauth2.authorization);
127 router.post('/oauth/authorize/decision', identifyRoute('login-oauth-decision'), oauth2.decision);
129 router.post('/oauth/token', identifyRoute('login-oauth-token'), oauth2.token);
131 router.all('/oauth/callback', identifyRoute('login-oauth-callback'), function(req, res) {
133 'Wait time is the worst / I can hardly sit / No one has the time / Someone is always late'
137 // Wait? Why is this here?
138 // REMOVE IT: app.post('/oauth/authorize/decision', oauth2.decision);
140 // Zendesk login callback
141 router.get('/zendesk', ensureLoggedIn, identifyRoute('login-zendesk'), function(req, res, next) {
142 var ghMe = new GithubMeService(req.user);
145 .then(function(email) {
146 var cfg = config.get('zendesk');
148 iat: new Date().getTime() / 1000,
150 name: req.user.displayName,
152 external_id: req.user.id,
153 remote_photo_url: resolveUserAvatarUrl(req.user, 128),
155 username: req.user.username
159 logger.info('Sending data to Zendesk', payload);
161 var token = jwt.encode(payload, cfg.sharedKey);
162 var redirect = 'https://' + cfg.subdomain + '.zendesk.com/access/jwt?jwt=' + token;
164 var query = url.parse(req.url, true).query;
166 if (query.return_to) {
167 redirect += '&return_to=' + encodeURIComponent(query.return_to);
170 res.redirect(redirect);
175 router.get('/embed', ensureLoggedIn, function(req, res) {
176 res.render('embed-login-complete');
179 module.exports = router;