update depedencies, changes to support updated authentication-module
[squeep-indie-auther] / src / service.js
index 2a159a9181c7b4d327282035c014a4e87a194e55..dc131c35c76a5950f67fa37371a2074e8a12b0d7 100644 (file)
@@ -11,8 +11,9 @@ const common = require('./common');
 const Manager = require('./manager');
 const { Authenticator, SessionManager } = require('@squeep/authentication-module');
 const { ResourceAuthenticator } = require('@squeep/resource-authentication-module');
-const { TemplateHelper: { initContext } } = require('@squeep/html-template-helper');
+const { initContext, navLinks } = require('./template/template-helper');
 const Enum = require('./enum');
+const { ResponseError } = require('./errors');
 
 const _fileScope = common.fileScope(__filename);
 
@@ -22,6 +23,7 @@ class Service extends Dingus {
       ...options.dingus,
       ignoreTrailingSlash: false,
     });
+    this.options = options;
     this.asyncLocalStorage = asyncLocalStorage;
     this.staticPath = path.normalize(path.join(__dirname, '..', 'static'));
     this.manager = new Manager(logger, db, options);
@@ -55,6 +57,9 @@ class Service extends Dingus {
     // Information page about service
     this.on(['GET'], '/', this.handlerGetRoot.bind(this));
 
+    // Temmporary to see what rando payload someone is sending us unsolicited
+    this.on(['POST'], '/', this.handlerWhaGwan.bind(this));
+
     // Give load-balancers something to check
     this.on(['GET'], route('healthcheck'), this.handlerGetHealthcheck.bind(this));
 
@@ -79,6 +84,8 @@ class Service extends Dingus {
     this.on(['GET'], '/admin/login', this.handlerGetAdminLogin.bind(this));
     this.on(['POST'], '/admin/login', this.handlerPostAdminLogin.bind(this));
     this.on(['GET'], '/admin/logout', this.handlerGetAdminLogout.bind(this));
+    this.on(['GET'], '/admin/settings', this.handlerGetAdminSettings.bind(this));
+    this.on(['POST'], '/admin/settings', this.handlerPostAdminSettings.bind(this));
 
     // Page for upkeep info et cetera
     this.on(['GET'], '/admin/maintenance', this.handlerGetAdminMaintenance.bind(this));
@@ -101,12 +108,19 @@ class Service extends Dingus {
    * @param {Object} ctx
    */
   async preHandler(req, res, ctx) {
+    const _scope = _fileScope('preHandler');
+
     await super.preHandler(req, res, ctx);
     ctx.url = req.url; // Persist this for logout redirect
 
     const logObject = this.asyncLocalStorage.getStore();
-    logObject.requestId = ctx.requestId;
-    delete ctx.requestId;
+    // istanbul ignore else
+    if (logObject) { // Debugging in vscode seems to kill ALS, work around
+      logObject.requestId = ctx.requestId;
+      delete ctx.requestId;
+    } else {
+      this.logger.debug(_scope, 'no async local store');
+    }
   }
 
 
@@ -123,7 +137,7 @@ class Service extends Dingus {
 
     await this.authenticator.sessionOptionalLocal(req, res, ctx);
 
-    await this.sessionManager.getAdminLogin(res, ctx);
+    await this.sessionManager.getAdminLogin(res, ctx, navLinks);
   }
 
 
@@ -144,7 +158,46 @@ class Service extends Dingus {
 
     await this.ingestBody(req, res, ctx);
 
-    await this.sessionManager.postAdminLogin(res, ctx);
+    await this.sessionManager.postAdminLogin(res, ctx, navLinks);
+  }
+
+
+  /**
+   * @param {http.IncomingMessage} req
+   * @param {http.ServerResponse} res
+   * @param {Object} ctx
+   */
+  async handlerGetAdminSettings(req, res, ctx) {
+    const _scope = _fileScope('handlerGetAdminSettings');
+    this.logger.debug(_scope, 'called', { req, ctx });
+
+    initContext(ctx);
+
+    this.setResponseType(this.responseTypes, req, res, ctx);
+
+    if (await this.authenticator.sessionRequiredLocal(req, res, ctx)) {
+      await this.sessionManager.getAdminSettings(res, ctx, navLinks);
+    }
+  }
+
+
+  /**
+   * @param {http.IncomingMessage} req
+   * @param {http.ServerResponse} res
+   * @param {Object} ctx
+   */
+  async handlerPostAdminSettings(req, res, ctx) {
+    const _scope = _fileScope('handlerPostAdminSettings');
+    this.logger.debug(_scope, 'called', { req, ctx });
+
+    initContext(ctx);
+
+    this.setResponseType(this.responseTypes, req, res, ctx);
+
+    if (await this.authenticator.sessionRequiredLocal(req, res, ctx)) {
+      await this.ingestBody(req, res, ctx);
+      await this.sessionManager.postAdminSettings(res, ctx, navLinks);
+    }
   }
 
 
@@ -466,6 +519,15 @@ class Service extends Dingus {
   }
 
 
+  /**
+   * Temporary to see what an unsolicited payload contains.
+   */
+  async handlerWhaGwan(req, res, ctx) {
+    this.setResponseType(this.responseTypes, req, res, ctx);
+    await this.ingestBody(req, res, ctx);
+    throw new ResponseError(Enum.ErrorResponse.MethodNotAllowed);
+  }
+
   /**
    * @param {http.IncomingMessage} req 
    * @param {http.ServerResponse} res