diff --git a/backend/package.json b/backend/package.json index b90f31c..40f5a50 100644 --- a/backend/package.json +++ b/backend/package.json @@ -36,6 +36,7 @@ "passport": "^0.5.2", "passport-jwt": "^4.0.0", "passport-local": "^1.0.0", + "passport-strategy": "^1.0.0", "pg": "^8.7.3", "picsur-shared": "*", "reflect-metadata": "^0.1.13", @@ -52,6 +53,7 @@ "@types/node": "^17.0.21", "@types/passport-jwt": "^3.0.6", "@types/passport-local": "^1.0.34", + "@types/passport-strategy": "^0.2.35", "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^5.13.0", "@typescript-eslint/parser": "^5.13.0", diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index da25673..7fa9015 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -10,6 +10,7 @@ import { DemoManagerModule } from './managers/demo/demomanager.module'; import { AuthModule } from './routes/api/auth/auth.module'; import { PrefModule } from './routes/api/pref/pref.module'; import { ImageModule } from './routes/image/imageroute.module'; +import { ExperimentModule } from './routes/api/experiment/experiment.module'; @Module({ imports: [ @@ -27,6 +28,7 @@ import { ImageModule } from './routes/image/imageroute.module'; DemoManagerModule, PrefModule, PicsurLoggerModule, + ExperimentModule, ], }) export class AppModule {} diff --git a/backend/src/decorators/authenticated.ts b/backend/src/decorators/authenticated.ts index 5430f7f..278e22f 100644 --- a/backend/src/decorators/authenticated.ts +++ b/backend/src/decorators/authenticated.ts @@ -1,9 +1,9 @@ import { CanActivate, UseGuards } from '@nestjs/common'; import { AdminGuard } from '../managers/auth/guards/admin.guard'; -import { JwtAuthGuard } from '../managers/auth/guards/jwt.guard'; +import { MainAuthGuard } from '../managers/auth/guards/main.guard'; export const Authenticated = (adminOnly: boolean = false) => { - const guards: (Function | CanActivate)[] = [JwtAuthGuard]; + const guards: (Function | CanActivate)[] = [MainAuthGuard]; if (adminOnly) guards.push(AdminGuard); return UseGuards(...guards); diff --git a/backend/src/managers/auth/auth.module.ts b/backend/src/managers/auth/auth.module.ts index 98c5b6d..2dba37a 100644 --- a/backend/src/managers/auth/auth.module.ts +++ b/backend/src/managers/auth/auth.module.ts @@ -10,6 +10,7 @@ import { } from '../../config/jwt.lateconfig.service'; import { PicsurLateConfigModule } from '../../config/lateconfig.module'; import { AuthManagerService } from './auth.service'; +import { GuestStrategy } from './guards/guest.strategy'; import { JwtStrategy } from './guards/jwt.strategy'; import { LocalAuthStrategy } from './guards/localauth.strategy'; @@ -28,6 +29,7 @@ import { LocalAuthStrategy } from './guards/localauth.strategy'; AuthManagerService, LocalAuthStrategy, JwtStrategy, + GuestStrategy, JwtSecretProvider, ], exports: [AuthManagerService], diff --git a/backend/src/managers/auth/guards/guest.strategy.ts b/backend/src/managers/auth/guards/guest.strategy.ts new file mode 100644 index 0000000..6fd0c57 --- /dev/null +++ b/backend/src/managers/auth/guards/guest.strategy.ts @@ -0,0 +1,39 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Request } from 'express'; +import { ParamsDictionary } from 'express-serve-static-core'; +import { Strategy } from 'passport-strategy'; +import { ParsedQs } from 'qs'; + +type ReqType = Request< + ParamsDictionary, + any, + any, + ParsedQs, + Record +>; + +class GuestPassportStrategy extends Strategy { + async validate(req: ReqType): Promise { + return undefined; + } + + override authenticate(req: ReqType, options?: any): void { + const user = this.validate(req); + req['user'] = user; + this.pass(); + } +} + +@Injectable() +export class GuestStrategy extends PassportStrategy( + GuestPassportStrategy, + 'guest', +) { + private readonly logger = new Logger('GuestStrategy'); + + override async validate(payload: any) { + // TODO: add guest user + return; + } +} diff --git a/backend/src/managers/auth/guards/jwt.guard.ts b/backend/src/managers/auth/guards/main.guard.ts similarity index 61% rename from backend/src/managers/auth/guards/jwt.guard.ts rename to backend/src/managers/auth/guards/main.guard.ts index 2155290..c4924ad 100644 --- a/backend/src/managers/auth/guards/jwt.guard.ts +++ b/backend/src/managers/auth/guards/main.guard.ts @@ -2,4 +2,4 @@ import { Injectable } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @Injectable() -export class JwtAuthGuard extends AuthGuard('jwt') {} +export class MainAuthGuard extends AuthGuard(['jwt', 'guest']) {} diff --git a/backend/src/routes/api/experiment/experiment.controller.ts b/backend/src/routes/api/experiment/experiment.controller.ts new file mode 100644 index 0000000..f28aadf --- /dev/null +++ b/backend/src/routes/api/experiment/experiment.controller.ts @@ -0,0 +1,15 @@ +import { Controller, Get, Request, UseGuards } from '@nestjs/common'; +import { MainAuthGuard } from '../../../managers/auth/guards/main.guard'; +import AuthFasityRequest from '../../../models/dto/authrequest.dto'; + +@Controller('api/experiment') +export class ExperimentController { + @Get() + @UseGuards(MainAuthGuard) + async testRoute(@Request() req: AuthFasityRequest) { + console.log("calledroutes") + return { + message: req.user, + }; + } +} diff --git a/backend/src/routes/api/experiment/experiment.module.ts b/backend/src/routes/api/experiment/experiment.module.ts new file mode 100644 index 0000000..2b88b0d --- /dev/null +++ b/backend/src/routes/api/experiment/experiment.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { ExperimentController } from './experiment.controller'; + +@Module({ + controllers: [ExperimentController] +}) +export class ExperimentModule {} diff --git a/yarn.lock b/yarn.lock index 54ec7a2..fbba8ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1814,7 +1814,7 @@ "@types/passport" "*" "@types/passport-strategy" "*" -"@types/passport-strategy@*": +"@types/passport-strategy@*", "@types/passport-strategy@^0.2.35": version "0.2.35" resolved "https://registry.yarnpkg.com/@types/passport-strategy/-/passport-strategy-0.2.35.tgz#e52f5212279ea73f02d9b06af67efe9cefce2d0c" integrity sha512-o5D19Jy2XPFoX2rKApykY15et3Apgax00RRLf0RUotPDUsYrQa7x4howLYr9El2mlUApHmCMv5CZ1IXqKFQ2+g==