From 9c22bfd1e808f42cc187d273643c68d389652aa2 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Mon, 20 Dec 2021 12:06:05 +1000 Subject: [PATCH] Move captchaToken to passwordTokenRequest subclass --- .../models/request/identityToken/apiTokenRequest.ts | 3 +-- .../request/identityToken/passwordTokenRequest.ts | 13 +++++++++---- .../models/request/identityToken/ssoTokenRequest.ts | 3 +-- .../models/request/identityToken/tokenRequest.ts | 7 +------ common/src/services/auth.service.ts | 10 ++-------- spec/common/services/auth.service.spec.ts | 3 +-- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/common/src/models/request/identityToken/apiTokenRequest.ts b/common/src/models/request/identityToken/apiTokenRequest.ts index 00352175..9f42108e 100644 --- a/common/src/models/request/identityToken/apiTokenRequest.ts +++ b/common/src/models/request/identityToken/apiTokenRequest.ts @@ -7,10 +7,9 @@ export class ApiTokenRequest extends TokenRequest { public clientId: string, public clientSecret: string, protected twoFactor: TwoFactorData, - captchaResponse: string, device?: DeviceRequest ) { - super(twoFactor, captchaResponse, device); + super(twoFactor, device); } toIdentityToken(clientId: string) { diff --git a/common/src/models/request/identityToken/passwordTokenRequest.ts b/common/src/models/request/identityToken/passwordTokenRequest.ts index 2047ebb9..14cf1a78 100644 --- a/common/src/models/request/identityToken/passwordTokenRequest.ts +++ b/common/src/models/request/identityToken/passwordTokenRequest.ts @@ -1,18 +1,19 @@ import { TokenRequest, TwoFactorData } from "./tokenRequest"; +import { CaptchaProtectedRequest } from '../captchaProtectedRequest'; import { DeviceRequest } from "../deviceRequest"; import { Utils } from "../../../misc/utils"; -export class PasswordTokenRequest extends TokenRequest { +export class PasswordTokenRequest extends TokenRequest implements CaptchaProtectedRequest { constructor( public email: string, - public masterPasswordHash: string, + private masterPasswordHash: string, + public captchaResponse: string, protected twoFactor: TwoFactorData, - captchaResponse: string, device?: DeviceRequest ) { - super(twoFactor, captchaResponse, device); + super(twoFactor, device); } toIdentityToken(clientId: string) { @@ -22,6 +23,10 @@ export class PasswordTokenRequest extends TokenRequest { obj.username = this.email; obj.password = this.masterPasswordHash; + if (this.captchaResponse != null) { + obj.captchaResponse = this.captchaResponse; + } + return obj; } diff --git a/common/src/models/request/identityToken/ssoTokenRequest.ts b/common/src/models/request/identityToken/ssoTokenRequest.ts index 0d455ed9..e7dfbe90 100644 --- a/common/src/models/request/identityToken/ssoTokenRequest.ts +++ b/common/src/models/request/identityToken/ssoTokenRequest.ts @@ -8,10 +8,9 @@ export class SsoTokenRequest extends TokenRequest { public codeVerifier: string, public redirectUri: string, protected twoFactor: TwoFactorData, - captchaResponse: string, device?: DeviceRequest ) { - super(twoFactor, captchaResponse, device); + super(twoFactor, device); } toIdentityToken(clientId: string) { diff --git a/common/src/models/request/identityToken/tokenRequest.ts b/common/src/models/request/identityToken/tokenRequest.ts index 92255c0d..1399a023 100644 --- a/common/src/models/request/identityToken/tokenRequest.ts +++ b/common/src/models/request/identityToken/tokenRequest.ts @@ -9,12 +9,11 @@ export interface TwoFactorData { remember: boolean; } -export abstract class TokenRequest implements CaptchaProtectedRequest { +export abstract class TokenRequest { protected device?: DeviceRequest; constructor( protected twoFactor: TwoFactorData, - public captchaResponse: string, device?: DeviceRequest ) { this.device = device != null ? device : null; @@ -40,10 +39,6 @@ export abstract class TokenRequest implements CaptchaProtectedRequest { obj.twoFactorRemember = this.twoFactor.remember ? "1" : "0"; } - if (this.captchaResponse != null) { - obj.captchaResponse = this.captchaResponse; - } - return obj; } diff --git a/common/src/services/auth.service.ts b/common/src/services/auth.service.ts index 99535db5..a639052b 100644 --- a/common/src/services/auth.service.ts +++ b/common/src/services/auth.service.ts @@ -76,8 +76,8 @@ export class AuthService implements AuthServiceAbstraction { tokenRequest = new PasswordTokenRequest( email, hashedPassword, - await this.createTwoFactorData(twoFactor), captchaToken, + await this.createTwoFactorData(twoFactor), await this.createDeviceRequest() ); } else { @@ -124,7 +124,6 @@ export class AuthService implements AuthServiceAbstraction { codeVerifier, redirectUrl, await this.createTwoFactorData(twoFactor), - null, await this.createDeviceRequest() ); } else { @@ -178,7 +177,6 @@ export class AuthService implements AuthServiceAbstraction { clientId, clientSecret, await this.createTwoFactorData(twoFactor), - null, await this.createDeviceRequest() ); } else { @@ -266,7 +264,7 @@ export class AuthService implements AuthServiceAbstraction { private async processTokenResponse( response: IdentityTokenResponse | IdentityTwoFactorResponse | IdentityCaptchaResponse, - newSsoUser?: boolean, + newSsoUser: boolean = false, ): Promise { this.clearState(); const result = new AuthResult(); @@ -393,8 +391,4 @@ export class AuthService implements AuthServiceAbstraction { this.localHashedPassword = null; this.key = null; } - - private isNewSsoUser(code: string, key: string) { - return code != null && key == null; - } } diff --git a/spec/common/services/auth.service.spec.ts b/spec/common/services/auth.service.spec.ts index d31d8df5..effee700 100644 --- a/spec/common/services/auth.service.spec.ts +++ b/spec/common/services/auth.service.spec.ts @@ -426,8 +426,7 @@ describe("Cipher Service", () => { ssoTokenRequest.redirectUri === ssoRedirectUrl && ssoTokenRequest.device.identifier === deviceId && ssoTokenRequest.twoFactor.provider == null && - ssoTokenRequest.twoFactor.token == null && - actual.captchaResponse == null + ssoTokenRequest.twoFactor.token == null ); }) );