-
Notifications
You must be signed in to change notification settings - Fork 7
/
gauth.js
130 lines (114 loc) · 3.14 KB
/
gauth.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
var express = require('express');
var router = express.Router();
var secrets = require('../services/configuration');
var gapi = require('googleapis');
var oauth2Client = gauthEnabled() ? new gapi.auth.OAuth2(
secrets.config.gauth.client_id,
secrets.config.gauth.client_secret,
secrets.config.gauth.redirect_uris[0]) : null;
router.post('/login', function(req, res) {
if(!gauthEnabled()) {
return res.render('unauthlogin');
}
var options = {
access_type: 'offline',
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
].join(' ')
};
var generatedUrl = oauth2Client.generateAuthUrl(options);
res.redirect(generatedUrl);
});
router.post('/unauthlogin', function(req, res) {
var name = req.body.name,
email = req.body.email;
req.session = req.session || {};
req.session.tokens = name;
req.session.user = {
email : email
};
req.session.destroy = function (cb) {
delete req.session.tokens;
cb();
};
var path = req.session.custom_target;
delete req.session.custom_target;
res.redirect(path || '/');
});
router.get('/oauth2callback', function(req, res) {
var code = req.query.code;
if(code) {
req.session.code = code;
oauth2Client.getToken(code, function(err, tokens) {
if(err) {
console.log(err);
}
req.session.tokens = tokens;
setSessionData(req, oauth2Client, tokens, function() {
var path = req.session.custom_target;
delete req.session.custom_target;
res.redirect(path || '/');
});
});
}
else {
res.redirect('/');
}
});
/* GET login page. */
router.get('/logout', function(req, res, next) {
req.session.destroy(function(err) {
if(err) res.send(err);
else res.redirect('/');
})
});
router.get('/*', function(req, res, next) {
//if (req.session.tokens || req.host === 'localhost') {
if (req.session.tokens) {
next();
}
else {
req.session.custom_target = req.path;
res.render('login');
}
});
function setSessionData(req, oauth2Client, tokens, cb) {
oauth2Client.setCredentials(tokens);
gapi.oauth2("v2").userinfo.get({auth:oauth2Client}, function(err, results){
req.session.user = results;
cb();
});
};
function gauthEnabled() {
if(secrets.config.gauth.client_secret) {
oauth2Client = new gapi.auth.OAuth2(
secrets.config.gauth.client_id,
secrets.config.gauth.client_secret,
secrets.config.gauth.redirect_uris[0]);
}
return !!secrets.config.gauth.client_secret;
}
/*
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
if(appAuthenticate(req))
if(req.session.custom_target)
res.redirect(req.session.custom_target);
else
res.redirect('/list');
else {
logout(req, true);
res.redirect('/unauthorized');
}
});
router.get('/*', ensureAuthenticated);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
req.session.custom_target = req.url;
res.redirect('/login');
}
*/
module.exports = router;