From d6c19258e3d1cb350309c652d872bfc93b768b61 Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Fri, 15 Feb 2019 00:04:23 +0100
Subject: [PATCH] ended basic website

---
 .gitignore                        |  1 +
 auth.html                         | 38 ++++++++++++++++++++++++
 index.html                        |  2 +-
 index.js                          | 26 ++--------------
 node_modules/request/package.json | 16 +++++-----
 package.json                      |  3 +-
 telegram.js                       |  7 +++--
 website.js                        | 49 +++++++++++++++++++++++++++++++
 8 files changed, 107 insertions(+), 35 deletions(-)
 create mode 100644 auth.html
 create mode 100644 website.js

diff --git a/.gitignore b/.gitignore
index 1bf4259..acd849f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 config.js
+mongoose.js
\ No newline at end of file
diff --git a/auth.html b/auth.html
new file mode 100644
index 0000000..fc69128
--- /dev/null
+++ b/auth.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html>
+<title>Happy Botday !</title>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
+<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
+<style>
+    body,
+    h1 {
+        font-family: "Raleway", sans-serif
+    }
+
+    body,
+    html {
+        height: 100%
+    }
+
+    .bgimg {
+        background-image: url('/background.jpg');
+        min-height: 100%;
+        background-position: center;
+        background-size: cover;
+    }
+</style>
+
+<body>
+
+    <div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
+        <div class="w3-display-middle">
+            <p class="w3-large w3-center">Connecté ! Tu peux fermer cette fenêtre.
+            </p>
+        </div>
+    </div>
+
+</body>
+
+</html>
\ No newline at end of file
diff --git a/index.html b/index.html
index 326714c..087cba5 100644
--- a/index.html
+++ b/index.html
@@ -30,7 +30,7 @@
         <div class="w3-display-middle">
             <h1 class="w3-jumbo w3-animate-top">HAPPY BOTDAY!</h1>
             <hr class="w3-border-grey" style="margin:auto;width:40%">
-            <p class="w3-large w3-center">Go to <a href="https://t.me/happyCSbot">t.me/happyCSbot</a> for configuration.
+            <p class="w3-large w3-center">Va sur <a href="https://t.me/happyCSbot">t.me/happyCSbot</a> pour la configuration.
             </p>
         </div>
     </div>
diff --git a/index.js b/index.js
index 26127d3..b6bd6c3 100644
--- a/index.js
+++ b/index.js
@@ -1,24 +1,2 @@
-require('./telegram');
-const config = require('./config');
-
-var app = require('express')();
-
-app.listen(80, config.website.hostname, () => {
-    console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}`);
-})
-
-app.get('/', function (req, res) {
-    res.sendFile(__dirname + '/index.html')
-})
-
-app.get('/background.jpg', function (req, res) {
-    res.sendFile(__dirname + '/background.jpg')
-})
-
-app.get('/auth', function (req, res) {
-    res.sendFile(__dirname + '/auth.html');
-})
-
-app.get('/:id', function (req, res) {
-    res.send(req.params.id)
-})
\ No newline at end of file
+const bot = require('./telegram');
+const app = require('./website');
\ No newline at end of file
diff --git a/node_modules/request/package.json b/node_modules/request/package.json
index bc4d268..abc9f07 100644
--- a/node_modules/request/package.json
+++ b/node_modules/request/package.json
@@ -1,27 +1,29 @@
 {
-  "_from": "request@^2.83.0",
+  "_from": "request",
   "_id": "request@2.88.0",
   "_inBundle": false,
   "_integrity": "sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==",
   "_location": "/request",
   "_phantomChildren": {},
   "_requested": {
-    "type": "range",
+    "type": "tag",
     "registry": true,
-    "raw": "request@^2.83.0",
+    "raw": "request",
     "name": "request",
     "escapedName": "request",
-    "rawSpec": "^2.83.0",
+    "rawSpec": "",
     "saveSpec": null,
-    "fetchSpec": "^2.83.0"
+    "fetchSpec": "latest"
   },
   "_requiredBy": [
+    "#USER",
+    "/",
     "/node-telegram-bot-api"
   ],
   "_resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
   "_shasum": "9c2fca4f7d35b592efe57c7f0a55e81052124fef",
-  "_spec": "request@^2.83.0",
-  "_where": "/home/capsule_man/developpement/happy-botday/node_modules/node-telegram-bot-api",
+  "_spec": "request",
+  "_where": "/home/capsule_man/developpement/happy-botday",
   "author": {
     "name": "Mikeal Rogers",
     "email": "mikeal.rogers@gmail.com"
diff --git a/package.json b/package.json
index ee4563d..a11ae9c 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
   "license": "ISC",
   "dependencies": {
     "express": "^4.16.4",
-    "node-telegram-bot-api": "^0.30.0"
+    "node-telegram-bot-api": "^0.30.0",
+    "request": "^2.88.0"
   }
 }
diff --git a/telegram.js b/telegram.js
index db3c402..da69397 100644
--- a/telegram.js
+++ b/telegram.js
@@ -1,3 +1,4 @@
+process.env["NTBA_FIX_319"] = 1;
 const TelegramBot = require('node-telegram-bot-api');
 
 const config = require('./config');
@@ -13,6 +14,8 @@ bot.onText(/\/start/, (msg, _) => {
 
 bot.onText(/\/connect/, (msg, _) => {
     const chatId = msg.chat.id;
-    const resp = `Pour vous identifier, connectez-vous via l\'OAuth2 de ViaRézo depuis ce lien : ${config.website.protocol}://${config.website.hostname}/${chatId}`;
+    const resp = `Pour vous identifier, connectez-vous via l\'OAuth2 de ViaRézo depuis ce lien : ${config.website.protocol}://${config.website.hostname}/?chatid=${chatId}`;
     bot.sendMessage(chatId, resp);
-});
\ No newline at end of file
+});
+
+module.exports = bot;
\ No newline at end of file
diff --git a/website.js b/website.js
new file mode 100644
index 0000000..efe2fe1
--- /dev/null
+++ b/website.js
@@ -0,0 +1,49 @@
+const config = require('./config');
+
+const app = require('express')();
+const request = require('request');
+const bot = require('./telegram');
+
+app.listen(80, config.website.hostname, () => {
+    console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}/`);
+})
+
+app.get('/', function (req, res) {
+    if (!req.query.chatid) { return res.sendFile(`${__dirname}/index.html`) };
+
+    const state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
+    const redirectURI = config.website.protocol + '://' + config.website.hostname + '/auth';
+    const url = `https://auth.viarezo.fr/oauth/authorize/?redirect_uri=${redirectURI}&client_id=${config.oauth2.clientid}&response_type=code&state=${state}&scope=${config.oauth2.scope}`;
+    return res.redirect(301, url);
+})
+
+app.get('/background.jpg', function (req, res) {
+    res.sendFile(`${__dirname}/background.jpg`)
+})
+
+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
+        }
+    }
+
+    request(options, (err, res, body) => {
+        console.log(res.statusCode);
+        if (!err && res.statusCode == 200) {
+            console.log(body)
+        }
+    })
+    return res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`);
+})
+
+module.exports = app;
\ No newline at end of file
-- 
GitLab