Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/authorization/auth-with-api.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BadRequestException,
HttpException,
Injectable,
InternalServerErrorException,
Expand All @@ -15,6 +14,7 @@ import { Repository } from 'typeorm';
import { JwtScopesEnum } from '../entities/user/enums/jwt-scopes.enum.js';
import { UserEntity } from '../entities/user/user.entity.js';
import { EncryptionAlgorithmEnum } from '../enums/encryption-algorithm.enum.js';
import { TwoFaRequiredException } from '../exceptions/custom-exceptions/two-fa-required-exception.js';
import { Messages } from '../exceptions/text/messages.js';
import { Constants } from '../helpers/constants/constants.js';
import { Encryptor } from '../helpers/encryption/encryptor.js';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
const addedScope: Array<JwtScopesEnum> = data.scope;
if (addedScope && addedScope.length > 0) {
if (addedScope.includes(JwtScopesEnum.TWO_FA_ENABLE)) {
throw new BadRequestException(Messages.TWO_FA_REQUIRED);
throw new TwoFaRequiredException();
}
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/authorization/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BadRequestException,
HttpException,
Injectable,
InternalServerErrorException,
Expand All @@ -14,6 +13,7 @@ import { Repository } from 'typeorm';
import { LogOutEntity } from '../entities/log-out/log-out.entity.js';
import { JwtScopesEnum } from '../entities/user/enums/jwt-scopes.enum.js';
import { UserEntity } from '../entities/user/user.entity.js';
import { TwoFaRequiredException } from '../exceptions/custom-exceptions/two-fa-required-exception.js';
import { Messages } from '../exceptions/text/messages.js';
import { isTest } from '../helpers/app/is-test.js';
import { Constants } from '../helpers/constants/constants.js';
Expand Down Expand Up @@ -72,7 +72,7 @@ export class AuthMiddleware implements NestMiddleware {
const addedScope: Array<JwtScopesEnum> = data.scope;
if (addedScope && addedScope.length > 0) {
if (addedScope.includes(JwtScopesEnum.TWO_FA_ENABLE)) {
throw new BadRequestException(Messages.TWO_FA_REQUIRED);
throw new TwoFaRequiredException();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BadRequestException, Inject, Injectable, Scope } from '@nestjs/common';
import { HttpStatus, Inject, Injectable, Scope } from '@nestjs/common';
import Sentry from '@sentry/minimal';
import { Response } from 'express';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { getErrorMessage } from '../../../helpers/get-error-message.js';
import { SharedJobsService } from '../../shared-jobs/shared-jobs.service.js';
import { IAISettingsAndWidgetsCreation } from '../ai-use-cases.interface.js';
Expand All @@ -28,7 +28,7 @@ export class RequestAISettingsAndWidgetsCreationUseCase

const connection = await this._dbContext.connectionRepository.findAndDecryptConnection(connectionId, masterPwd);
if (!connection) {
throw new BadRequestException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}

this.setupResponseHeaders(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { BaseMessage } from '@langchain/core/messages';
import {
BadRequestException,
ForbiddenException,
Inject,
Injectable,
Logger,
NotFoundException,
Scope,
} from '@nestjs/common';
import { BadRequestException, ForbiddenException, HttpStatus, Inject, Injectable, Logger, Scope } from '@nestjs/common';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import { ConnectionTypesEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js';
import { IDataAccessObject } from '@rocketadmin/shared-code/dist/src/shared/interfaces/data-access-object.interface.js';
Expand All @@ -32,6 +24,7 @@ import { encodeError, encodeToToon } from '../../../ai-core/utils/toon-encoder.j
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { getErrorMessage } from '../../../helpers/get-error-message.js';
import { isConnectionTypeAgent } from '../../../helpers/is-connection-entity-agent.js';
Expand Down Expand Up @@ -479,7 +472,7 @@ export class RequestInfoFromTableWithAIUseCaseV7
);

if (!foundConnection) {
throw new NotFoundException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.NOT_FOUND);
}

let userEmail = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as cedarWasm from '@cedar-policy/cedar-wasm/nodejs';
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { IGlobalDatabaseContext } from '../../common/application/global-database-context.interface.js';
import { BaseType } from '../../common/data-injection.tokens.js';
import { AccessLevelEnum } from '../../enums/access-level.enum.js';
import { Messages } from '../../exceptions/text/messages.js';
import { ConnectionNotFoundException } from '../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Cacher } from '../../helpers/cache/cacher.js';
import { GroupEntity } from '../group/group.entity.js';
import { ITablePermissionData } from '../permission/permission.interface.js';
Expand Down Expand Up @@ -368,7 +368,7 @@ export class CedarPermissionsService implements IUserAccessRepository {
async getConnectionId(groupId: string): Promise<string> {
const group = await this.globalDbContext.groupRepository.findGroupByIdWithConnectionAndUsers(groupId);
if (!group?.connection?.id) {
throw new HttpException({ message: Messages.CONNECTION_NOT_FOUND }, HttpStatus.BAD_REQUEST);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
return group.connection.id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { HttpException } from '@nestjs/common/exceptions/http.exception.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Encryptor } from '../../../helpers/encryption/encryptor.js';
import { CreateConnectionPropertiesDs } from '../application/data-structures/create-connection-properties.ds.js';
import { FoundConnectionPropertiesDs } from '../application/data-structures/found-connection-properties.ds.js';
Expand Down Expand Up @@ -31,12 +30,7 @@ export class CreateConnectionPropertiesUseCase
master_password ?? '',
);
if (!foundConnection) {
throw new HttpException(
{
message: Messages.CONNECTION_NOT_FOUND,
},
HttpStatus.NOT_FOUND,
);
throw new ConnectionNotFoundException(HttpStatus.NOT_FOUND);
}
await validateCreateConnectionPropertiesDs(inputData, foundConnection);
const newConnectionProperties = buildConnectionPropertiesEntity(inputData, foundConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpException } from '@nestjs/common/exceptions/http.exception.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { CreateConnectionPropertiesDs } from '../application/data-structures/create-connection-properties.ds.js';
import { FoundConnectionPropertiesDs } from '../application/data-structures/found-connection-properties.ds.js';
Expand Down Expand Up @@ -34,12 +35,7 @@ export class UpdateConnectionPropertiesUseCase
master_password ?? '',
);
if (!foundConnection) {
throw new HttpException(
{
message: Messages.CONNECTION_NOT_FOUND,
},
HttpStatus.NOT_FOUND,
);
throw new ConnectionNotFoundException(HttpStatus.NOT_FOUND);
}
await validateCreateConnectionPropertiesDs(inputData, foundConnection);
const connectionPropertiesToUpdate =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Repository } from 'typeorm';
import { Messages } from '../../../exceptions/text/messages.js';
import {
MasterPasswordIncorrectError,
MasterPasswordMissingError,
} from '../../../exceptions/domain-errors/master-password.errors.js';
import { Constants } from '../../../helpers/constants/constants.js';
import { Encryptor } from '../../../helpers/encryption/encryptor.js';
import { isConnectionTypeAgent } from '../../../helpers/is-connection-entity-agent.js';
Expand Down Expand Up @@ -111,14 +114,14 @@ export const customConnectionRepositoryExtension: IConnectionRepository &
await decryptConnectionCredentialsAsync(connection);

if (connection.masterEncryption && !masterPwd) {
throw new Error(Messages.MASTER_PASSWORD_MISSING);
throw new MasterPasswordMissingError();
}

if (connection.masterEncryption && masterPwd) {
if (connection.master_hash) {
const isMasterPwdCorrect = await Encryptor.verifyUserPassword(masterPwd, connection.master_hash);
if (!isMasterPwdCorrect) {
throw new Error(Messages.MASTER_PASSWORD_INCORRECT);
throw new MasterPasswordIncorrectError();
}
}
connection = Encryptor.decryptConnectionCredentials(connection, masterPwd);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BadRequestException, Inject, Injectable, Scope } from '@nestjs/common';
import { BadRequestException, HttpStatus, Inject, Injectable, Scope } from '@nestjs/common';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { AccessLevelEnum } from '../../../enums/access-level.enum.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { Cacher } from '../../../helpers/cache/cacher.js';
import { generateCedarPolicyForGroup } from '../../cedar-authorization/cedar-policy-generator.js';
Expand Down Expand Up @@ -31,7 +32,7 @@ export class CreateGroupInConnectionUseCase
} = inputData;
const connectionToUpdate = await this._dbContext.connectionRepository.findConnectionWithGroups(connectionId);
if (!connectionToUpdate) {
throw new BadRequestException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
if (connectionToUpdate.groups.find((group) => group.title === title)) {
throw new BadRequestException(Messages.GROUP_NAME_UNIQUE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BadRequestException, Inject, Injectable, NotFoundException, Scope } from '@nestjs/common';
import { BadRequestException, HttpStatus, Inject, Injectable, Scope } from '@nestjs/common';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { DeleteConnectionDs } from '../application/data-structures/delete-connection.ds.js';
import { CreatedConnectionDTO } from '../application/dto/created-connection.dto.js';
Expand All @@ -26,7 +27,7 @@ export class DeleteConnectionUseCase
inputData.masterPwd,
);
if (!connectionToDelete) {
throw new NotFoundException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.NOT_FOUND);
}
const userNonTestConnections = await this._dbContext.connectionRepository.findAllUserNonTestsConnections(
inputData.cognitoUserName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { BadRequestException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { HttpException } from '@nestjs/common/exceptions/http.exception.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { AccessLevelEnum } from '../../../enums/access-level.enum.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { MasterPasswordIncorrectException } from '../../../exceptions/custom-exceptions/master-password-incorrect-exception.js';
import { MasterPasswordMissingException } from '../../../exceptions/custom-exceptions/master-password-missing-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { Constants } from '../../../helpers/constants/constants.js';
import { Encryptor } from '../../../helpers/encryption/encryptor.js';
Expand Down Expand Up @@ -33,33 +36,21 @@ export class FindOneConnectionUseCase
protected async implementation(inputData: FindOneConnectionDs): Promise<FoundOneConnectionDs> {
const connection = await this._dbContext.connectionRepository.findOneConnection(inputData.connectionId);
if (!connection) {
throw new BadRequestException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
const accessLevel: AccessLevelEnum = await this.cedarPermissions.getUserConnectionAccessLevel(
inputData.cognitoUserName,
inputData.connectionId,
);

if (connection.masterEncryption && !inputData.masterPwd) {
throw new HttpException(
{
message: Messages.MASTER_PASSWORD_MISSING,
type: 'no_master_key',
},
HttpStatus.BAD_REQUEST,
);
throw new MasterPasswordMissingException();
}

if (connection.masterEncryption && inputData.masterPwd) {
const isMaterPwdValid = await Encryptor.verifyUserPassword(inputData.masterPwd, connection.master_hash ?? '');
if (!isMaterPwdValid) {
throw new HttpException(
{
message: Messages.MASTER_PASSWORD_INCORRECT,
type: 'invalid_master_key',
},
HttpStatus.BAD_REQUEST,
);
throw new MasterPasswordIncorrectException();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, HttpException, HttpStatus, Inject, Injectable, Scope } from '@nestjs/common';
import { BadRequestException, HttpStatus, Inject, Injectable, Scope } from '@nestjs/common';
import { validateSchemaCache } from '@rocketadmin/shared-code/dist/src/caching/schema-cache-validator.js';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import { ForeignKeyDS } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/data-structures/foreign-key.ds.js';
Expand All @@ -8,6 +8,7 @@ import { ConnectionTypesEnum } from '@rocketadmin/shared-code/dist/src/shared/en
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { ExceptionOperations } from '../../../exceptions/custom-exceptions/exception-operation.js';
import { UnknownSQLException } from '../../../exceptions/custom-exceptions/unknown-sql-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
Expand Down Expand Up @@ -35,7 +36,7 @@ export class GetConnectionDiagramUseCase
const { connectionId, masterPwd, userId } = inputData;
const connection = await this._dbContext.connectionRepository.findAndDecryptConnection(connectionId, masterPwd);
if (!connection) {
throw new HttpException({ message: Messages.CONNECTION_NOT_FOUND }, HttpStatus.BAD_REQUEST);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
if (!isSqlConnectionType(connection.type)) {
throw new BadRequestException(Messages.DIAGRAM_NOT_SUPPORTED_FOR_CONNECTION_TYPE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { AccessLevelEnum } from '../../../enums/access-level.enum.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { parseCedarPolicyToClassicalPermissions } from '../../cedar-authorization/cedar-policy-parser.js';
import { TablePermissionDs } from '../../permission/application/data-structures/create-permissions.ds.js';
import { FoundPermissionsInConnectionDs } from '../application/data-structures/found-permissions-in-connection.ds.js';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class GetPermissionsForGroupInConnectionUseCase
inputData.masterPwd,
);
if (!connection) {
throw new BadRequestException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
const dao = getDataAccessObject(connection);
const tables: Array<string> = (await dao.getTablesFromDB()).map((table) => table.tableName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { ConnectionNotFoundException } from '../../../exceptions/custom-exceptions/connection-not-found-exception.js';
import { CedarPermissionsService } from '../../cedar-authorization/cedar-permissions.service.js';
import { TablePermissionDs } from '../../permission/application/data-structures/create-permissions.ds.js';
import { FoundPermissionsInConnectionDs } from '../application/data-structures/found-permissions-in-connection.ds.js';
Expand Down Expand Up @@ -33,7 +33,7 @@ export class GetUserPermissionsForGroupInConnectionUseCase

const connection = await this._dbContext.connectionRepository.findAndDecryptConnection(connectionId, masterPwd);
if (!connection) {
throw new BadRequestException(Messages.CONNECTION_NOT_FOUND);
throw new ConnectionNotFoundException(HttpStatus.BAD_REQUEST);
}
const dao = getDataAccessObject(connection);
const tables: Array<string> = (await dao.getTablesFromDB()).map((table) => table.tableName);
Expand Down
Loading
Loading