Skip to content

Commit

Permalink
added local access guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Jan 2, 2025
1 parent fc6df9c commit 5700292
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
17 changes: 17 additions & 0 deletions src/guards/localAccess.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, CanActivate, ExecutionContext, HttpException } from '@nestjs/common';
import { Request } from 'express';


@Injectable()
export class LocalAccessGuard implements CanActivate {
constructor() { }

async canActivate(context: ExecutionContext): Promise<boolean> {
const request: Request = context.switchToHttp().getRequest();

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN)
throw new HttpException('Forbidden', 403);

return true;
}
}
6 changes: 2 additions & 4 deletions src/user/user.controller.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { PageTakeQueryDTO, QueryDTO } from './dto/queries.dto';
import { LocaleException } from 'src/interceptors/localization.interceptor';
import responses_minecraft from 'src/localization/minecraft.localization';
import responses_common from 'src/localization/common.localization';
import { LocalAccessGuard } from 'src/guards/localAccess.guard';


@Controller({ version: '1' })
Expand Down Expand Up @@ -119,15 +120,12 @@ export class UserController {

@Get("/users/:username")
@Auth(AuthEnum.Weak)
@UseGuards(new LocalAccessGuard())
async user_profile(
@Param('username') username: string,
@Req() request: RequestSession
) {
/* get user data by nickname */

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN)
throw new LocaleException(responses_common.FORBIDDEN, 403);

return await this.userService.getUserByNickname(username, request.session);
}

Expand Down
18 changes: 5 additions & 13 deletions src/workshop/workshop.controller.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { SetQueryDTO } from 'src/user/dto/queries.dto';
import responses_common from 'src/localization/common.localization';
import { LocaleException } from 'src/interceptors/localization.interceptor';
import { LocalAccessGuard } from 'src/guards/localAccess.guard';

@Controller({ path: 'workshop', version: '1' })
@UseGuards(AuthGuard)
Expand Down Expand Up @@ -89,30 +90,23 @@ export class WorkshopController {

@Post(':id/view')
@SkipThrottle()
async viewBandage(
@Param('id') id: string,
@Req() request: Request,
) {
@UseGuards(new LocalAccessGuard())
async viewBandage(@Param('id') id: string) {
/* Add bandage view (internal endpoint) */

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN) {
throw new LocaleException(responses_common.FORBIDDEN, 403);
}
await this.bandageService.addView(id);
}

@Get(':id/info')
@SkipThrottle()
@Auth(AuthEnum.Weak)
@UseGuards(new LocalAccessGuard())
async getBandageOg(
@Param('id') id: string,
@Req() request: RequestSession
) {
/* get bandage info by external id (internal endpoint) */

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN) {
throw new LocaleException(responses_common.FORBIDDEN, 403);
}
return await this.bandageService.getDataForOg(id, request.session);
}

Expand Down Expand Up @@ -268,15 +262,13 @@ export class WorkshopController {
@Get(':id')
@SkipThrottle()
@Auth(AuthEnum.Weak)
@UseGuards(new LocalAccessGuard())
async getBandage(
@Param('id') id: string,
@Req() request: RequestSession
) {
/* get bandage by external id (internal endpoint) */

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN) {
throw new LocaleException(responses_common.FORBIDDEN, 403);
}
return await this.bandageService.getBandage(id, request.session);
}
}

0 comments on commit 5700292

Please sign in to comment.