You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
// admin/core-node.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const DTP_COMPONENT_NAME = 'admin:core-node';
|
|
|
|
const express = require('express');
|
|
// const multer = require('multer');
|
|
|
|
const { /*SiteError,*/ SiteController } = require('../../../lib/site-lib');
|
|
|
|
class CoreNodeController extends SiteController {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, DTP_COMPONENT_NAME);
|
|
}
|
|
|
|
async start ( ) {
|
|
// const upload = multer({ dest: `/tmp/${this.dtp.config.site.domainKey}/upload` });
|
|
|
|
const router = express.Router();
|
|
router.use(async (req, res, next) => {
|
|
res.locals.currentView = 'admin';
|
|
res.locals.adminView = 'core-node';
|
|
return next();
|
|
});
|
|
|
|
router.post('/connect', this.postCoreNodeConnect.bind(this));
|
|
router.get('/connect', this.getCoreNodeConnectForm.bind(this));
|
|
|
|
router.get('/', this.getIndex.bind(this));
|
|
|
|
return router;
|
|
}
|
|
|
|
async postCoreNodeConnect (req, res, next) {
|
|
const { coreNode: coreNodeService } = this.dtp.services;
|
|
|
|
try {
|
|
res.locals.core = await coreNodeService.resolveCore(req.body.host);
|
|
this.log.info('sending Core connection request', { core: res.locals.core });
|
|
} catch (error) {
|
|
this.log.error('failed to resolve Core', { host: req.body.host, error });
|
|
return next(error);
|
|
}
|
|
|
|
const CORE_SCHEME = process.env.DTP_CORE_AUTH_SCHEME || 'https';
|
|
res.locals.siteConfig = Object.assign({ }, this.dtp.config.site);
|
|
|
|
if (req.body.callbackUri) {
|
|
res.locals.siteConfig.coreAuth.callbackUrl = `${CORE_SCHEME}://${this.dtp.config.site.domain}${req.body.callbackUri}`;
|
|
this.log.info('registering with custom callback URL', { callbackUrl: res.locals.siteConfig.coreAuth.callbackUrl });
|
|
} else {
|
|
const callbackUri = `/auth/core/${res.locals.core._id}/callback`;
|
|
res.locals.siteConfig.coreAuth.callbackUrl = `${CORE_SCHEME}://${this.dtp.config.site.domain}${callbackUri}`;
|
|
this.log.info('registering with standard callback URL', { callbackUrl: res.locals.siteConfig.coreAuth.callbackUrl });
|
|
}
|
|
|
|
try {
|
|
res.locals.txConnect = await coreNodeService.sendRequest(res.locals.core, {
|
|
method: 'POST',
|
|
url: '/core/connect/node',
|
|
tokenized: true,
|
|
body: {
|
|
version: this.dtp.pkg.version,
|
|
site: res.locals.siteConfig,
|
|
},
|
|
});
|
|
|
|
const { request, response } = res.locals.txConnect;
|
|
this.log.info('connect tranaction', { request, response });
|
|
if (response.success && response.connect) {
|
|
await coreNodeService.setCoreOAuth2Credentials(res.locals.core, response.connect);
|
|
}
|
|
} catch (error) {
|
|
this.log.error('failed to create Core Node connection request', { error });
|
|
return next(error);
|
|
}
|
|
|
|
res.render('admin/core-node/connect-result');
|
|
}
|
|
|
|
async getCoreNodeConnectForm (req, res) {
|
|
res.render('admin/core-node/connect');
|
|
}
|
|
|
|
async getIndex (req, res, next) {
|
|
const { coreNode: coreNodeService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
|
res.locals.coreNodes = await coreNodeService.getConnectedCores(res.locals.pagination);
|
|
res.render('admin/core-node/index');
|
|
} catch (error) {
|
|
this.log.error('failed to render Core Node home', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = async (dtp) => {
|
|
let controller = new CoreNodeController(dtp);
|
|
return controller;
|
|
};
|