From 4a21d9ac1f3649ac5a20041a140c1a4f7eeca3b1 Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Sat, 23 Feb 2019 17:06:54 +0100
Subject: [PATCH] divide website

---
 requests.js | 35 +++++++++++++++++++++++++++++++++--
 schedule.js |  0
 website.js  | 29 +++--------------------------
 3 files changed, 36 insertions(+), 28 deletions(-)
 create mode 100644 schedule.js

diff --git a/requests.js b/requests.js
index 7034ef0..c2d8579 100644
--- a/requests.js
+++ b/requests.js
@@ -2,7 +2,7 @@
 const rp = require('request-promise');
 
 // Modules propres
-var { modifyChan } = require('./connection-db');
+var { modifyChan, getChanByState } = require('./connection-db');
 
 // Configurations
 const config = require('./config');
@@ -54,6 +54,37 @@ function getGroupById(token, id) {
     }).catch(err => { console.error(err) })
 }
 
+// Récupération d'un token
+function getFirstToken(code, state) {
+
+    const options = {
+        url: 'https://auth.viarezo.fr/oauth/token',
+        method: 'POST',
+        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
+        form: {
+            grant_type: 'authorization_code',
+            code: code,
+            redirect_uri: config.website.protocol + '://' + config.website.hostname + '/auth',
+            client_id: config.oauth2.clientid,
+            client_secret: config.oauth2.secretid
+        }
+    }
+
+    return Promise.all([
+        rp(options),
+        getChanByState(state)
+    ]).then(([body, chan]) => {
+        if (!chan) return
+        rep = JSON.parse(body);
+        chan.token = rep.access_token;
+        chan.refresh = rep.refresh_token;
+        chan.expiration = rep.expires_at;
+        chan.state = '';
+        return modifyChan(chan)
+    })
+}
+
+
 // Récupération d'un nouveau token
 function getNewToken(chan) {
 
@@ -79,4 +110,4 @@ function getNewToken(chan) {
     })
 };
 
-module.exports = { getBirthdays, sendRequest, searchGroups, getGroupById, getNewToken };
\ No newline at end of file
+module.exports = { getBirthdays, sendRequest, searchGroups, getGroupById, getFirstToken, getNewToken };
\ No newline at end of file
diff --git a/schedule.js b/schedule.js
new file mode 100644
index 0000000..e69de29
diff --git a/website.js b/website.js
index bc608cb..77cbe15 100644
--- a/website.js
+++ b/website.js
@@ -1,16 +1,16 @@
 // Modules extérieurs
 var app = require('express')();
-var rp = require('request-promise');
 
 // Modules propres
 var bot = require('./telegram');
 var { modifyChan, getChanByState } = require('./connection-db');
+var { getFirstToken } = require('./requests');
 
 // Configurations
 const config = require('./config');
 
 
-app.listen(80, '127.0.0.1', () => {
+app.listen(config.website.port, '127.0.0.1', () => {
     console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}/`);
 })
 
@@ -41,30 +41,7 @@ app.get('/auth', function (req, res) {
 
     if (!req.query.code || !req.query.state) return res.sendFile(`${__dirname}/auth.html`)
 
-    const options = {
-        url: 'https://auth.viarezo.fr/oauth/token',
-        method: 'POST',
-        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
-        form: {
-            grant_type: 'authorization_code',
-            code: req.query.code,
-            redirect_uri: config.website.protocol + '://' + config.website.hostname + '/auth',
-            client_id: config.oauth2.clientid,
-            client_secret: config.oauth2.secretid
-        }
-    }
-
-    return rp(options).then(body => {
-        return Promise.all([body, getChanByState(req.query.state)])
-    }).then(([body, chan]) => {
-        if (!chan) return
-        rep = JSON.parse(body);
-        chan.token = rep.access_token;
-        chan.refresh = rep.refresh_token;
-        chan.expiration = rep.expires_at;
-        chan.state = '';
-        return modifyChan(chan)
-    }).then(chan => {
+return getFirstToken(req.query.code, req.query.state).then(chan => {
         bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?`)
         res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`)
     })
-- 
GitLab