@@ -13,25 +13,25 @@ import { JwtService } from '@nestjs/jwt';
1313import { Authenticator } from '@otplib/core' ;
1414import { emails , users } from '@prisma/client' ;
1515import { compare , hash } from 'bcrypt' ;
16+ import anonymize from 'ip-anonymize' ;
1617import { authenticator } from 'otplib' ;
1718import qrcode from 'qrcode' ;
1819import { safeEmail } from 'src/helpers/safe-email' ;
20+ import { ApprovedSubnetsService } from '../approved-subnets/approved-subnets.service' ;
1921import { EmailService } from '../email/email.service' ;
22+ import { GeolocationService } from '../geolocation/geolocation.service' ;
2023import { Expose } from '../prisma/prisma.interface' ;
2124import { PrismaService } from '../prisma/prisma.service' ;
2225import { PwnedService } from '../pwned/pwned.service' ;
2326import {
27+ APPROVE_SUBNET_TOKEN ,
2428 EMAIL_VERIFY_TOKEN ,
2529 PASSWORD_RESET_TOKEN ,
2630 TWO_FACTOR_TOKEN ,
27- APPROVE_SUBNET_TOKEN ,
2831} from '../tokens/tokens.constants' ;
2932import { TokensService } from '../tokens/tokens.service' ;
3033import { RegisterDto } from './auth.dto' ;
31- import { AccessTokenClaims } from './auth.interface' ;
32- import anonymize from 'ip-anonymize' ;
33- import { GeolocationService } from '../geolocation/geolocation.service' ;
34- import { ApprovedSubnetsService } from '../approved-subnets/approved-subnets.service' ;
34+ import { AccessTokenClaims , TokenResponse } from './auth.interface' ;
3535
3636@Injectable ( )
3737export class AuthService {
@@ -184,7 +184,11 @@ export class AuthService {
184184 return { queued : true } ;
185185 }
186186
187- async refresh ( ipAddress : string , userAgent : string , token : string ) {
187+ async refresh (
188+ ipAddress : string ,
189+ userAgent : string ,
190+ token : string ,
191+ ) : Promise < TokenResponse > {
188192 if ( ! token ) throw new UnprocessableEntityException ( ) ;
189193 const session = await this . prisma . sessions . findFirst ( {
190194 where : { token } ,
@@ -215,6 +219,13 @@ export class AuthService {
215219 } ) ;
216220 }
217221
222+ async approveSubnet ( ipAddress : string , userAgent : string , token : string ) {
223+ if ( ! token ) throw new UnprocessableEntityException ( ) ;
224+ const id = this . tokensService . verify < number > ( APPROVE_SUBNET_TOKEN , token ) ;
225+ await this . approvedSubnetsService . approveNewSubnet ( id , ipAddress ) ;
226+ return this . loginResponse ( ipAddress , userAgent , id ) ;
227+ }
228+
218229 /** Get the two-factor authentication QR code */
219230 async getTotpQrCode ( userId : number ) {
220231 const secret = randomStringGenerator ( ) ;
@@ -359,7 +370,7 @@ export class AuthService {
359370 ipAddress : string ,
360371 userAgent : string ,
361372 id : number ,
362- ) {
373+ ) : Promise < TokenResponse > {
363374 const token = randomStringGenerator ( ) ;
364375 await this . prisma . sessions . create ( {
365376 data : { token, ipAddress, userAgent, user : { connect : { id } } } ,
0 commit comments