From 6c2164f8984945ce95c50c44c2d39f4415dc74ac Mon Sep 17 00:00:00 2001
From: ManifoldFR <wilson.jallet@gmail.com>
Date: Thu, 30 Aug 2018 19:30:04 +0200
Subject: [PATCH] fix flash in server.ts, convert admin.router

---
 README.md                                     |  1 +
 package-lock.json                             | 32 +++++++++++++++--
 package.json                                  |  3 ++
 .../{admin.router.js => admin.router.ts}      |  3 ++
 src/config_passport.js                        |  2 --
 src/server.ts                                 | 35 +++++++------------
 tsconfig.json                                 |  5 ++-
 webpack.config.js                             | 10 +++---
 8 files changed, 56 insertions(+), 35 deletions(-)
 rename src/admin_view/{admin.router.js => admin.router.ts} (98%)

diff --git a/README.md b/README.md
index 91ffe85..d41a124 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,7 @@ L'API de Sigma nécessite de se connecter au LDAP Frankiz, à la fois pour obten
 
 * La configuration LDAP de base se situe dans [ldap_config.json](ldap_config.json).
 * Elle est importée dans l'application depuis [src/ldap/config.js](src/ldap/config.js). Ce fichier écrase la config de base selon les options suivantes :
+
     | **Variable** | **Description** | **Défaut** (`ldap_config.json`) |
     | ------ | ------ | ----- |
     | LDAP_URI | URI vers le serveur LDAP. | <ldap://frankiz.eleves.polytechnique.fr:389> |
diff --git a/package-lock.json b/package-lock.json
index b1bd166..fcc43b4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -201,6 +201,23 @@
         "@types/node": "*"
       }
     },
+    "@types/connect-ensure-login": {
+      "version": "0.1.4",
+      "resolved": "https://registry.npmjs.org/@types/connect-ensure-login/-/connect-ensure-login-0.1.4.tgz",
+      "integrity": "sha512-MLXOS6+5MnhkB7F34xE66wX9MEEfrUJRfNM9Uv0vEre8kH13tILO9j2b7fI4xwyLXSEn9k3uIE8U46MQ1CWByw==",
+      "dev": true,
+      "requires": {
+        "@types/express": "*"
+      }
+    },
+    "@types/connect-flash": {
+      "version": "0.0.34",
+      "resolved": "https://registry.npmjs.org/@types/connect-flash/-/connect-flash-0.0.34.tgz",
+      "integrity": "sha512-QC93TwnTZ0sk//bfT81o7U4GOedbOZAcgvqi0v1vJqCESC8tqIVnhzB1CHiAUBUWFjoxG5JQF0TYaNa6DMb6Ig==",
+      "requires": {
+        "@types/express": "*"
+      }
+    },
     "@types/events": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz",
@@ -250,9 +267,10 @@
       "integrity": "sha512-DOzWZKUnmFYG0KUOs+9HEBju2QhBU6oM2zeluunQNt0vnJvnkHvtDNlQPZDkTrkC5pZrNx1TPqeL137zciXZMQ=="
     },
     "@types/passport": {
-      "version": "0.3.5",
-      "resolved": "https://registry.npmjs.org/@types/passport/-/passport-0.3.5.tgz",
-      "integrity": "sha512-J7mdY1nnhjdbkXT84S3WsyrTtDf2KqUJ9JW3Y9vxA5GuXlejIuvwHw9A2TdNklAqPG2Q0TWqlsA2a2GIeV1jYA==",
+      "version": "0.4.6",
+      "resolved": "https://registry.npmjs.org/@types/passport/-/passport-0.4.6.tgz",
+      "integrity": "sha512-P7TxrdpAze3nvHghYPeLlHkYcFDiIkRBbp7xYz2ehX9zmi1yr/qWQMTpXsMxN5w3ESJpMzn917inK4giASaDcQ==",
+      "dev": true,
       "requires": {
         "@types/express": "*"
       }
@@ -7722,6 +7740,14 @@
           "version": "7.0.56",
           "resolved": "http://registry.npmjs.org/@types/node/-/node-7.0.56.tgz",
           "integrity": "sha512-NgjN3xPyqbAXSIpznNAR5Cisx5uKqJWxcS9kefzSFEX/9J7O01/FHyfnvPI7SztBf9p6c8mqOn3olZWJx3ja6g=="
+        },
+        "@types/passport": {
+          "version": "0.3.5",
+          "resolved": "https://registry.npmjs.org/@types/passport/-/passport-0.3.5.tgz",
+          "integrity": "sha512-J7mdY1nnhjdbkXT84S3WsyrTtDf2KqUJ9JW3Y9vxA5GuXlejIuvwHw9A2TdNklAqPG2Q0TWqlsA2a2GIeV1jYA==",
+          "requires": {
+            "@types/express": "*"
+          }
         }
       }
     },
diff --git a/package.json b/package.json
index 28ce1ad..6d5a9fd 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,10 @@
     "url-loader": "^0.6.2"
   },
   "devDependencies": {
+    "@types/connect-ensure-login": "^0.1.4",
+    "@types/connect-flash": "0.0.34",
     "@types/node": "^10.9.3",
+    "@types/passport": "^0.4.6",
     "babel-eslint": "^8.2.6",
     "eslint": "^4.19.1",
     "eslint-config-standard": "^11.0.0",
diff --git a/src/admin_view/admin.router.js b/src/admin_view/admin.router.ts
similarity index 98%
rename from src/admin_view/admin.router.js
rename to src/admin_view/admin.router.ts
index a7b8b2a..f873d58 100644
--- a/src/admin_view/admin.router.js
+++ b/src/admin_view/admin.router.ts
@@ -10,10 +10,13 @@
 import { Router } from 'express';
 import knex from '../../db/knex_router';
 import passport from 'passport';
+import flash from 'connect-flash';
 import { ensureLoggedIn } from 'connect-ensure-login';
 
 const router = Router();
 
+router.use(flash());
+
 let port = process.env.PORT || 3000;
 
 
diff --git a/src/config_passport.js b/src/config_passport.js
index 07bc463..ca35df9 100644
--- a/src/config_passport.js
+++ b/src/config_passport.js
@@ -17,8 +17,6 @@
 */
 import passport from 'passport';
 import LdapStrategy from 'passport-ldapauth';
-import fs from 'fs';
-import path from 'path';
 import { ldapConfig } from './ldap/config';
 
 // specifies options for 'ldapauth' strategy, to customize the behaviour of subsequent passport.authenticate('ldapauth') calls
diff --git a/src/server.ts b/src/server.ts
index 42dc86a..2b194c2 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -45,11 +45,6 @@ app.use(bodyParser.urlencoded({ //parses bodies of media type "application/x-www
 }));
 app.use(cookieParser()); //parses Cookie header and populate req.cookies with an object keyed by the cookie names. was necessary for express-session before its v1.5.0. on peut probablement l'enlever desormais.
 
-// Config de passport pour l'authentification ldap. 
-// Ne fait que *configurer* passport pour la strategie 'ldap' (pas d'incidence sur la strategie 'session' normalement)
-import './config_passport';
-
-
 
 /**
  * @desc TRUCS DIVERS
@@ -69,20 +64,22 @@ app.use('/assets', express.static(path.resolve('./', 'assets')));
  */
 
 
-
-
-
 /**
  * @desc AUTHENTIFICATION POUR LES REQUETES POSSEDANT UN COOKIE ET PROVENANT D'UN UTILISATEUR DEJA AUTHENTIFIE
  * Remarque: introduit aussi les middlewares session et passport, qui sont aussi utiles pour l'authentification dans les autres cas.
  */
 
-// WTF??? why is sessionSecret in ldap_config.json? it has nothing to do with ldap. TODO
+/**
+ * WTF??? why is sessionSecret in ldap_config.json? it has nothing to do with ldap.
+ * @todo FIX
+ */
 
-// defines parameters for *session store*. (adds field req.session and do some magic stuff)
-// basically, searches for a session matching the received cookie and, if found, adds field req.blasomethingbla containing serialized object representing user (i.e. similar to what passport.serializeUser() could produce)
-// TODO: it is important to configure this right!!! please check out https://www.npmjs.com/package/express-session and make sure you understand the way session is stored. (en vrai c'est vraiment important...)
-app.use(session({
+/**
+/* defines parameters for *session store*. (adds field req.session and do some magic stuff)
+ * basically, searches for a session matching the received cookie and, if found, adds field req.blasomethingbla containing serialized object representing user (i.e. similar to what passport.serializeUser() could produce)
+ * @todo it is important to configure this right!!! please check out https://www.npmjs.com/package/express-session and make sure you understand the way session is stored. (en vrai c'est vraiment important...)
+ */
+ app.use(session({
     secret: ldapConfig.sessionSecret,
     resave: true,
     saveUninitialized: false,
@@ -106,13 +103,6 @@ app.use((req, res, next) => {
 });
 */
 
-/**
- * FIN AUTHENTIFICATION POUR LES REQUETES POSSEDANT UN COOKIE ET PROVENANT D'UN UTILISATEUR DEJA AUTHENTIFIE
- */
-
-
-
-
 
 /**
  * @desc AUTHENTIFICATION POUR LES REQUETES DE CONNEXION VIA LDAP VENANT DU FRONT
@@ -121,13 +111,12 @@ app.use((req, res, next) => {
  * Remarque: configure aussi passport pour l'authentification ldap, ce qui est aussi utile pour les requetes de connexion via ldap venant de adminview
  */
 
-const FRONTEND_SERVER_URL = 'change this to frontend server IP address';
-const FRONTEND_SERVER_URL_LOCAL = 'http://localhost:8888';
+const FRONTEND_SERVER_URL = process.env.FRONTEND_SERVER_URL || 'http://localhost:8888';
 
 // Options de configuration pour le _middleware_ `cors`.
 // CORS = Cross Origin Resource Sharing
 const corsOptions = {
-    origin: FRONTEND_SERVER_URL_LOCAL, // Configures the Access-Control-Allow-Origin CORS header. i.e. specifies that sigma-back wants to make resources accessible to this site (and this site only)
+    origin: FRONTEND_SERVER_URL, // Configures the Access-Control-Allow-Origin CORS header. i.e. specifies that sigma-back wants to make resources accessible to this site (and this site only)
     credentials: true // Configures the Access-Control-Allow-Credentials CORS header. i.e. allows cookies to be included on cross-origin requests
 };
 app.use(cors(corsOptions));
diff --git a/tsconfig.json b/tsconfig.json
index 7678ec4..6d8e026 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,5 +8,8 @@
         "outDir": "build",
         "resolveJsonModule": true,
     },
-    "include": ["src/**/*"]
+    "include": [
+        "src/**/*",
+        "db/**/*"
+    ]
 }
\ No newline at end of file
diff --git a/webpack.config.js b/webpack.config.js
index 8e59545..43d18e4 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -9,10 +9,10 @@ const config = {
     node: {
         __dirname: false
     },
-    
     //devtool: 'inline-source-map',
-    
-    externals: [nodeExternals()],
+    externals: [
+        nodeExternals()
+    ],
     
     module: {
         rules: [{
@@ -21,13 +21,11 @@ const config = {
         },{
             test: /\.ts$/,
             use: ['ts-loader'],
-            exclude: /node_modules/
         },{
             test: /\.css$/,
             use: ['style-loader', 'css-loader']
         },{
             test: /\.(png|jpg|ico)$/,
-            exclude: /node_modules/,
             loader: 'file-loader',
             options: {
                 limit: 10000
@@ -35,7 +33,7 @@ const config = {
         }]
     },
     resolve: {
-        extensions: ['.ts', '.js', '.json']
+        extensions: ['.ts', '.js']
     },
     plugins: [
         new CopyWebpackPlugin([{
-- 
GitLab