diff --git a/requests/Switcher API.postman_collection.json b/requests/Switcher API.postman_collection.json index c5d6cc2..7f1e0c2 100644 --- a/requests/Switcher API.postman_collection.json +++ b/requests/Switcher API.postman_collection.json @@ -3751,6 +3751,48 @@ }, "response": [] }, + { + "name": "Config - Relay - Generate Verification Code", + "request": { + "method": "PATCH", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/config/relay/verificationCode/:config", + "host": [ + "{{url}}" + ], + "path": [ + "config", + "relay", + "verificationCode", + ":config" + ], + "variable": [ + { + "key": "config", + "value": "5e0eceb66f4f994eac9007b2" + } + ] + } + }, + "response": [] + }, { "name": "Config - Read Relay Specs", "request": { diff --git a/src/api-docs/paths/path-config.js b/src/api-docs/paths/path-config.js index 0fe630a..7a22393 100644 --- a/src/api-docs/paths/path-config.js +++ b/src/api-docs/paths/path-config.js @@ -274,6 +274,34 @@ export default { } } }, + '/config/relay/verificationCode/{id}': { + patch: { + tags: ['Config'], + description: 'Config Relay generates verification code', + security: [{ bearerAuth: [] }], + parameters: [ + pathParameter('id', 'Config ID', true) + ], + responses: { + '200': { + description: 'Config Relay verification code generated', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + code: { + type: 'string', + description: 'Verification code' + } + } + } + } + } + } + } + } + }, '/config/spec/relay': { get: { tags: ['Config'], diff --git a/src/api-docs/schemas/config.js b/src/api-docs/schemas/config.js index e8229d4..4e83f7f 100644 --- a/src/api-docs/schemas/config.js +++ b/src/api-docs/schemas/config.js @@ -42,6 +42,14 @@ export const relay = { additionalProperties: { type: 'string' } + }, + verification_code: { + type: 'string', + description: 'Generated string used to verify Relay endpoint ownership' + }, + verified: { + type: 'boolean', + description: 'Valid when true (default: false)' } } }; diff --git a/src/routers/config.js b/src/routers/config.js index cfd1ea5..f1010a2 100644 --- a/src/routers/config.js +++ b/src/routers/config.js @@ -208,6 +208,17 @@ router.patch('/config/removeRelay/:id/:env', auth, [ } }); +router.patch('/config/relay/verificationCode/:id', auth, [ + check('id').isMongoId() +], validate, async (req, res) => { + try { + const config = await Services.getRelayVerificationCode(req.params.id, req.admin); + res.send({ code: config.relay.verification_code }); + } catch (e) { + responseException(res, e, 500); + } +}); + router.get('/config/spec/relay', auth, (_req, res) => { res.send(relayOptions()); }); diff --git a/src/services/config.js b/src/services/config.js index a6c98da..e97e1c0 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -1,4 +1,5 @@ import mongoose from 'mongoose'; +import { randomBytes } from 'crypto'; import { response } from './common'; import { Config } from '../models/config'; import { formatInput, verifyOwnership, checkEnvironmentStatusRemoval } from '../helpers'; @@ -258,4 +259,18 @@ export async function removeRelay(id, env, admin) { } return config; +} + +export async function getRelayVerificationCode(id, admin) { + let config = await getConfigById(id); + config = await verifyOwnership(admin, config, config.domain, ActionTypes.UPDATE, RouterTypes.CONFIG); + + const buffer = randomBytes(32); + const code = Buffer.from(buffer).toString('base64'); + + config.updatedBy = admin.email; + config.relay.verification_code = code; + config.relay.verified = false; + + return config.save(); } \ No newline at end of file diff --git a/tests/config.test.js b/tests/config.test.js index cecc6fd..ce84591 100644 --- a/tests/config.test.js +++ b/tests/config.test.js @@ -978,6 +978,22 @@ describe('Testing relay association', () => { expect(response.body).toMatchObject({ methods: [ 'POST', 'GET' ], types: [ 'VALIDATION', 'NOTIFICATION' ] }); }); + test('CONFIG_SUITE - Should generate verification code', async () => { + const response = await request(app) + .patch(`/config/relay/verificationCode/${configId1}`) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send().expect(200); + + expect(response.body.code).not.toBe(undefined); + }); + + test('CONFIG_SUITE - Should NOT generate verification code', async () => { + await request(app) + .patch(`/config/relay/verificationCode/${new mongoose.Types.ObjectId()}`) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send(bodyRelayProd).expect(404); + }); + }); describe('Testing disable metrics', () => {