|
|
@@ -1,9 +1,7 @@
|
|
|
import express, { NextFunction, Request, Router } from 'express';
|
|
|
-import { Types as MongooseTypes } from 'mongoose';
|
|
|
+import { body } from 'express-validator';
|
|
|
|
|
|
-import TransferKeyModel from '~/server/models/transfer-key';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
-import { TransferKey } from '~/utils/vo/transfer-key';
|
|
|
|
|
|
import Crowi from '../../crowi';
|
|
|
import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
|
|
|
@@ -13,15 +11,23 @@ import { ApiV3Response } from './interfaces/apiv3-response';
|
|
|
|
|
|
const logger = loggerFactory('growi:routes:apiv3:transfer');
|
|
|
|
|
|
+const validator = {
|
|
|
+ transfer: [
|
|
|
+ body('transferKey').isString().withMessage('transferKey is required'),
|
|
|
+ ],
|
|
|
+};
|
|
|
+
|
|
|
/*
|
|
|
* Routes
|
|
|
*/
|
|
|
-export default (crowi: Crowi): Router => {
|
|
|
+module.exports = (crowi: Crowi): Router => {
|
|
|
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();
|
|
|
@@ -31,6 +37,7 @@ export default (crowi: Crowi): Router => {
|
|
|
return adminRequired(req, res, next);
|
|
|
};
|
|
|
|
|
|
+ // Middleware
|
|
|
const appSiteUrlRequiredIfNotInstalled = (req: Request, res: ApiV3Response, next: NextFunction) => {
|
|
|
if (!isInstalled && req.body.appSiteUrl != null) {
|
|
|
next();
|
|
|
@@ -45,18 +52,26 @@ export default (crowi: Crowi): Router => {
|
|
|
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();
|
|
|
|
|
|
- // TODO: Refactor process as TransferService methods
|
|
|
- router.post('/generate-key', accessTokenParser, adminRequiredIfInstalled, appSiteUrlRequiredIfNotInstalled, async(req: Request, res: ApiV3Response) => {
|
|
|
- const appSiteUrl = req.body.appSiteUrl ?? crowi.configManager?.getConfig('crowi', 'app:siteUrl');
|
|
|
+ router.post('/', validator.transfer, apiV3FormValidator, validateTransferKey, async(req: Request, res: ApiV3Response) => {
|
|
|
+ return;
|
|
|
+ });
|
|
|
|
|
|
- const uuid = new MongooseTypes.ObjectId().toString();
|
|
|
+ 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 transferKeyString: string;
|
|
|
+ let appSiteUrl: URL;
|
|
|
try {
|
|
|
- transferKeyString = TransferKey.generateKeyString(new URL(appSiteUrl), uuid);
|
|
|
+ appSiteUrl = new URL(strAppSiteUrl);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
|
@@ -64,8 +79,9 @@ export default (crowi: Crowi): Router => {
|
|
|
}
|
|
|
|
|
|
// Save TransferKey document
|
|
|
+ let transferKeyString: string;
|
|
|
try {
|
|
|
- await TransferKeyModel.create({ _id: uuid, appSiteUrl, value: transferKeyString });
|
|
|
+ transferKeyString = await crowi.g2gTransferService.createTransferKey(appSiteUrl);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
|
@@ -75,5 +91,10 @@ export default (crowi: Crowi): Router => {
|
|
|
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;
|
|
|
};
|