B Thibault 8 سال پیش
والد
کامیت
cc769777a0
11فایلهای تغییر یافته به همراه163 افزوده شده و 49 حذف شده
  1. 0 43
      srv/public/index.html
  2. 14 0
      srv/sessions.json
  3. 16 3
      srv/src/httpServ.js
  4. 3 1
      srv/src/session.js
  5. 2 2
      srv/src/slack.js
  6. 29 0
      srv/src/url.js
  7. 15 0
      srv/template/_model.js
  8. 21 0
      srv/template/_templates.js
  9. 41 0
      srv/template/index.js
  10. 22 0
      srv/template/login.js
  11. 0 0
      srv/template/register.js

+ 0 - 43
srv/public/index.html

@@ -1,43 +0,0 @@
-<html>
-    <head>
-        <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
-        <link href="style.css" rel="stylesheet"/>
-        <link href="emojione.sprites.css" rel="stylesheet"/>
-        <link href="favicon_err.png" type="image/png" rel="icon" id="linkFavicon" />
-    </head>
-    <body>
-        <aside class="slack-context" id="slackCtx">
-            <nav class="slack-context-menu"></nav>
-            <div class="slack-context-rooms" id="chatList">
-                <ul class="slack-context-channellist" id="chanList"></ul>
-                <div class="slack-context-imlist"></div>
-            </div>
-            <div class="slack-context-roominfo hidden"></div>
-        </aside>
-        <div class="slack-chat-container">
-            <div class="slack-chat-title" id="currentRoomTitle"></div>
-            <div class="slack-chat-content" id="chatWindow"></div>
-            <ul class="slack-chat-whoistyping" id="whoistyping"></ul>
-            <div class="slack-chat-control">
-                <div id="replyToContainer" class="replyto-container"></div>
-                <ul id="slashList" class="slack-command-list"></ul>
-                <form id="msgForm" class="msgform">
-                    <input type="text" id="msgInput" class="msgform-input" autocomplete="off" />
-                    <a id="emojiButton" class="button"/></a>
-                    <a id="attachFile" href="#!" class="button"><img src="paperclip.svg" alt="Send file" class="attach-file-icon" /></a>
-                    <input type="submit" class="button" value=">" />
-                </form>
-            </div>
-        </div>
-        <div class="hidden file-upload-container" id="fileUploadContainer"><form id="fileUploadForm" enctype="multipart/form-data">
-            <input type="file" id="fileUploadInput" />
-            <div id="fileUploadError" class="file-upload-error hidden"></div>
-            <a id="fileUploadCancel" class="button"/></a>
-            <input type="submit" class="button"/>
-        </form></div>
-        <div class="error" id="neterror"></div>
-        <script src="emojione.sprites.js"></script>
-        <script src="openpgp.min.js"></script>
-        <script src="slack.min.js"></script>
-    </body>
-</html>

+ 14 - 0
srv/sessions.json

@@ -0,0 +1,14 @@
+{
+  "index": [
+    {
+      "key": "1607ea15395e788e07dc10694cb25b52",
+      "val": {
+        "sessId": "1607ea15395e788e07dc10694cb25b52",
+        "slackToken": "xoxp-148001785602-147323841537-164283409479-40059e7d86535cbb48c695c5a588fb67",
+        "modifiedAt": 1497910040041
+      },
+      "life": 1497996450677,
+      "tags": []
+    }
+  ]
+}

+ 16 - 3
srv/src/httpServ.js

@@ -65,7 +65,7 @@ Server.prototype.onListen = function() {
 function redirectToSlackAuth(res) {
     res.writeHeader("302", {
         Location: "https://slack.com/oauth/authorize"
-        +"?client_id=" +config.clientId
+        +"?client_id=" +config.login.slack.clientId
         +"&scope=" +slackManager.getScope().join(",")
         +"&redirect_uri=" +config.rootUrl
     });
@@ -78,7 +78,12 @@ Server.prototype.onRequest = function(req, res) {
     req.session = sessionManager.forRequest(req);
     req.urlObj = new Url(req.url);
 
-    if (!req.session || !req.session.slackToken) {
+    if (req.urlObj.isTemplate() && req.urlObj.template.needLogin === false) {
+        var resp = req.urlObj.template.exec(req, res);
+        res.writeHeader(resp.status || 200);
+        res.end(resp.body);
+        return;
+    } else if (!req.session || !req.session.slackToken) {
         if (req.urlObj.queryTokens.code) {
             Slack.getOauthToken(req.urlObj.queryTokens.code, config.rootUrl, (token) => {
                 if (token) {
@@ -95,8 +100,16 @@ Server.prototype.onRequest = function(req, res) {
                 }
             });
         } else {
-            redirectToSlackAuth(res);
+            res.writeHeader("302", {
+                Location: "login"
+            });
+            res.end();
         }
+    } else if (req.urlObj.isTemplate()) {
+        var resp = req.urlObj.template.exec(req, res);
+        res.writeHeader(resp.status || 200);
+        res.end(resp.body);
+        return;
     } else if (req.urlObj.isPublic()) {
         if (!config.isDebug)
             res.setHeader('Cache-Control', 'private, max-age=' +15 * 60);

+ 3 - 1
srv/src/session.js

@@ -2,7 +2,9 @@ const crypto = require('crypto')
     ,Cache = require('node-file-cache');
 
 function SessionManager() {
-    this.sessions = Cache.create();
+    this.sessions = Cache.create({
+        file: "sessions.json"
+    });
 }
 
 SessionManager.defaultTtl = 60 * 60 * 24; // keep sessions 24h

+ 2 - 2
srv/src/slack.js

@@ -315,8 +315,8 @@ Slack.prototype.close = function() {
 
 Slack.getOauthToken = function(code, redirectUri, cb) {
     httpsRequest(SLACK_ENDPOINT+GETAPI.oauth
-        +"?client_id=" +config.clientId
-        +"&client_secret=" +config.clientSecret
+        +"?client_id=" +config.services.Slack.clientId
+        +"&client_secret=" +config.services.Slack.clientSecret
         +"&redirect_uri=" +redirectUri
         +"&code=" +code,
     (status, resp) => {

+ 29 - 0
srv/src/url.js

@@ -30,6 +30,13 @@ function Url(url) {
         }
     }
     this.serve = null;
+    this.template = null;
+
+    if (!tryLoadTemplate.apply(this))
+        tryLoadPublic.apply(this);
+}
+
+function tryLoadPublic() {
     try{
         if (!this.urlParts.length) {
             this.serve = {
@@ -42,15 +49,37 @@ function Url(url) {
                 ,stat: fs.statSync("public/" +this.urlParts[0])
             };
         }
+        return true;
     } catch (e) {
         // Not a public/ file
     }
+    return false;
+}
+
+function tryLoadTemplate(Url) {
+    try{
+        if (!this.urlParts.length) {
+            this.template = require('../template/index.js');
+        } else if (this.urlParts.length == 1 && this.urlParts[0][0] !== '_') {
+            this.template = require('../template/' +this.urlParts[0] +'.js');
+        }
+        return true;
+    } catch (e) {
+        if (e.code !== "MODULE_NOT_FOUND")
+            throw e;
+        // Not a template
+    }
+    return false;
 }
 
 Url.prototype.isPublic = function() {
     return this.serve !== null;
 };
 
+Url.prototype.isTemplate = function() {
+    return !!this.template;
+};
+
 Url.prototype.getReadStream = function() {
     return fs.createReadStream(this.serve.filename);
 };

+ 15 - 0
srv/template/_model.js

@@ -0,0 +1,15 @@
+
+const templates = require('./_templates.js');
+
+module.exports.exec = function() {
+    return {
+        status: 200
+        ,body:
+            templates.header(["login.css"])
+            +``
+            +templates.footer(["emojione.sprites.js", "openpgp.min.js", "slack.min.js"])
+    };
+};
+
+module.exports.needLogin = true;
+

+ 21 - 0
srv/template/_templates.js

@@ -0,0 +1,21 @@
+
+module.exports = {
+    header: function(cssFiles, moreHeader) {
+        var res = `<html><head><link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />`;
+        (cssFiles || []).forEach(function(i) {
+            res += '<link href="' +i +'" rel="stylesheet"/>';
+        });
+        (moreHeader || []).forEach(function(i) {
+            res += '<link href="favicon_err.png" type="image/png" rel="icon" id="linkFavicon" />';
+        });
+        return res +`</head><body>`;
+    }
+    ,footer: function(jsFiles) {
+        var res = "";
+        (jsFiles || []).forEach(function(i) {
+            res += '<script src="' +i +'"></script>';
+        });
+        return res +`</body></html>`;
+    }
+};
+

+ 41 - 0
srv/template/index.js

@@ -0,0 +1,41 @@
+
+const templates = require('./_templates.js');
+
+module.exports.exec = function() {
+    return {
+        status: 200
+        ,body: templates.header(["style.css", "emojione.sprites.css"], ['<link href="favicon_err.png" type="image/png" rel="icon" id="linkFavicon" />'])
+        +`<aside class="slack-context" id="slackCtx">
+                    <nav class="slack-context-menu"></nav>
+                    <div class="slack-context-rooms" id="chatList">
+                        <ul class="slack-context-channellist" id="chanList"></ul>
+                        <div class="slack-context-imlist"></div>
+                    </div>
+                    <div class="slack-context-roominfo hidden"></div>
+                </aside>
+                <div class="slack-chat-container">
+                    <div class="slack-chat-title" id="currentRoomTitle"></div>
+                    <div class="slack-chat-content" id="chatWindow"></div>
+                    <ul class="slack-chat-whoistyping" id="whoistyping"></ul>
+                    <div class="slack-chat-control">
+                        <div id="replyToContainer" class="replyto-container"></div>
+                        <ul id="slashList" class="slack-command-list"></ul>
+                        <form id="msgForm" class="msgform">
+                            <input type="text" id="msgInput" class="msgform-input" autocomplete="off" />
+                            <a id="emojiButton" class="button"/></a>
+                            <a id="attachFile" href="#!" class="button"><img src="paperclip.svg" alt="Send file" class="attach-file-icon" /></a>
+                            <input type="submit" class="button" value=">" />
+                        </form>
+                    </div>
+                </div>
+                <div class="hidden file-upload-container" id="fileUploadContainer"><form id="fileUploadForm" enctype="multipart/form-data">
+                    <input type="file" id="fileUploadInput" />
+                    <div id="fileUploadError" class="file-upload-error hidden"></div>
+                    <a id="fileUploadCancel" class="button"/></a>
+                    <input type="submit" class="button"/>
+                </form></div>
+                <div class="error" id="neterror"></div>`
+            +templates.footer(["emojione.sprites.js", "openpgp.min.js", "slack.min.js"])
+    };
+};
+

+ 22 - 0
srv/template/login.js

@@ -0,0 +1,22 @@
+
+const config = require("../config.js")
+    ,slackManager = require("../src/slackManager.js").SlackManager
+    ,templates = require('./_templates.js');
+
+module.exports.exec = function() {
+    var slackUri = "https://slack.com/oauth/authorize"
+        +"?client_id=" +config.login.slack.clientId
+        +"&scope=identity.basic"
+        +"&redirect_uri=" +config.rootUrl;
+
+    return {
+        status: 200
+        ,body:
+            templates.header(["login.css"])
+            +`<a href="${slackUri}">Slack login</a>`
+            +templates.footer()
+    };
+};
+
+module.exports.needLogin = false;
+

+ 0 - 0
srv/template/register.js