login.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const config = require("../config.js")
  2. ,Slack = require("../src/slack.js").Slack
  3. ,GoogleOAuth = require("../src/googleOAuth.js").GoogleOAuth
  4. ,FacebookOAuth = require("../src/facebookOAuth.js").FacebookOAuth
  5. ,slackManager = require("../src/slackManager.js").SlackManager
  6. ,accountManager = require("../src/accounts.js").accountManager
  7. ,templates = require('./_templates.js');
  8. function checkTokens(service, req, cb) {
  9. switch (service) {
  10. case "slack":
  11. if (req.urlObj.queryTokens.code) {
  12. Slack.getUserMail(req.urlObj.queryTokens.code, config.login.slack.redirect_uri, (email) => {
  13. if (email) {
  14. console.log("from slack email " +email);
  15. var account = accountManager.fromSlackEmailAuth(email);
  16. cb(account);
  17. } else {
  18. cb(null);
  19. }
  20. });
  21. } else {
  22. cb(null);
  23. }
  24. break;
  25. case "google":
  26. if (req.urlObj.queryTokens.code) {
  27. GoogleOAuth.getUserId(req.urlObj.queryTokens.code, (id) => {
  28. if (id) {
  29. console.log("from google id " +id);
  30. var account = accountManager.fromGoogleIdAuth(id);
  31. cb(account);
  32. } else {
  33. cb(null);
  34. }
  35. });
  36. } else {
  37. cb(null);
  38. }
  39. break;
  40. case "facebook":
  41. if (req.urlObj.queryTokens.code) {
  42. FacebookOAuth.getUserId(req.urlObj.queryTokens.code[0], (id) => {
  43. if (id) {
  44. console.log("from facebook id " +id);
  45. var account = accountManager.fromFacebookIdAuth(id);
  46. cb(account);
  47. } else {
  48. cb(null);
  49. }
  50. });
  51. } else {
  52. cb(null);
  53. }
  54. break;
  55. default:
  56. cb(null);
  57. break;
  58. }
  59. }
  60. function makeLoginPage() {
  61. const
  62. slackUri = config.login.slack.requestLoginUri
  63. +"?client_id=" +config.login.slack.clientId
  64. +"&scope=" +slackManager.getAuthScope().join(',')
  65. +"&redirect_uri=" +config.login.slack.redirect_uri,
  66. googleUri = config.login.google.requestLoginUri
  67. +"?client_id=" +config.login.google.clientId
  68. +"&scope=" +(["openid", "email", "profile"]).join("%20")
  69. +"&redirect_uri=" +config.login.google.redirect_uri
  70. +"&response_type=code"
  71. facebookUri = config.login.facebook.requestLoginUri
  72. +"?client_id=" +config.login.facebook.clientId
  73. +"&redirect_uri=" +config.login.facebook.redirect_uri;
  74. return templates.header("Mimou - login", ["login.css"])
  75. +`<div class="services"><h1>Login</h1>`
  76. +`<a href="${googleUri}"><img src="https://developers.google.com/identity/images/btn_google_signin_light_normal_web.png" alt="Sign in with Google" class="attempt-right"></a>`
  77. +`<a href="${facebookUri}"><img src="${config.rootUrl}btn_facebook_connect.png" alt="Log in with facebook"/></a>`
  78. +`<a href="${slackUri}"><img src="https://platform.slack-edge.com/img/sign_in_with_slack.png" srcset="https://platform.slack-edge.com/img/sign_in_with_slack.png 1x, https://platform.slack-edge.com/img/sign_in_with_slack@2x.png 2x" /></a>`
  79. +`</div>`
  80. +templates.footer();
  81. }
  82. module.exports.match = function(url) {
  83. if (url.urlParts.length === 1) {
  84. return true;
  85. } else if (url.urlParts.length === 2 && Object.keys(config.login).indexOf(url.urlParts[1]) >= 0) {
  86. return true;
  87. }
  88. return false;
  89. };
  90. module.exports.exec = function(req, res) {
  91. if (!req.urlObj.urlParts[1]) {
  92. res.end(makeLoginPage());
  93. } else {
  94. checkTokens(req.urlObj.urlParts[1], req, (account) => {
  95. if (account) {
  96. req.account = account;
  97. req.session = sessionManager.lazyForRequest(req);
  98. req.session.setSlackToken(req.reqT, token);
  99. res.writeHeader("302", {
  100. Location: config.rootUrl
  101. ,"Set-Cookie": "sessID="+req.session.sessId
  102. });
  103. sessionManager.saveSession(req.session);
  104. res.end();
  105. } else {
  106. res.end(makeLoginPage());
  107. }
  108. });
  109. }
  110. };
  111. module.exports.needLogin = false;