From 5a49763fd6ecf4d6a1da74876c0145c968ffe50c Mon Sep 17 00:00:00 2001 From: FLYBYME Date: Mon, 4 Dec 2023 22:13:12 -0500 Subject: [PATCH] Add generateAuthToken function to repositories.service.js --- services/repositories.service.js | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/services/repositories.service.js b/services/repositories.service.js index 54ea1b9..b71535b 100644 --- a/services/repositories.service.js +++ b/services/repositories.service.js @@ -622,6 +622,44 @@ module.exports = { } }, + /** + * generate auth token for repository + * + * @actions + * @param {String} id - id of repository + * + * @returns {String} - repository auth token + */ + generateAuthToken: { + rest: { + method: "GET", + path: "/:id/token", + }, + params: { + id: { + type: "string", + optional: false, + }, + }, + async handler(ctx) { + const params = Object.assign({}, ctx.params); + + // get repository + const repository = await this.getById(ctx, params.id); + + // check repository exists + if (!repository) { + throw new MoleculerClientError("repository not found", 404); + } + + // generate auth token + const authToken = await this.generateAuthToken(ctx, repository); + + // return auth token + return authToken; + } + }, + // clean db clean: { async handler(ctx) { @@ -801,6 +839,25 @@ module.exports = { return repositoryPath; }, + /** + * generate auth token for repository + * + * @param {Context} ctx - context of request + * @param {Object} repository - repository object + * + * @returns {Promise} - repository auth token + */ + async generateAuthToken(ctx, repository) { + // generate auth token + const authToken = await ctx.call('v1.tokens.generate', { + type: 'api-key', + owner: repository.owner, + }); + + // return auth token + return authToken; + }, + },