From 313785b4ace01eddc53ce48bc7b076a4eeee8c0f Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 22 Jan 2025 09:39:31 +0100 Subject: [PATCH] renaming api module to webapi --- .../apiCommand.ts => webapi/webapiCommand.ts} | 6 +- .../webapiCommandHandler.ts} | 38 ++++----- .../webapiPermissionCommand.ts} | 6 +- .../webapiPermissionCommandHandler.ts} | 63 +++++++------- .../{apiController.ts => webapiController.ts} | 58 +++++++------ .../{apiController.ts => webapiController.ts} | 6 +- src/data-source.ts | 12 +-- src/entity/user/{api.ts => webapi.ts} | 8 +- ...api_permission.ts => webapi_permission.ts} | 10 +-- src/factory/admin/user/{api.ts => webapi.ts} | 12 +-- src/helpers/jwtHelper.ts | 16 ++-- src/middleware/preventWebApiAccess.ts | 2 +- ...ns.ts => 1737453096674-addwebapiTokens.ts} | 28 +++---- src/routes/admin/index.ts | 6 +- src/routes/admin/user/{api.ts => webapi.ts} | 30 +++---- src/routes/index.ts | 2 +- src/routes/{api.ts => webapi.ts} | 2 +- src/service/user/apiService.ts | 83 ------------------- ...nService.ts => webapiPermissionService.ts} | 14 ++-- src/service/user/webapiService.ts | 83 +++++++++++++++++++ .../user/{api.models.ts => webapi.models.ts} | 0 21 files changed, 247 insertions(+), 238 deletions(-) rename src/command/user/{api/apiCommand.ts => webapi/webapiCommand.ts} (50%) rename src/command/user/{api/apiCommandHandler.ts => webapi/webapiCommandHandler.ts} (54%) rename src/command/user/{api/apiPermissionCommand.ts => webapi/webapiPermissionCommand.ts} (60%) rename src/command/user/{api/apiPermissionCommandHandler.ts => webapi/webapiPermissionCommandHandler.ts} (57%) rename src/controller/admin/user/{apiController.ts => webapiController.ts} (58%) rename src/controller/{apiController.ts => webapiController.ts} (86%) rename src/entity/user/{api.ts => webapi.ts} (72%) rename src/entity/user/{api_permission.ts => webapi_permission.ts} (73%) rename src/factory/admin/user/{api.ts => webapi.ts} (64%) rename src/migrations/{1737453096674-addApiTokens.ts => 1737453096674-addwebapiTokens.ts} (67%) rename src/routes/admin/user/{api.ts => webapi.ts} (69%) rename src/routes/{api.ts => webapi.ts} (78%) delete mode 100644 src/service/user/apiService.ts rename src/service/user/{apiPermissionService.ts => webapiPermissionService.ts} (52%) create mode 100644 src/service/user/webapiService.ts rename src/viewmodel/admin/user/{api.models.ts => webapi.models.ts} (100%) diff --git a/src/command/user/api/apiCommand.ts b/src/command/user/webapi/webapiCommand.ts similarity index 50% rename from src/command/user/api/apiCommand.ts rename to src/command/user/webapi/webapiCommand.ts index b2c669f..2ec3586 100644 --- a/src/command/user/api/apiCommand.ts +++ b/src/command/user/webapi/webapiCommand.ts @@ -1,15 +1,15 @@ -export interface CreateApiCommand { +export interface CreateWebapiCommand { title: string; token: string; expiry?: Date; } -export interface UpdateApiCommand { +export interface UpdateWebapiCommand { id: number; title: string; expiry?: Date; } -export interface DeleteApiCommand { +export interface DeleteWebapiCommand { id: number; } diff --git a/src/command/user/api/apiCommandHandler.ts b/src/command/user/webapi/webapiCommandHandler.ts similarity index 54% rename from src/command/user/api/apiCommandHandler.ts rename to src/command/user/webapi/webapiCommandHandler.ts index 180ff32..71689a6 100644 --- a/src/command/user/api/apiCommandHandler.ts +++ b/src/command/user/webapi/webapiCommandHandler.ts @@ -1,23 +1,23 @@ import { dataSource } from "../../../data-source"; -import { api } from "../../../entity/user/api"; +import { webapi } from "../../../entity/user/webapi"; import InternalException from "../../../exceptions/internalException"; -import { CreateApiCommand, DeleteApiCommand, UpdateApiCommand } from "./apiCommand"; +import { CreateWebapiCommand, DeleteWebapiCommand, UpdateWebapiCommand } from "./webapiCommand"; -export default abstract class ApiCommandHandler { +export default abstract class WebapiCommandHandler { /** * @description create api - * @param {CreateApiCommand} createApi + * @param {CreateWebapiCommand} createWebapi * @returns {Promise} */ - static async create(createApi: CreateApiCommand): Promise { + static async create(createWebapi: CreateWebapiCommand): Promise { return await dataSource .createQueryBuilder() .insert() - .into(api) + .into(webapi) .values({ - token: createApi.token, - title: createApi.title, - expiry: createApi.expiry, + token: createWebapi.token, + title: createWebapi.title, + expiry: createWebapi.expiry, }) .execute() .then((result) => { @@ -30,18 +30,18 @@ export default abstract class ApiCommandHandler { /** * @description update api - * @param {UpdateApiCommand} updateApi + * @param {UpdateWebapiCommand} updateWebapi * @returns {Promise} */ - static async update(updateApi: UpdateApiCommand): Promise { + static async update(updateWebapi: UpdateWebapiCommand): Promise { return await dataSource .createQueryBuilder() - .update(api) + .update(webapi) .set({ - title: updateApi.title, - expiry: updateApi.expiry, + title: updateWebapi.title, + expiry: updateWebapi.expiry, }) - .where("id = :id", { id: updateApi.id }) + .where("id = :id", { id: updateWebapi.id }) .execute() .then(() => {}) .catch((err) => { @@ -51,15 +51,15 @@ export default abstract class ApiCommandHandler { /** * @description delete api - * @param {DeleteApiCommand} deleteApi + * @param {DeleteWebapiCommand} deleteWebapi * @returns {Promise} */ - static async delete(deleteApi: DeleteApiCommand): Promise { + static async delete(deleteWebapi: DeleteWebapiCommand): Promise { return await dataSource .createQueryBuilder() .delete() - .from(api) - .where("id = :id", { id: deleteApi.id }) + .from(webapi) + .where("id = :id", { id: deleteWebapi.id }) .execute() .then(() => {}) .catch((err) => { diff --git a/src/command/user/api/apiPermissionCommand.ts b/src/command/user/webapi/webapiPermissionCommand.ts similarity index 60% rename from src/command/user/api/apiPermissionCommand.ts rename to src/command/user/webapi/webapiPermissionCommand.ts index 2b61b65..6f192d1 100644 --- a/src/command/user/api/apiPermissionCommand.ts +++ b/src/command/user/webapi/webapiPermissionCommand.ts @@ -1,16 +1,16 @@ import { PermissionString } from "../../../type/permissionTypes"; -export interface CreateApiPermissionCommand { +export interface CreateWebapiPermissionCommand { permission: PermissionString; apiId: number; } -export interface DeleteApiPermissionCommand { +export interface DeleteWebapiPermissionCommand { permission: PermissionString; apiId: number; } -export interface UpdateApiPermissionsCommand { +export interface UpdateWebapiPermissionsCommand { apiId: number; permissions: Array; } diff --git a/src/command/user/api/apiPermissionCommandHandler.ts b/src/command/user/webapi/webapiPermissionCommandHandler.ts similarity index 57% rename from src/command/user/api/apiPermissionCommandHandler.ts rename to src/command/user/webapi/webapiPermissionCommandHandler.ts index f6fdb64..6d30bc0 100644 --- a/src/command/user/api/apiPermissionCommandHandler.ts +++ b/src/command/user/webapi/webapiPermissionCommandHandler.ts @@ -1,34 +1,39 @@ import { DeleteResult, EntityManager, InsertResult } from "typeorm"; import { dataSource } from "../../../data-source"; -import { apiPermission } from "../../../entity/user/api_permission"; +import { webapiPermission } from "../../../entity/user/webapi_permission"; import InternalException from "../../../exceptions/internalException"; -import ApiService from "../../../service/user/apiService"; +import WebapiService from "../../../service/user/webapiService"; import { - CreateApiPermissionCommand, - DeleteApiPermissionCommand, - UpdateApiPermissionsCommand, -} from "./apiPermissionCommand"; + CreateWebapiPermissionCommand, + DeleteWebapiPermissionCommand, + UpdateWebapiPermissionsCommand, +} from "./webapiPermissionCommand"; import PermissionHelper from "../../../helpers/permissionHelper"; -import ApiPermissionService from "../../../service/user/apiPermissionService"; +import WebapiPermissionService from "../../../service/user/webapiPermissionService"; import { PermissionString } from "../../../type/permissionTypes"; -export default abstract class ApiPermissionCommandHandler { +export default abstract class WebapiPermissionCommandHandler { /** * @description update api permissions - * @param {UpdateApiPermissionsCommand} updateApiPermissions + * @param {UpdateWebapiPermissionsCommand} updateWebapiPermissions * @returns {Promise} */ - static async updatePermissions(updateApiPermissions: UpdateApiPermissionsCommand): Promise { - let currentPermissions = (await ApiPermissionService.getByApi(updateApiPermissions.apiId)).map((r) => r.permission); + static async updatePermissions(updateWebapiPermissions: UpdateWebapiPermissionsCommand): Promise { + let currentPermissions = (await WebapiPermissionService.getByApi(updateWebapiPermissions.apiId)).map( + (r) => r.permission + ); return await dataSource.manager .transaction(async (manager) => { - let newPermissions = PermissionHelper.getWhatToAdd(currentPermissions, updateApiPermissions.permissions); - let removePermissions = PermissionHelper.getWhatToRemove(currentPermissions, updateApiPermissions.permissions); + let newPermissions = PermissionHelper.getWhatToAdd(currentPermissions, updateWebapiPermissions.permissions); + let removePermissions = PermissionHelper.getWhatToRemove( + currentPermissions, + updateWebapiPermissions.permissions + ); if (newPermissions.length != 0) { - await this.updatePermissionsAdd(manager, updateApiPermissions.apiId, newPermissions); + await this.updatePermissionsAdd(manager, updateWebapiPermissions.apiId, newPermissions); } if (removePermissions.length != 0) { - await this.updatePermissionsRemove(manager, updateApiPermissions.apiId, removePermissions); + await this.updatePermissionsRemove(manager, updateWebapiPermissions.apiId, removePermissions); } }) .then(() => {}) @@ -39,17 +44,17 @@ export default abstract class ApiPermissionCommandHandler { private static async updatePermissionsAdd( manager: EntityManager, - apiId: number, + webapiId: number, permissions: Array ): Promise { return await manager .createQueryBuilder() .insert() - .into(apiPermission) + .into(webapiPermission) .values( permissions.map((p) => ({ permission: p, - apiId: apiId, + apiId: webapiId, })) ) .orIgnore() @@ -58,31 +63,31 @@ export default abstract class ApiPermissionCommandHandler { private static async updatePermissionsRemove( manager: EntityManager, - apiId: number, + webapiId: number, permissions: Array ): Promise { return await manager .createQueryBuilder() .delete() - .from(apiPermission) - .where("apiId = :id", { id: apiId }) + .from(webapiPermission) + .where("webapiId = :id", { id: webapiId }) .andWhere("permission IN (:...permission)", { permission: permissions }) .execute(); } /** * @description grant permission to user - * @param {CreateApiPermissionCommand} createPermission + * @param {CreateWebapiPermissionCommand} createPermission * @returns {Promise} */ - static async create(createPermission: CreateApiPermissionCommand): Promise { + static async create(createPermission: CreateWebapiPermissionCommand): Promise { return await dataSource .createQueryBuilder() .insert() - .into(apiPermission) + .into(webapiPermission) .values({ permission: createPermission.permission, - apiId: createPermission.apiId, + webapiId: createPermission.apiId, }) .execute() .then((result) => { @@ -95,15 +100,15 @@ export default abstract class ApiPermissionCommandHandler { /** * @description remove permission from api - * @param {DeleteApiPermissionCommand} deletePermission + * @param {DeleteWebapiPermissionCommand} deletePermission * @returns {Promise} */ - static async delete(deletePermission: DeleteApiPermissionCommand): Promise { + static async delete(deletePermission: DeleteWebapiPermissionCommand): Promise { return await dataSource .createQueryBuilder() .delete() - .from(apiPermission) - .where("apiId = :id", { id: deletePermission.apiId }) + .from(webapiPermission) + .where("webapiId = :id", { id: deletePermission.apiId }) .andWhere("permission = :permission", { permission: deletePermission.permission }) .execute() .then(() => {}) diff --git a/src/controller/admin/user/apiController.ts b/src/controller/admin/user/webapiController.ts similarity index 58% rename from src/controller/admin/user/apiController.ts rename to src/controller/admin/user/webapiController.ts index 1d64139..bcef24b 100644 --- a/src/controller/admin/user/apiController.ts +++ b/src/controller/admin/user/webapiController.ts @@ -1,12 +1,16 @@ import { Request, Response } from "express"; -import ApiService from "../../../service/user/apiService"; -import ApiFactory from "../../../factory/admin/user/api"; -import ApiPermissionService from "../../../service/user/apiPermissionService"; +import WebapiService from "../../../service/user/webapiService"; +import ApiFactory from "../../../factory/admin/user/webapi"; +import WebapiPermissionService from "../../../service/user/webapiPermissionService"; import PermissionHelper from "../../../helpers/permissionHelper"; -import { CreateApiCommand, DeleteApiCommand, UpdateApiCommand } from "../../../command/user/api/apiCommand"; -import ApiCommandHandler from "../../../command/user/api/apiCommandHandler"; -import { UpdateApiPermissionsCommand } from "../../../command/user/api/apiPermissionCommand"; -import ApiPermissionCommandHandler from "../../../command/user/api/apiPermissionCommandHandler"; +import { + CreateWebapiCommand, + DeleteWebapiCommand, + UpdateWebapiCommand, +} from "../../../command/user/webapi/webapiCommand"; +import WebapiCommandHandler from "../../../command/user/webapi/webapiCommandHandler"; +import { UpdateWebapiPermissionsCommand } from "../../../command/user/webapi/webapiPermissionCommand"; +import WebapiPermissionCommandHandler from "../../../command/user/webapi/webapiPermissionCommandHandler"; import { JWTHelper } from "../../../helpers/jwtHelper"; import { CLUB_NAME } from "../../../env.defaults"; import { StringHelper } from "../../../helpers/stringHelper"; @@ -17,8 +21,8 @@ import { StringHelper } from "../../../helpers/stringHelper"; * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function getAllApis(req: Request, res: Response): Promise { - let apis = await ApiService.getAll(); +export async function getAllWebapis(req: Request, res: Response): Promise { + let apis = await WebapiService.getAll(); res.json(ApiFactory.mapToBase(apis)); } @@ -29,9 +33,9 @@ export async function getAllApis(req: Request, res: Response): Promise { * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function getApiById(req: Request, res: Response): Promise { +export async function getWebapiById(req: Request, res: Response): Promise { const id = parseInt(req.params.id); - let api = await ApiService.getById(id); + let api = await WebapiService.getById(id); res.json(ApiFactory.mapToSingle(api)); } @@ -42,9 +46,9 @@ export async function getApiById(req: Request, res: Response): Promise { * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function getApiTokenById(req: Request, res: Response): Promise { +export async function getWebapiTokenById(req: Request, res: Response): Promise { const id = parseInt(req.params.id); - let { token } = await ApiService.getTokenById(id); + let { token } = await WebapiService.getTokenById(id); res.send(token); } @@ -55,9 +59,9 @@ export async function getApiTokenById(req: Request, res: Response): Promise * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function getApiPermissions(req: Request, res: Response): Promise { +export async function getWebapiPermissions(req: Request, res: Response): Promise { const id = parseInt(req.params.id); - let permissions = await ApiPermissionService.getByApi(id); + let permissions = await WebapiPermissionService.getByApi(id); res.json(PermissionHelper.convertToObject(permissions.map((p) => p.permission))); } @@ -68,7 +72,7 @@ export async function getApiPermissions(req: Request, res: Response): Promise} */ -export async function createApi(req: Request, res: Response): Promise { +export async function createWebapi(req: Request, res: Response): Promise { let title = req.body.title; let expiry = req.body.expiry; @@ -79,12 +83,12 @@ export async function createApi(req: Request, res: Response): Promise { aud: StringHelper.random(32), }); - let createApi: CreateApiCommand = { + let createApi: CreateWebapiCommand = { token: token, title: title, expiry: expiry, }; - await ApiCommandHandler.create(createApi); + await WebapiCommandHandler.create(createApi); res.sendStatus(204); } @@ -95,17 +99,17 @@ export async function createApi(req: Request, res: Response): Promise { * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function updateApi(req: Request, res: Response): Promise { +export async function updateWebapi(req: Request, res: Response): Promise { const id = parseInt(req.params.id); let title = req.body.title; let expiry = req.body.expiry; - let updateApi: UpdateApiCommand = { + let updateApi: UpdateWebapiCommand = { id: id, title: title, expiry: expiry, }; - await ApiCommandHandler.update(updateApi); + await WebapiCommandHandler.update(updateApi); res.sendStatus(204); } @@ -116,17 +120,17 @@ export async function updateApi(req: Request, res: Response): Promise { * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function updateApiPermissions(req: Request, res: Response): Promise { +export async function updateWebapiPermissions(req: Request, res: Response): Promise { const id = parseInt(req.params.id); let permissions = req.body.permissions; let permissionStrings = PermissionHelper.convertToStringArray(permissions); - let updateApiPermissions: UpdateApiPermissionsCommand = { + let updateApiPermissions: UpdateWebapiPermissionsCommand = { apiId: id, permissions: permissionStrings, }; - await ApiPermissionCommandHandler.updatePermissions(updateApiPermissions); + await WebapiPermissionCommandHandler.updatePermissions(updateApiPermissions); res.sendStatus(204); } @@ -137,13 +141,13 @@ export async function updateApiPermissions(req: Request, res: Response): Promise * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function deleteApi(req: Request, res: Response): Promise { +export async function deleteWebapi(req: Request, res: Response): Promise { const id = parseInt(req.params.id); - let deleteApi: DeleteApiCommand = { + let deleteApi: DeleteWebapiCommand = { id: id, }; - await ApiCommandHandler.delete(deleteApi); + await WebapiCommandHandler.delete(deleteApi); res.sendStatus(204); } diff --git a/src/controller/apiController.ts b/src/controller/webapiController.ts similarity index 86% rename from src/controller/apiController.ts rename to src/controller/webapiController.ts index bb49a8c..33f765d 100644 --- a/src/controller/apiController.ts +++ b/src/controller/webapiController.ts @@ -8,7 +8,7 @@ import UserService from "../service/user/userService"; import speakeasy from "speakeasy"; import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException"; import RefreshService from "../service/refreshService"; -import ApiService from "../service/user/apiService"; +import WebapiService from "../service/user/webapiService"; import ForbiddenRequestException from "../exceptions/forbiddenRequestException"; /** @@ -20,13 +20,13 @@ import ForbiddenRequestException from "../exceptions/forbiddenRequestException"; export async function getWebApiAccess(req: Request, res: Response): Promise { const bearer = req.headers.authorization?.split(" ")?.[1] ?? undefined; - let { expiry } = await ApiService.getByToken(bearer); + let { expiry } = await WebapiService.getByToken(bearer); if (new Date() > new Date(expiry)) { throw new ForbiddenRequestException("api token expired"); } - let accessToken = await JWTHelper.buildApiToken(bearer); + let accessToken = await JWTHelper.buildWebapiToken(bearer); res.json({ accessToken, diff --git a/src/data-source.ts b/src/data-source.ts index b3c0227..8ba9056 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -68,9 +68,9 @@ import { Memberlist1736079005086 } from "./migrations/1736079005086-memberlist"; import { ExtendViewValues1736084198860 } from "./migrations/1736084198860-extendViewValues"; import { FinishInternalIdTransfer1736505324488 } from "./migrations/1736505324488-finishInternalIdTransfer"; import { ProtocolPresenceExcuse1737287798828 } from "./migrations/1737287798828-protocolPresenceExcuse"; -import { api } from "./entity/user/api"; -import { apiPermission } from "./entity/user/api_permission"; -import { AddApiTokens1737453096674 } from "./migrations/1737453096674-addApiTokens"; +import { webapi } from "./entity/user/webapi"; +import { webapiPermission } from "./entity/user/webapi_permission"; +import { AddWebapiTokens1737453096674 } from "./migrations/1737453096674-addwebapiTokens"; const dataSource = new DataSource({ type: DB_TYPE as any, @@ -120,8 +120,8 @@ const dataSource = new DataSource({ memberExecutivePositionsView, memberQualificationsView, membershipView, - api, - apiPermission, + webapi, + webapiPermission, ], migrations: [ Initial1724317398939, @@ -151,7 +151,7 @@ const dataSource = new DataSource({ ExtendViewValues1736084198860, FinishInternalIdTransfer1736505324488, ProtocolPresenceExcuse1737287798828, - AddApiTokens1737453096674, + AddWebapiTokens1737453096674, ], migrationsRun: true, migrationsTransactionMode: "each", diff --git a/src/entity/user/api.ts b/src/entity/user/webapi.ts similarity index 72% rename from src/entity/user/api.ts rename to src/entity/user/webapi.ts index 3e21b78..e1c73a6 100644 --- a/src/entity/user/api.ts +++ b/src/entity/user/webapi.ts @@ -1,8 +1,8 @@ import { Column, CreateDateColumn, Entity, OneToMany, PrimaryColumn } from "typeorm"; -import { apiPermission } from "./api_permission"; +import { webapiPermission } from "./webapi_permission"; @Entity() -export class api { +export class webapi { @PrimaryColumn({ generated: "increment", type: "int" }) id: number; @@ -21,6 +21,6 @@ export class api { @Column({ type: "datetime", nullable: true }) expiry?: Date; - @OneToMany(() => apiPermission, (apiPermission) => apiPermission.api) - permissions: apiPermission[]; + @OneToMany(() => webapiPermission, (apiPermission) => apiPermission.webapi) + permissions: webapiPermission[]; } diff --git a/src/entity/user/api_permission.ts b/src/entity/user/webapi_permission.ts similarity index 73% rename from src/entity/user/api_permission.ts rename to src/entity/user/webapi_permission.ts index 4e7509f..988c7b9 100644 --- a/src/entity/user/api_permission.ts +++ b/src/entity/user/webapi_permission.ts @@ -1,19 +1,19 @@ import { Column, Entity, ManyToOne, OneToMany, PrimaryColumn } from "typeorm"; import { PermissionObject, PermissionString } from "../../type/permissionTypes"; -import { api } from "./api"; +import { webapi } from "./webapi"; @Entity() -export class apiPermission { +export class webapiPermission { @PrimaryColumn({ type: "int" }) - apiId: number; + webapiId: number; @PrimaryColumn({ type: "varchar", length: 255 }) permission: PermissionString; - @ManyToOne(() => api, { + @ManyToOne(() => webapi, { nullable: false, onDelete: "CASCADE", onUpdate: "RESTRICT", }) - api: api; + webapi: webapi; } diff --git a/src/factory/admin/user/api.ts b/src/factory/admin/user/webapi.ts similarity index 64% rename from src/factory/admin/user/api.ts rename to src/factory/admin/user/webapi.ts index 111be2a..3c0ede4 100644 --- a/src/factory/admin/user/api.ts +++ b/src/factory/admin/user/webapi.ts @@ -1,14 +1,14 @@ -import { api } from "../../../entity/user/api"; +import { webapi } from "../../../entity/user/webapi"; import PermissionHelper from "../../../helpers/permissionHelper"; -import { ApiViewModel } from "../../../viewmodel/admin/user/api.models"; +import { ApiViewModel } from "../../../viewmodel/admin/user/webapi.models"; export default abstract class ApiFactory { /** * @description map record to api - * @param {api} record + * @param {webapi} record * @returns {apiViewModel} */ - public static mapToSingle(record: api): ApiViewModel { + public static mapToSingle(record: webapi): ApiViewModel { return { id: record.id, permissions: PermissionHelper.convertToObject(record.permissions.map((e) => e.permission)), @@ -21,10 +21,10 @@ export default abstract class ApiFactory { /** * @description map records to api - * @param {Array} records + * @param {Array} records * @returns {Array} */ - public static mapToBase(records: Array): Array { + public static mapToBase(records: Array): Array { return records.map((r) => this.mapToSingle(r)); } } diff --git a/src/helpers/jwtHelper.ts b/src/helpers/jwtHelper.ts index 66beaab..af177e5 100644 --- a/src/helpers/jwtHelper.ts +++ b/src/helpers/jwtHelper.ts @@ -6,8 +6,8 @@ import RolePermissionService from "../service/user/rolePermissionService"; import UserPermissionService from "../service/user/userPermissionService"; import UserService from "../service/user/userService"; import PermissionHelper from "./permissionHelper"; -import ApiService from "../service/user/apiService"; -import ApiPermissionService from "../service/user/apiPermissionService"; +import WebapiService from "../service/user/webapiService"; +import WebapiPermissionService from "../service/user/webapiPermissionService"; export abstract class JWTHelper { static validate(token: string): Promise { @@ -75,11 +75,11 @@ export abstract class JWTHelper { }); } - static async buildApiToken(token: string): Promise { - let { id, title } = await ApiService.getByToken(token); - let apiPermissions = await ApiPermissionService.getByApi(id); - let apiPermissionStrings = apiPermissions.map((e) => e.permission); - let permissionObject = PermissionHelper.convertToObject(apiPermissionStrings); + static async buildWebapiToken(token: string): Promise { + let { id, title } = await WebapiService.getByToken(token); + let webapiPermissions = await WebapiPermissionService.getByApi(id); + let webapiPermissionStrings = webapiPermissions.map((e) => e.permission); + let permissionObject = PermissionHelper.convertToObject(webapiPermissionStrings); let jwtData: JWTToken = { userId: id, @@ -97,7 +97,7 @@ export abstract class JWTHelper { return result; }) .catch((err) => { - throw new InternalException("Failed accessToken creation", err); + throw new InternalException("Failed webapi accessToken creation", err); }); } } diff --git a/src/middleware/preventWebApiAccess.ts b/src/middleware/preventWebApiAccess.ts index a7b10a5..3c3b7c6 100644 --- a/src/middleware/preventWebApiAccess.ts +++ b/src/middleware/preventWebApiAccess.ts @@ -1,7 +1,7 @@ import { Request, Response } from "express"; import ForbiddenRequestException from "../exceptions/forbiddenRequestException"; -export default async function preventApiAccess(req: Request, res: Response, next: Function) { +export default async function preventWebapiAccess(req: Request, res: Response, next: Function) { if (req.isWebApiRequest) { throw new ForbiddenRequestException("This route cannot be accessed via webapi"); } else { diff --git a/src/migrations/1737453096674-addApiTokens.ts b/src/migrations/1737453096674-addwebapiTokens.ts similarity index 67% rename from src/migrations/1737453096674-addApiTokens.ts rename to src/migrations/1737453096674-addwebapiTokens.ts index d547892..81b4e45 100644 --- a/src/migrations/1737453096674-addApiTokens.ts +++ b/src/migrations/1737453096674-addwebapiTokens.ts @@ -1,17 +1,17 @@ import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm"; import { DB_TYPE } from "../env.defaults"; -export class AddApiTokens1737453096674 implements MigrationInterface { - name = "AddApiTokens1737453096674"; +export class AddWebapiTokens1737453096674 implements MigrationInterface { + name = "AddWebApiTokens1737453096674"; public async up(queryRunner: QueryRunner): Promise { const variableType_int = DB_TYPE == "mysql" ? "int" : "integer"; await queryRunner.createTable( new Table({ - name: "api", + name: "webapi", columns: [ - { name: "id", type: variableType_int, isPrimary: true, isNullable: false }, + { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { name: "token", type: "varchar", length: "255", isUnique: true, isNullable: false }, { name: "title", type: "varchar", length: "255", isNullable: false }, { name: "createdAt", type: "datetime", default: "CURRENT_TIMESTAMP(6)", isNullable: false }, @@ -24,9 +24,9 @@ export class AddApiTokens1737453096674 implements MigrationInterface { await queryRunner.createTable( new Table({ - name: "api_permission", + name: "webapi_permission", columns: [ - { name: "apiId", type: variableType_int, isPrimary: true, isNullable: false }, + { name: "webapiId", type: variableType_int, isPrimary: true, isNullable: false }, { name: "permission", type: "varchar", length: "255", isPrimary: true, isNullable: false }, ], }), @@ -34,11 +34,11 @@ export class AddApiTokens1737453096674 implements MigrationInterface { ); await queryRunner.createForeignKey( - "api_permission", + "webapi_permission", new TableForeignKey({ - columnNames: ["apiId"], + columnNames: ["webapiId"], referencedColumnNames: ["id"], - referencedTableName: "api", + referencedTableName: "webapi", onDelete: "CASCADE", onUpdate: "RESTRICT", }) @@ -46,10 +46,10 @@ export class AddApiTokens1737453096674 implements MigrationInterface { } public async down(queryRunner: QueryRunner): Promise { - const table = await queryRunner.getTable("api_permission"); - const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("apiToken") !== -1); - await queryRunner.dropForeignKey("api_permission", foreignKey); - await queryRunner.dropTable("api_permission"); - await queryRunner.dropTable("api"); + const table = await queryRunner.getTable("webapi_permission"); + const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("webapiId") !== -1); + await queryRunner.dropForeignKey("webapi_permission", foreignKey); + await queryRunner.dropTable("webapi_permission"); + await queryRunner.dropTable("webapi"); } } diff --git a/src/routes/admin/index.ts b/src/routes/admin/index.ts index 3662de9..9aa24ce 100644 --- a/src/routes/admin/index.ts +++ b/src/routes/admin/index.ts @@ -21,8 +21,8 @@ import newsletter from "./club/newsletter"; import role from "./user/role"; import user from "./user/user"; import invite from "./user/invite"; -import api from "./user/api"; -import preventApiAccess from "../../middleware/preventWebApiAccess"; +import api from "./user/webapi"; +import preventWebapiAccess from "../../middleware/preventWebApiAccess"; var router = express.Router({ mergeParams: true }); @@ -62,6 +62,6 @@ router.use("/newsletter", PermissionHelper.passCheckMiddleware("read", "club", " router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role); router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user); router.use("/invite", PermissionHelper.passCheckMiddleware("read", "user", "user"), invite); -router.use("/webapi", preventApiAccess, PermissionHelper.passCheckMiddleware("read", "user", "webapi"), api); +router.use("/webapi", preventWebapiAccess, PermissionHelper.passCheckMiddleware("read", "user", "webapi"), api); export default router; diff --git a/src/routes/admin/user/api.ts b/src/routes/admin/user/webapi.ts similarity index 69% rename from src/routes/admin/user/api.ts rename to src/routes/admin/user/webapi.ts index c5b3089..4e8feb9 100644 --- a/src/routes/admin/user/api.ts +++ b/src/routes/admin/user/webapi.ts @@ -1,34 +1,34 @@ import express, { Request, Response } from "express"; import PermissionHelper from "../../../helpers/permissionHelper"; import { - createApi, - deleteApi, - getAllApis, - getApiById, - getApiPermissions, - updateApi, - updateApiPermissions, -} from "../../../controller/admin/user/apiController"; + createWebapi, + deleteWebapi, + getAllWebapis, + getWebapiById, + getWebapiPermissions, + updateWebapi, + updateWebapiPermissions, +} from "../../../controller/admin/user/webapiController"; var router = express.Router({ mergeParams: true }); router.get("/", async (req: Request, res: Response) => { - await getAllApis(req, res); + await getAllWebapis(req, res); }); router.get("/:id", async (req: Request, res: Response) => { - await getApiById(req, res); + await getWebapiById(req, res); }); router.get("/:id/permissions", async (req: Request, res: Response) => { - await getApiPermissions(req, res); + await getWebapiPermissions(req, res); }); router.post( "/", PermissionHelper.passCheckMiddleware("create", "user", "webapi"), async (req: Request, res: Response) => { - await createApi(req, res); + await createWebapi(req, res); } ); @@ -36,7 +36,7 @@ router.patch( "/:id", PermissionHelper.passCheckMiddleware("update", "user", "webapi"), async (req: Request, res: Response) => { - await updateApi(req, res); + await updateWebapi(req, res); } ); @@ -44,7 +44,7 @@ router.patch( "/:id/permissions", PermissionHelper.passCheckMiddleware("admin", "user", "webapi"), async (req: Request, res: Response) => { - await updateApiPermissions(req, res); + await updateWebapiPermissions(req, res); } ); @@ -52,7 +52,7 @@ router.delete( "/:id", PermissionHelper.passCheckMiddleware("delete", "user", "webapi"), async (req: Request, res: Response) => { - await deleteApi(req, res); + await deleteWebapi(req, res); } ); diff --git a/src/routes/index.ts b/src/routes/index.ts index 06a1063..3509940 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -14,7 +14,7 @@ import auth from "./auth"; import admin from "./admin/index"; import user from "./user"; import detectPWA from "../middleware/detectPWA"; -import api from "./api"; +import api from "./webapi"; import authenticateAPI from "../middleware/authenticateAPI"; export default (app: Express) => { diff --git a/src/routes/api.ts b/src/routes/webapi.ts similarity index 78% rename from src/routes/api.ts rename to src/routes/webapi.ts index 0ded862..164802c 100644 --- a/src/routes/api.ts +++ b/src/routes/webapi.ts @@ -1,5 +1,5 @@ import express, { Request, Response } from "express"; -import { getWebApiAccess } from "../controller/apiController"; +import { getWebApiAccess } from "../controller/webapiController"; var router = express.Router({ mergeParams: true }); diff --git a/src/service/user/apiService.ts b/src/service/user/apiService.ts deleted file mode 100644 index b44b3ca..0000000 --- a/src/service/user/apiService.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { dataSource } from "../../data-source"; -import { api } from "../../entity/user/api"; -import InternalException from "../../exceptions/internalException"; - -export default abstract class ApiService { - /** - * @description get apis - * @returns {Promise>} - */ - static async getAll(): Promise> { - return await dataSource - .getRepository(api) - .createQueryBuilder("api") - .leftJoinAndSelect("api.permissions", "permissions") - .getMany() - .then((res) => { - return res; - }) - .catch((err) => { - throw new InternalException("apis not found", err); - }); - } - - /** - * @description get api by id - * @param id number - * @returns {Promise} - */ - static async getById(id: number): Promise { - return await dataSource - .getRepository(api) - .createQueryBuilder("api") - .leftJoinAndSelect("api.permissions", "permissions") - .where("api.id = :id", { id: id }) - .getOneOrFail() - .then((res) => { - return res; - }) - .catch((err) => { - throw new InternalException("api not found by id", err); - }); - } - - /** - * @description get api by token - * @param token string - * @returns {Promise} - */ - static async getByToken(token: string): Promise { - return await dataSource - .getRepository(api) - .createQueryBuilder("api") - .leftJoinAndSelect("api.permissions", "permissions") - .where("api.token = :token", { token: token }) - .getOneOrFail() - .then((res) => { - return res; - }) - .catch((err) => { - throw new InternalException("api not found by token", err); - }); - } - - /** - * @description get api by id - * @param id number - * @returns {Promise} - */ - static async getTokenById(id: number): Promise { - return await dataSource - .getRepository(api) - .createQueryBuilder("api") - .select("token") - .where("api.id = :id", { id: id }) - .getOneOrFail() - .then((res) => { - return res; - }) - .catch((err) => { - throw new InternalException("api token not found by id", err); - }); - } -} diff --git a/src/service/user/apiPermissionService.ts b/src/service/user/webapiPermissionService.ts similarity index 52% rename from src/service/user/apiPermissionService.ts rename to src/service/user/webapiPermissionService.ts index 13f0ed2..c50895c 100644 --- a/src/service/user/apiPermissionService.ts +++ b/src/service/user/webapiPermissionService.ts @@ -1,18 +1,18 @@ import { dataSource } from "../../data-source"; -import { apiPermission } from "../../entity/user/api_permission"; +import { webapiPermission } from "../../entity/user/webapi_permission"; import InternalException from "../../exceptions/internalException"; -export default abstract class ApiPermissionService { +export default abstract class WebapiPermissionService { /** * @description get permission by api - * @param apiId number - * @returns {Promise>} + * @param webapiId number + * @returns {Promise>} */ - static async getByApi(apiId: number): Promise> { + static async getByApi(webapiId: number): Promise> { return await dataSource - .getRepository(apiPermission) + .getRepository(webapiPermission) .createQueryBuilder("api_permission") - .where("api_permission.apiId = :apiId", { apiId: apiId }) + .where("api_permission.apiId = :apiId", { apiId: webapiId }) .getMany() .then((res) => { return res; diff --git a/src/service/user/webapiService.ts b/src/service/user/webapiService.ts new file mode 100644 index 0000000..264ef7a --- /dev/null +++ b/src/service/user/webapiService.ts @@ -0,0 +1,83 @@ +import { dataSource } from "../../data-source"; +import { webapi } from "../../entity/user/webapi"; +import InternalException from "../../exceptions/internalException"; + +export default abstract class WebapiService { + /** + * @description get apis + * @returns {Promise>} + */ + static async getAll(): Promise> { + return await dataSource + .getRepository(webapi) + .createQueryBuilder("webapi") + .leftJoinAndSelect("webapi.permissions", "permissions") + .getMany() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("webapis not found", err); + }); + } + + /** + * @description get api by id + * @param id number + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(webapi) + .createQueryBuilder("webapi") + .leftJoinAndSelect("webapi.permissions", "permissions") + .where("webapi.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("webapi not found by id", err); + }); + } + + /** + * @description get api by token + * @param token string + * @returns {Promise} + */ + static async getByToken(token: string): Promise { + return await dataSource + .getRepository(webapi) + .createQueryBuilder("webapi") + .leftJoinAndSelect("webapi.permissions", "permissions") + .where("webapi.token = :token", { token: token }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("webapi not found by token", err); + }); + } + + /** + * @description get api by id + * @param id number + * @returns {Promise} + */ + static async getTokenById(id: number): Promise { + return await dataSource + .getRepository(webapi) + .createQueryBuilder("webapi") + .select("token") + .where("webapi.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("webapi token not found by id", err); + }); + } +} diff --git a/src/viewmodel/admin/user/api.models.ts b/src/viewmodel/admin/user/webapi.models.ts similarity index 100% rename from src/viewmodel/admin/user/api.models.ts rename to src/viewmodel/admin/user/webapi.models.ts