chore(server): auth request type (#2502)

This commit is contained in:
Jason Rasmussen 2023-05-20 21:44:26 -04:00 committed by GitHub
parent 02b8b2c125
commit 3d426b55d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 25 deletions

View file

@ -1,5 +1,6 @@
import { Request } from 'express';
import * as fs from 'fs';
import { AuthRequest } from '../decorators/auth-user.decorator';
import { multerUtils } from './asset-upload.config';
const { fileFilter, destination, filename } = multerUtils;
@ -14,7 +15,7 @@ const mock = {
deviceId: 'test-device',
fileExtension: '.jpg',
},
} as Request,
} as AuthRequest,
file: { originalname: 'test.jpg' } as Express.Multer.File,
};

View file

@ -2,12 +2,11 @@ import { StorageCore, StorageFolder } from '@app/domain/storage';
import { BadRequestException, Logger, UnauthorizedException } from '@nestjs/common';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import { createHash, randomUUID } from 'crypto';
import { Request } from 'express';
import { existsSync, mkdirSync } from 'fs';
import { diskStorage, StorageEngine } from 'multer';
import { extname } from 'path';
import sanitize from 'sanitize-filename';
import { AuthUserDto } from '../decorators/auth-user.decorator';
import { AuthRequest, AuthUserDto } from '../decorators/auth-user.decorator';
import { patchFormData } from '../utils/path-form-data.util';
export interface ImmichFile extends Express.Multer.File {
@ -50,7 +49,7 @@ export const multerUtils = { fileFilter, filename, destination };
const logger = new Logger('AssetUploadConfig');
function fileFilter(req: Request, file: any, cb: any) {
function fileFilter(req: AuthRequest, file: any, cb: any) {
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
return cb(new UnauthorizedException());
}
@ -66,7 +65,7 @@ function fileFilter(req: Request, file: any, cb: any) {
}
}
function destination(req: Request, file: Express.Multer.File, cb: any) {
function destination(req: AuthRequest, file: Express.Multer.File, cb: any) {
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
return cb(new UnauthorizedException());
}
@ -82,7 +81,7 @@ function destination(req: Request, file: Express.Multer.File, cb: any) {
cb(null, uploadFolder);
}
function filename(req: Request, file: Express.Multer.File, cb: any) {
function filename(req: AuthRequest, file: Express.Multer.File, cb: any) {
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
return cb(new UnauthorizedException());
}

View file

@ -1,5 +1,6 @@
import { Request } from 'express';
import * as fs from 'fs';
import { AuthRequest } from '../decorators/auth-user.decorator';
import { multerUtils } from './profile-image-upload.config';
const { fileFilter, destination, filename } = multerUtils;
@ -10,7 +11,7 @@ const mock = {
user: {
id: 'test-user',
},
} as Request,
} as AuthRequest,
file: { originalname: 'test.jpg' } as Express.Multer.File,
};

View file

@ -1,12 +1,11 @@
import { StorageCore, StorageFolder } from '@app/domain/storage';
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import { Request } from 'express';
import { existsSync, mkdirSync } from 'fs';
import { diskStorage } from 'multer';
import { extname } from 'path';
import sanitize from 'sanitize-filename';
import { AuthUserDto } from '../decorators/auth-user.decorator';
import { AuthRequest, AuthUserDto } from '../decorators/auth-user.decorator';
import { patchFormData } from '../utils/path-form-data.util';
export const profileImageUploadOption: MulterOptions = {
@ -21,7 +20,7 @@ export const multerUtils = { fileFilter, filename, destination };
const storageCore = new StorageCore();
function fileFilter(req: Request, file: any, cb: any) {
function fileFilter(req: AuthRequest, file: any, cb: any) {
if (!req.user) {
return cb(new UnauthorizedException());
}
@ -33,7 +32,7 @@ function fileFilter(req: Request, file: any, cb: any) {
}
}
function destination(req: Request, file: Express.Multer.File, cb: any) {
function destination(req: AuthRequest, file: Express.Multer.File, cb: any) {
if (!req.user) {
return cb(new UnauthorizedException());
}
@ -48,7 +47,7 @@ function destination(req: Request, file: Express.Multer.File, cb: any) {
cb(null, profileImageLocation);
}
function filename(req: Request, file: Express.Multer.File, cb: any) {
function filename(req: AuthRequest, file: Express.Multer.File, cb: any) {
if (!req.user) {
return cb(new UnauthorizedException());
}

View file

@ -1,8 +1,13 @@
export { AuthUserDto } from '@app/domain';
import { AuthUserDto, LoginDetails } from '@app/domain';
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Request } from 'express';
import { UAParser } from 'ua-parser-js';
export interface AuthRequest extends Request {
user?: AuthUserDto;
}
export const GetAuthUser = createParamDecorator((data, ctx: ExecutionContext): AuthUserDto => {
return ctx.switchToHttp().getRequest<{ user: AuthUserDto }>().user;
});

View file

@ -1,11 +0,0 @@
import { AuthUserDto } from './decorators/auth-user.decorator';
declare global {
namespace Express {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface User extends AuthUserDto {}
export interface Request {
user: AuthUserDto;
}
}
}

View file

@ -1,7 +1,7 @@
import { AuthService } from '@app/domain';
import { CanActivate, ExecutionContext, Injectable, Logger } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Request } from 'express';
import { AuthRequest } from '../decorators/auth-user.decorator';
import { Metadata } from '../decorators/authenticated.decorator';
@Injectable()
@ -21,7 +21,7 @@ export class AuthGuard implements CanActivate {
return true;
}
const req = context.switchToHttp().getRequest<Request>();
const req = context.switchToHttp().getRequest<AuthRequest>();
const authDto = await this.authService.validate(req.headers, req.query as Record<string, string>);
if (!authDto) {