refactor(auth): add workspaces selection (#12098)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
|
||||
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';
|
||||
import { FilePayloadToEncode } from 'src/engine/core-modules/file/services/file.service';
|
||||
import { extractFileInfoFromRequest } from 'src/engine/core-modules/file/utils/extract-file-info-from-request.utils';
|
||||
import {
|
||||
FileTokenJwtPayload,
|
||||
JwtTokenTypeEnum,
|
||||
} from 'src/engine/core-modules/auth/types/auth-context.type';
|
||||
|
||||
@Injectable()
|
||||
export class FilePathGuard implements CanActivate {
|
||||
@@ -19,11 +22,11 @@ export class FilePathGuard implements CanActivate {
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = (await this.jwtWrapperService.verifyWorkspaceToken(
|
||||
const payload = await this.jwtWrapperService.verifyJwtToken(
|
||||
fileSignature,
|
||||
'FILE',
|
||||
JwtTokenTypeEnum.FILE,
|
||||
ignoreExpirationToken ? { ignoreExpiration: true } : {},
|
||||
)) as FilePayloadToEncode;
|
||||
);
|
||||
|
||||
if (
|
||||
!payload.workspaceId ||
|
||||
@@ -36,9 +39,12 @@ export class FilePathGuard implements CanActivate {
|
||||
return false;
|
||||
}
|
||||
|
||||
const decodedPayload = (await this.jwtWrapperService.decode(fileSignature, {
|
||||
json: true,
|
||||
})) as FilePayloadToEncode;
|
||||
const decodedPayload = this.jwtWrapperService.decode<FileTokenJwtPayload>(
|
||||
fileSignature,
|
||||
{
|
||||
json: true,
|
||||
},
|
||||
);
|
||||
|
||||
request.workspaceId = decodedPayload.workspaceId;
|
||||
|
||||
|
||||
@@ -11,11 +11,10 @@ import { FileStorageService } from 'src/engine/core-modules/file-storage/file-st
|
||||
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';
|
||||
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
|
||||
import { extractFilenameFromPath } from 'src/engine/core-modules/file/utils/extract-file-id-from-path.utils';
|
||||
|
||||
export type FilePayloadToEncode = {
|
||||
workspaceId: string;
|
||||
filename: string;
|
||||
};
|
||||
import {
|
||||
FileTokenJwtPayload,
|
||||
JwtTokenTypeEnum,
|
||||
} from 'src/engine/core-modules/auth/types/auth-context.type';
|
||||
|
||||
@Injectable()
|
||||
export class FileService {
|
||||
@@ -52,26 +51,26 @@ export class FileService {
|
||||
});
|
||||
}
|
||||
|
||||
encodeFileToken(payloadToEncode: FilePayloadToEncode) {
|
||||
encodeFileToken(payloadToEncode: Omit<FileTokenJwtPayload, 'type' | 'sub'>) {
|
||||
const fileTokenExpiresIn = this.twentyConfigService.get(
|
||||
'FILE_TOKEN_EXPIRES_IN',
|
||||
);
|
||||
|
||||
const payload: FileTokenJwtPayload = {
|
||||
...payloadToEncode,
|
||||
sub: payloadToEncode.workspaceId,
|
||||
type: JwtTokenTypeEnum.FILE,
|
||||
};
|
||||
|
||||
const secret = this.jwtWrapperService.generateAppSecret(
|
||||
'FILE',
|
||||
payload.type,
|
||||
payloadToEncode.workspaceId,
|
||||
);
|
||||
|
||||
const signedPayload = this.jwtWrapperService.sign(
|
||||
{
|
||||
...payloadToEncode,
|
||||
},
|
||||
{
|
||||
secret,
|
||||
expiresIn: fileTokenExpiresIn,
|
||||
},
|
||||
);
|
||||
|
||||
return signedPayload;
|
||||
return this.jwtWrapperService.sign(payload, {
|
||||
secret,
|
||||
expiresIn: fileTokenExpiresIn,
|
||||
});
|
||||
}
|
||||
|
||||
async deleteFile({
|
||||
|
||||
Reference in New Issue
Block a user