support interaction between module and apps for updating templates before rendering
[squeep-authentication-module] / lib / session-manager.js
index d1045ff570674c608585752b5f7a9e900490c221..2d3d96cd10706e07512ee3d908a6d849adb829b5 100644 (file)
@@ -73,12 +73,17 @@ class SessionManager {
     await this._sessionCookieSet(res, undefined, 0, path);
   }
 
+  /**
+   * @typedef {(pagePathLevel: Number, ctx: Object, htmlOptions: Object) => void} AppTemplateCallback
+   */
+
   /**
    * GET request for establishing admin session.
    * @param {http.ServerResponse} res
    * @param {Object} ctx
+   * @param {AppTemplateCallback} appCb
    */
-  async getAdminLogin(res, ctx) {
+  async getAdminLogin(res, ctx, appCb) {
     const _scope = _fileScope('getAdminLogin');
     this.logger.debug(_scope, 'called', { ctx });
 
@@ -94,7 +99,7 @@ class SessionManager {
       res.setHeader(Enum.Header.Location, redirect);
       res.end();
     } else {
-      res.end(Template.LoginHTML(ctx, this.options));
+      res.end(Template.LoginHTML(ctx, this.options, appCb));
     }
 
     this.logger.info(_scope, 'finished', { ctx });
@@ -105,20 +110,21 @@ class SessionManager {
    * POST request for taking form data to establish admin session.
    * @param {http.ServerResponse} res
    * @param {Object} ctx
+   * @param {AppTemplateCallback} appCb
    */
-  async postAdminLogin(res, ctx) {
+  async postAdminLogin(res, ctx, appCb) {
     const _scope = _fileScope('postAdminLogin');
     this.logger.debug(_scope, 'called', { ctx });
 
     ctx.errors = [];
 
     // Check if this was an OTP entry attempt.
-    if (await this._otpSubmission(res, ctx)) {
+    if (await this._otpSubmission(res, ctx, appCb)) {
       // OTP path was taken, either successful entry and session creation, or re-prompting for otp.
       return;
     }
 
-    if (await this._localUserAuth(res, ctx)) {
+    if (await this._localUserAuth(res, ctx, appCb)) {
       // Local auth path was taken.
       return;
     }
@@ -136,7 +142,7 @@ class SessionManager {
     }
 
     if (ctx.errors.length) {
-      res.end(Template.LoginHTML(ctx, this.options));
+      res.end(Template.LoginHTML(ctx, this.options, appCb));
       return;
     }
 
@@ -207,7 +213,7 @@ class SessionManager {
     }
 
     if (ctx.errors.length) {
-      res.end(Template.LoginHTML(ctx, this.options));
+      res.end(Template.LoginHTML(ctx, this.options, appCb));
       return;
     }
 
@@ -261,7 +267,7 @@ class SessionManager {
    * @param {String} ctx.parsedBody.otp
    * @returns {Promise<Boolean>} true if otp was handled, otherwise false indicates further login processing needed
    */
-  async _otpSubmission(res, ctx) {
+  async _otpSubmission(res, ctx, appCb) {
     const _scope = _fileScope('_otpSubmission');
 
     const {
@@ -287,7 +293,7 @@ class SessionManager {
     if (!otp) {
       // Nothing submitted, but valid state, just present otp form again, do not count as attempt.
       ctx.otpState = stateBox;
-      res.end(Template.OTPHTML(ctx, this.options));
+      res.end(Template.OTPHTML(ctx, this.options, appCb));
       this.logger.info(_scope, 'finished otp, nothing entered, request again', { ctx });
       return true;
     }
@@ -313,7 +319,7 @@ class SessionManager {
           ...state,
           attempt: state.attempt + 1,
         });
-        res.end(Template.OTPHTML(ctx, this.options));
+        res.end(Template.OTPHTML(ctx, this.options, appCb));
         this.logger.info(_scope, 'finished otp, invalid, request again', { ctx });
         return true;
 
@@ -335,7 +341,7 @@ class SessionManager {
    * @param {Object} ctx
    * @returns {Promise<Boolean>} true if handled, false if flow should continue
    */
-  async _localUserAuth(res, ctx) {
+  async _localUserAuth(res, ctx, appCb) {
     const _scope = _fileScope('_localUserAuth');
 
     // If Indiauth enabled and profile was submitted, defer to that.
@@ -355,7 +361,7 @@ class SessionManager {
     }
 
     if (ctx.errors.length) {
-      res.end(Template.LoginHTML(ctx, this.options));
+      res.end(Template.LoginHTML(ctx, this.options, appCb));
       return true;
     }
 
@@ -368,7 +374,7 @@ class SessionManager {
         attempt: 0,
         redirect,
       });
-      res.end(Template.OTPHTML(ctx, this.options));
+      res.end(Template.OTPHTML(ctx, this.options, appCb));
       this.logger.info(_scope, 'finished local, otp required', { ctx });
       return true;
     }
@@ -412,8 +418,9 @@ class SessionManager {
    * This currently only redeems a scope-less profile.
    * @param {http.ServerResponse} res
    * @param {Object} ctx
+   * @param {AppTemplateCallback} appCb
    */
-  async getAdminIA(res, ctx) {
+  async getAdminIA(res, ctx, appCb) {
     const _scope = _fileScope('getAdminIA');
     this.logger.debug(_scope, 'called', { ctx });
 
@@ -504,7 +511,7 @@ class SessionManager {
 
     if (ctx.errors.length) {
       await this._sessionCookieClear(res);
-      res.end(Template.IAHTML(ctx, this.options));
+      res.end(Template.IAHTML(ctx, this.options, appCb));
       return;
     }
 
@@ -528,8 +535,9 @@ class SessionManager {
    * Page for modifying credentials and OTP.
    * @param {http.ServerResponse} res
    * @param {Object} ctx
+   * @param {AppTemplateCallback} appCb
    */
-  async getAdminSettings(res, ctx) {
+  async getAdminSettings(res, ctx, appCb) {
     const _scope = _fileScope('getAdminSettings');
     this.logger.debug(_scope, 'called', { ctx });
 
@@ -547,7 +555,7 @@ class SessionManager {
       ctx.errors.push('An error was encountered.  Sorry that is not very helpful.');
     }
 
-    res.end(Template.SettingsHTML(ctx, this.options));
+    res.end(Template.SettingsHTML(ctx, this.options, appCb));
     this.logger.info(_scope, 'finished', { ctx });
   }
 
@@ -556,8 +564,10 @@ class SessionManager {
    * Page for modifying credentials and OTP.
    * @param {http.ServerResponse} res
    * @param {Object} ctx
+   * @param {Object[]=} appNavLinks
+   * @param {AppTemplateCallback} appCb
    */
-  async postAdminSettings(res, ctx) {
+  async postAdminSettings(res, ctx, appCb) {
     const _scope = _fileScope('postAdminSettings');
     this.logger.debug(_scope, 'called', { ctx });
 
@@ -597,7 +607,7 @@ class SessionManager {
       ctx.errors.push('An error was encountered.  Sorry that is not very helpful.');
     }
 
-    res.end(Template.SettingsHTML(ctx, this.options));
+    res.end(Template.SettingsHTML(ctx, this.options, appCb));
     this.logger.info(_scope, 'finished', { ctx });
   }
 
@@ -738,7 +748,6 @@ class SessionManager {
     }
   }
 
-
 }
 
 module.exports = SessionManager;