Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Don't use HttpException
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 8e248ff commit 402ce14
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 148 deletions.
30 changes: 10 additions & 20 deletions src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
Expand Down Expand Up @@ -103,17 +102,15 @@ export class ApiKeysService {
const apiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!apiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!apiKey) throw new NotFoundException('API key not found');
if (apiKey.groupId !== groupId) throw new UnauthorizedException();
return this.prisma.expose<apiKeys>(apiKey);
}
async getApiKeyForUser(userId: number, id: number): Promise<Expose<apiKeys>> {
const apiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!apiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!apiKey) throw new NotFoundException('API key not found');
if (apiKey.userId !== userId) throw new UnauthorizedException();
return this.prisma.expose<apiKeys>(apiKey);
}
Expand All @@ -122,8 +119,7 @@ export class ApiKeysService {
const apiKey = await this.prisma.apiKeys.findFirst({
where: { apiKey: key },
});
if (!apiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!apiKey) throw new NotFoundException('API key not found');
if (this.lru.has(key)) return this.lru.get(key);
this.lru.set(key, apiKey);
return this.prisma.expose<apiKeys>(apiKey);
Expand All @@ -137,8 +133,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.groupId !== groupId) throw new UnauthorizedException();
data.scopes = this.cleanScopesForGroup(groupId, data.scopes);
const apiKey = await this.prisma.apiKeys.update({
Expand All @@ -156,8 +151,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.userId !== userId) throw new UnauthorizedException();
data.scopes = this.cleanScopesForUser(userId, data.scopes);
const apiKey = await this.prisma.apiKeys.update({
Expand All @@ -176,8 +170,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.groupId !== groupId) throw new UnauthorizedException();
data.scopes = this.cleanScopesForGroup(groupId, data.scopes);
const apiKey = await this.prisma.apiKeys.update({
Expand All @@ -195,8 +188,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.userId !== userId) throw new UnauthorizedException();
data.scopes = this.cleanScopesForUser(userId, data.scopes);
const apiKey = await this.prisma.apiKeys.update({
Expand All @@ -214,8 +206,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.groupId !== groupId) throw new UnauthorizedException();
const apiKey = await this.prisma.apiKeys.delete({
where: { id },
Expand All @@ -230,8 +221,7 @@ export class ApiKeysService {
const testApiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
if (!testApiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (!testApiKey) throw new NotFoundException('API key not found');
if (testApiKey.userId !== userId) throw new UnauthorizedException();
const apiKey = await this.prisma.apiKeys.delete({
where: { id },
Expand Down
17 changes: 8 additions & 9 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
approvedSubnets,
approvedSubnetsOrderByInput,
approvedSubnetsWhereInput,
approvedSubnetsWhereUniqueInput,
} from '@prisma/client';
import { Expose } from '../../modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import anonymize from 'ip-anonymize';
import { compare, hash } from 'bcrypt';
import { ConfigService } from '@nestjs/config';
import anonymize from 'ip-anonymize';
import { Expose } from '../../modules/prisma/prisma.interface';
import { GeolocationService } from '../geolocation/geolocation.service';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class ApprovedSubnetsService {
Expand Down Expand Up @@ -56,10 +55,10 @@ export class ApprovedSubnetsService {
where: { id },
});
if (!approvedSubnet)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
throw new NotFoundException('Approved subnet not found');
if (approvedSubnet.userId !== userId) throw new UnauthorizedException();
if (!approvedSubnet)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
throw new NotFoundException('Approved subnet not found');
return this.prisma.expose<approvedSubnets>(approvedSubnet);
}

Expand All @@ -71,7 +70,7 @@ export class ApprovedSubnetsService {
where: { id },
});
if (!testApprovedSubnet)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
throw new NotFoundException('Approved subnet not found');
if (testApprovedSubnet.userId !== userId) throw new UnauthorizedException();
const approvedSubnet = await this.prisma.approvedSubnets.delete({
where: { id },
Expand Down
6 changes: 2 additions & 4 deletions src/modules/audit-logs/audit-logs.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import {
Expand Down Expand Up @@ -42,8 +41,7 @@ export class AuditLogsService {
const auditLog = await this.prisma.auditLogs.findOne({
where: { id },
});
if (!auditLog)
throw new HttpException('AuditLog not found', HttpStatus.NOT_FOUND);
if (!auditLog) throw new NotFoundException('Audit log not found');
if (auditLog.groupId !== groupId) throw new UnauthorizedException();
return this.prisma.expose<auditLogs>(auditLog);
}
Expand Down
38 changes: 11 additions & 27 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
BadRequestException,
HttpException,
HttpStatus,
ConflictException,
Injectable,
NotFoundException,
NotImplementedException,
UnauthorizedException,
UnprocessableEntityException,
} from '@nestjs/common';
Expand Down Expand Up @@ -79,13 +79,12 @@ export class AuthService {
prefersEmail: true,
},
});
if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND);
if (!user) throw new NotFoundException('User not found');
if (!user.emails.find((i) => i.emailSafe === emailSafe)?.isVerified)
throw new UnauthorizedException('This email is not verified');
if (!password || !user.password)
throw new HttpException(
throw new NotImplementedException(
'Logging in without passwords is not supported',
HttpStatus.NOT_IMPLEMENTED,
);
if (!user.prefersEmail)
throw new BadRequestException('User has no email attached to it');
Expand Down Expand Up @@ -114,10 +113,7 @@ export class AuthService {
where: { emails: { some: { emailSafe } } },
});
if (testUser)
throw new HttpException(
'A user with this email already exists',
HttpStatus.CONFLICT,
);
throw new ConflictException('A user with this email already exists');
const ignorePwnedPassword = !!data.ignorePwnedPassword;
delete data.ignorePwnedPassword;

Expand Down Expand Up @@ -187,15 +183,9 @@ export class AuthService {
include: { user: true },
});
if (!emailDetails)
throw new HttpException(
'There is no user for this email',
HttpStatus.NOT_FOUND,
);
throw new NotFoundException('There is no user for this email');
if (emailDetails.isVerified)
throw new HttpException(
'This email is already verified',
HttpStatus.BAD_REQUEST,
);
throw new ConflictException('This email is already verified');
this.email.send({
to: `"${emailDetails.user.name}" <${email}>`,
template: resend
Expand Down Expand Up @@ -226,8 +216,7 @@ export class AuthService {
where: { token },
include: { user: true },
});
if (!session)
throw new HttpException('Session not found', HttpStatus.NOT_FOUND);
if (!session) throw new NotFoundException('Session not found');
await this.prisma.sessions.updateMany({
where: { token },
data: { ipAddress, userAgent },
Expand All @@ -244,8 +233,7 @@ export class AuthService {
where: { token },
select: { id: true, user: { select: { id: true } } },
});
if (!session)
throw new HttpException('Session not found', HttpStatus.NOT_FOUND);
if (!session) throw new NotFoundException('Session not found');
await this.prisma.sessions.delete({
where: { id: session.id },
});
Expand Down Expand Up @@ -348,10 +336,7 @@ export class AuthService {
include: { user: true },
});
if (!emailDetails)
throw new HttpException(
'There is no user for this email',
HttpStatus.NOT_FOUND,
);
throw new NotFoundException('There is no user for this email');
this.email.send({
to: `"${emailDetails.user.name}" <${email}>`,
template: 'auth/password-reset',
Expand Down Expand Up @@ -596,9 +581,8 @@ export class AuthService {
this.configService.get<number>('security.saltRounds') ?? 10,
);
if (!(await this.pwnedService.isPasswordSafe(password)))
throw new HttpException(
throw new BadRequestException(
'This password has been compromised in a data breach.',
HttpStatus.BAD_REQUEST,
);
}
return await hash(
Expand Down
14 changes: 5 additions & 9 deletions src/modules/domains/domains.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
BadRequestException,
HttpException,
HttpStatus,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
Expand All @@ -14,6 +13,7 @@ import {
domainsWhereUniqueInput,
} from '@prisma/client';
import got from 'got';
import { URL } from 'url';
import { DnsService } from '../dns/dns.service';
import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
Expand All @@ -23,7 +23,6 @@ import {
DOMAIN_VERIFICATION_TXT,
} from './domains.constants';
import { DomainVerificationMethods } from './domains.interface';
import { URL } from 'url';

@Injectable()
export class DomainsService {
Expand Down Expand Up @@ -97,8 +96,7 @@ export class DomainsService {
const domain = await this.prisma.domains.findOne({
where: { id },
});
if (!domain)
throw new HttpException('Domain not found', HttpStatus.NOT_FOUND);
if (!domain) throw new NotFoundException('Domain not found');
if (domain.groupId !== groupId) throw new UnauthorizedException();
return this.prisma.expose<domains>(domain);
}
Expand All @@ -111,8 +109,7 @@ export class DomainsService {
const domain = await this.prisma.domains.findOne({
where: { id },
});
if (!domain)
throw new HttpException('Domain not found', HttpStatus.NOT_FOUND);
if (!domain) throw new NotFoundException('Domain not found');
if (domain.groupId !== groupId) throw new UnauthorizedException();
if (method === DOMAIN_VERIFICATION_TXT) {
const txtRecords = await this.dnsService.lookup(domain.domain, 'TXT');
Expand Down Expand Up @@ -146,8 +143,7 @@ export class DomainsService {
const testDomain = await this.prisma.domains.findOne({
where: { id },
});
if (!testDomain)
throw new HttpException('Domain not found', HttpStatus.NOT_FOUND);
if (!testDomain) throw new NotFoundException('Domain not found');
if (testDomain.groupId !== groupId) throw new UnauthorizedException();
const domain = await this.prisma.domains.delete({
where: { id },
Expand Down
9 changes: 3 additions & 6 deletions src/modules/emails/emails.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import {
Expand Down Expand Up @@ -62,8 +61,7 @@ export class EmailsService {
const email = await this.prisma.emails.findOne({
where: { id },
});
if (!email)
throw new HttpException('Email not found', HttpStatus.NOT_FOUND);
if (!email) throw new NotFoundException('Email not found');
if (email.userId !== userId) throw new UnauthorizedException();
return this.prisma.expose<emails>(email);
}
Expand All @@ -72,8 +70,7 @@ export class EmailsService {
const testEmail = await this.prisma.emails.findOne({
where: { id },
});
if (!testEmail)
throw new HttpException('Email not found', HttpStatus.NOT_FOUND);
if (!testEmail) throw new NotFoundException('Email not found');
if (testEmail.userId !== userId) throw new UnauthorizedException();
const email = await this.prisma.emails.delete({
where: { id },
Expand Down
Loading

0 comments on commit 402ce14

Please sign in to comment.