Skip to content

Commit c81aeba

Browse files
committed
Another round of typecheck (take #2)
Signed-off-by: Eric Le Ponner <eric.leponner@icloud.com>
1 parent df551e5 commit c81aeba

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

back-end/apps/api/src/auth/auth.service.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export class AuthService {
6666

6767
this.logger.log(`User ${user.id} registered and temporary password generated.`);
6868

69-
emitUserRegistrationEmail(this.notificationsPublisher, [{ email: user.email, additionalData: { url, tempPassword, downloadUrl } }]);
69+
emitUserRegistrationEmail(this.notificationsPublisher, [
70+
{ email: user.email, additionalData: { url, tempPassword, downloadUrl } },
71+
]);
7072

7173
return user;
7274
}
@@ -89,7 +91,10 @@ export class AuthService {
8991
if (!correct) throw new BadRequestException(ErrorCodes.INOP);
9092

9193
if (user.status === UserStatus.NEW && user.keys.length === 0) {
92-
emitUserStatusUpdateNotifications(this.notificationsPublisher, { entityId: user.id, additionalData: { username: user.email } });
94+
emitUserStatusUpdateNotifications(this.notificationsPublisher, {
95+
entityId: user.id,
96+
additionalData: { username: user.email },
97+
});
9398
}
9499

95100
await this.usersService.setPassword(user, newPassword);
@@ -104,12 +109,17 @@ export class AuthService {
104109
// A legitimate new request always gets a clean slate of attempts, and
105110
// replaces whatever code (if any) was previously pending.
106111
await this.otpStoreService.resetFailedAttempts(user.email);
107-
if (!secret) return null;
108112

109113
const otp = this.generateOtp();
110-
await this.otpStoreService.storeCodeHash(user.email, this.hashOtp(otp), this.getOtpWindowSeconds());
114+
await this.otpStoreService.storeCodeHash(
115+
user.email,
116+
this.hashOtp(otp),
117+
this.getOtpWindowSeconds(),
118+
);
111119

112-
await emitUserPasswordResetEmail(this.notificationsPublisher, [{ email: user.email, additionalData: { otp } }]);
120+
await emitUserPasswordResetEmail(this.notificationsPublisher, [
121+
{ email: user.email, additionalData: { otp } },
122+
]);
113123

114124
// @deprecated This JWT proves nothing on its own (see OtpJwtStrategy) - it's
115125
// only issued so pre-existing clients that still send it back as the `otp`
@@ -118,7 +128,7 @@ export class AuthService {
118128
// pair, not this token.
119129
const token = this.getOtpToken(
120130
{ email: user.email, verified: false },
121-
this.configService.get<number>('OTP_EXPIRATION'),
131+
this.configService.get<number>('OTP_EXPIRATION') ?? 2,
122132
);
123133
return { token };
124134
}
@@ -137,7 +147,7 @@ export class AuthService {
137147

138148
// Atomically checks-and-deletes so the same code can never be redeemed twice,
139149
// even by two requests racing each other.
140-
const matched = await this.otpStoreService.consumeCodeHashIfMatch(email, tokenHash) ?? '';
150+
const matched = await this.otpStoreService.consumeCodeHashIfMatch(email, tokenHash);
141151

142152
if (!matched) {
143153
const attempts = await this.otpStoreService.registerFailedAttempt(email, windowSeconds);
@@ -207,18 +217,18 @@ export class AuthService {
207217
* the failed-attempt counter, since it doesn't need to outlive the code it's
208218
* protecting. */
209219
private getOtpWindowSeconds(): number {
210-
return this.configService.get<number>('OTP_EXPIRATION') * 60;
220+
return this.configService.get<number>('OTP_EXPIRATION')! * 60;
211221
}
212222

213223
private getOtpMaxAttempts(): number {
214-
return this.configService.get<number>('OTP_MAX_ATTEMPTS');
224+
return this.configService.get<number>('OTP_MAX_ATTEMPTS')!;
215225
}
216226

217227
/* How long the verified OTP JWT stays valid, in minutes. Independent of the
218228
* guessing window above - by this point the code is already spent, so this is
219229
* just giving the user reasonable time to submit their new password. */
220230
private getOtpVerifiedExpirationMinutes(): number {
221-
return this.configService.get<number>('OTP_VERIFIED_EXPIRATION');
231+
return this.configService.get<number>('OTP_VERIFIED_EXPIRATION')!;
222232
}
223233

224234
/* Sets the OTP jwt */

0 commit comments

Comments
 (0)