|
|
@@ -0,0 +1,79 @@
|
|
|
+import express, { NextFunction, Request, Router } from 'express';
|
|
|
+import { Types as MongooseTypes } from 'mongoose';
|
|
|
+
|
|
|
+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';
|
|
|
+import ErrorV3 from '../../models/vo/error-apiv3';
|
|
|
+
|
|
|
+import { ApiV3Response } from './interfaces/apiv3-response';
|
|
|
+
|
|
|
+const logger = loggerFactory('growi:routes:apiv3:transfer');
|
|
|
+
|
|
|
+/*
|
|
|
+ * Routes
|
|
|
+ */
|
|
|
+export default (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 adminRequiredIfInstalled = (req: Request, res: ApiV3Response, next: NextFunction) => {
|
|
|
+ if (!isInstalled) {
|
|
|
+ next();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ return adminRequired(req, res, next);
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
+ };
|
|
|
+
|
|
|
+ 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');
|
|
|
+
|
|
|
+ const uuid = new MongooseTypes.ObjectId().toString();
|
|
|
+
|
|
|
+ // Generate transfer key string
|
|
|
+ let transferKeyString: string;
|
|
|
+ try {
|
|
|
+ transferKeyString = TransferKey.generateKeyString(new URL(appSiteUrl), uuid);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(err);
|
|
|
+ return res.apiv3Err(new ErrorV3('appSiteUrl may be wrong', 'failed_to_generate_key_string'));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Save TransferKey document
|
|
|
+ try {
|
|
|
+ await TransferKeyModel.create({ _id: uuid, appSiteUrl, value: transferKeyString });
|
|
|
+ }
|
|
|
+ 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 });
|
|
|
+ });
|
|
|
+
|
|
|
+ return router;
|
|
|
+};
|