forked from Shopify/connect-googleapps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogleapps.js
60 lines (60 loc) · 2.01 KB
/
googleapps.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
(function() {
var openid;
openid = require('openid');
module.exports = function(domain, options) {
var hostMetaProxy, oExtensions, oRelyingParty;
if (options == null) {
options = {};
}
oExtensions = [
new openid.AttributeExchange({
'http://axschema.org/contact/email': 'required'
})
];
hostMetaProxy = function(identifier, host) {
return 'https://www.google.com/accounts/o8/.well-known/host-meta?hd=' + host;
};
oRelyingParty = new openid.RelyingParty('', null, false, false, oExtensions);
return function(req, res, next) {
var _ref;
oRelyingParty.returnUrl = "http" + (options.secure ? 's' : '') + "://" + req.headers.host + "/_auth";
if ((_ref = req.session) != null ? _ref.authenticated : void 0) {
return next();
}
if (/^\/_auth/.test(req.url)) {
return oRelyingParty.verifyAssertion(req, function(result) {
if (result != null ? result.authenticated : void 0) {
if (result.claimedIdentifier.indexOf(domain) === -1) {
res.writeHead(403, result.error);
return res.end();
}
req.session.authenticated = true;
req.session.user = result.email;
res.writeHead(302, {
Location: req.session.returnTo || '/'
});
req.session.returnTo = null;
return res.end();
} else {
console.log(result);
res.writeHead(403, result.error);
return res.end();
}
});
} else {
return oRelyingParty.authenticate("https://www.google.com/accounts/o8/site-xrds?hd=" + domain, false, function(authUrl) {
if (!authUrl) {
res.writeHead(500, 'google auth error');
return res.end();
} else {
req.session.returnTo = req.url;
res.writeHead(302, {
Location: authUrl
});
return res.end();
}
});
}
};
};
}).call(this);