|
|
@@ -0,0 +1,105 @@
|
|
|
+import express, { NextFunction, Request, Router } from 'express';
|
|
|
+import { body } from 'express-validator';
|
|
|
+
|
|
|
+import loggerFactory from '~/utils/logger';
|
|
|
+
|
|
|
+import Crowi from '../../crowi';
|
|
|
+import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
|
|
|
+import ErrorV3 from '../../models/vo/error-apiv3';
|
|
|
+
|
|
|
+import { ApiV3Response } from './interfaces/apiv3-response';
|
|
|
+
|
|
|
+const logger = loggerFactory('growi:routes:apiv3:transfer');
|
|
|
+
|
|
|
+const validator = {
|
|
|
+ transfer: [
|
|
|
+ body('transferKey').isString().withMessage('transferKey is required'),
|
|
|
+ ],
|
|
|
+};
|
|
|
+
|
|
|
+/*
|
|
|
+ * Routes
|
|
|
+ */
|
|
|
+module.exports = (crowi: Crowi): Router => {
|
|
|
+ const { g2gTransferService } = crowi;
|
|
|
+ if (g2gTransferService == null) {
|
|
|
+ throw Error('G2GTransferService is not set');
|
|
|
+ }
|
|
|
+
|
|
|
+ const isInstalled = crowi.configManager?.getConfig('crowi', 'app:installed');
|
|
|
+
|
|
|
+ const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
|
|
|
+ const adminRequired = require('../../middlewares/admin-required')(crowi);
|
|
|
+ const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
|
|
|
+
|
|
|
+ // Middleware
|
|
|
+ const adminRequiredIfInstalled = (req: Request, res: ApiV3Response, next: NextFunction) => {
|
|
|
+ if (!isInstalled) {
|
|
|
+ next();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ return adminRequired(req, res, next);
|
|
|
+ };
|
|
|
+
|
|
|
+ // Middleware
|
|
|
+ const appSiteUrlRequiredIfNotInstalled = (req: Request, res: ApiV3Response, next: NextFunction) => {
|
|
|
+ if (!isInstalled && req.body.appSiteUrl != null) {
|
|
|
+ next();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (crowi.configManager?.getConfig('crowi', 'app:siteUrl') != null || req.body.appSiteUrl != null) {
|
|
|
+ next();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.apiv3Err(new ErrorV3('Body param "appSiteUrl" is required when GROWI is NOT installed yet'), 400);
|
|
|
+ };
|
|
|
+
|
|
|
+ // Middleware to check if key is valid or not
|
|
|
+ const validateTransferKey = async(req: Request, res: ApiV3Response, next: NextFunction) => {
|
|
|
+ const { transferKey } = req.body;
|
|
|
+ // TODO: Check key
|
|
|
+ next();
|
|
|
+ };
|
|
|
+
|
|
|
+ const router = express.Router();
|
|
|
+
|
|
|
+ router.post('/', validator.transfer, apiV3FormValidator, validateTransferKey, async(req: Request, res: ApiV3Response) => {
|
|
|
+ return;
|
|
|
+ });
|
|
|
+
|
|
|
+ router.post('/generate-key', /* accessTokenParser, adminRequiredIfInstalled, appSiteUrlRequiredIfNotInstalled, */ async(req: Request, res: ApiV3Response) => {
|
|
|
+ const strAppSiteUrl = req.body.appSiteUrl ?? crowi.configManager?.getConfig('crowi', 'app:siteUrl');
|
|
|
+
|
|
|
+ // Generate transfer key string
|
|
|
+ let appSiteUrl: URL;
|
|
|
+ try {
|
|
|
+ appSiteUrl = new URL(strAppSiteUrl);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(err);
|
|
|
+ return res.apiv3Err(new ErrorV3('appSiteUrl may be wrong', 'failed_to_generate_key_string'));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Save TransferKey document
|
|
|
+ let transferKeyString: string;
|
|
|
+ try {
|
|
|
+ transferKeyString = await g2gTransferService.createTransferKey(appSiteUrl);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(err);
|
|
|
+ return res.apiv3Err(new ErrorV3('Error occurred while generating transfer key.', 'failed_to_generate_key'));
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.apiv3({ transferKey: transferKeyString });
|
|
|
+ });
|
|
|
+
|
|
|
+ // eslint-disable-next-line max-len
|
|
|
+ router.post('/transfer', accessTokenParser, loginRequiredStrictly, adminRequired, validator.transfer, apiV3FormValidator, async(req: Request, res: ApiV3Response) => {
|
|
|
+ return;
|
|
|
+ });
|
|
|
+
|
|
|
+ return router;
|
|
|
+};
|