2022-02-01 00:51:32 +01:00
|
|
|
import { ApiService } from "../../abstractions/api.service";
|
|
|
|
import { AppIdService } from "../../abstractions/appId.service";
|
|
|
|
import { AuthService } from "../../abstractions/auth.service";
|
|
|
|
import { CryptoService } from "../../abstractions/crypto.service";
|
|
|
|
import { LogService } from "../../abstractions/log.service";
|
|
|
|
import { MessagingService } from "../../abstractions/messaging.service";
|
|
|
|
import { PlatformUtilsService } from "../../abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "../../abstractions/state.service";
|
|
|
|
import { TokenService } from "../../abstractions/token.service";
|
|
|
|
import { TwoFactorService } from "../../abstractions/twoFactor.service";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { HashPurpose } from "../../enums/hashPurpose";
|
2022-03-03 02:47:57 +01:00
|
|
|
import { AuthResult } from "../../models/domain/authResult";
|
2022-02-01 00:51:32 +01:00
|
|
|
import { PasswordLogInCredentials } from "../../models/domain/logInCredentials";
|
|
|
|
import { SymmetricCryptoKey } from "../../models/domain/symmetricCryptoKey";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { PasswordTokenRequest } from "../../models/request/identityToken/passwordTokenRequest";
|
2022-03-03 02:47:57 +01:00
|
|
|
import { TokenRequestTwoFactor } from "../../models/request/identityToken/tokenRequest";
|
2022-02-01 00:51:32 +01:00
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
import { LogInStrategy } from "./logIn.strategy";
|
2022-02-01 00:51:32 +01:00
|
|
|
|
|
|
|
export class PasswordLogInStrategy extends LogInStrategy {
|
|
|
|
get email() {
|
|
|
|
return this.tokenRequest.email;
|
|
|
|
}
|
|
|
|
|
|
|
|
get masterPasswordHash() {
|
|
|
|
return this.tokenRequest.masterPasswordHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenRequest: PasswordTokenRequest;
|
|
|
|
|
|
|
|
private localHashedPassword: string;
|
|
|
|
private key: SymmetricCryptoKey;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
cryptoService: CryptoService,
|
|
|
|
apiService: ApiService,
|
|
|
|
tokenService: TokenService,
|
|
|
|
appIdService: AppIdService,
|
|
|
|
platformUtilsService: PlatformUtilsService,
|
|
|
|
messagingService: MessagingService,
|
|
|
|
logService: LogService,
|
|
|
|
stateService: StateService,
|
|
|
|
twoFactorService: TwoFactorService,
|
|
|
|
private authService: AuthService
|
|
|
|
) {
|
|
|
|
super(
|
|
|
|
cryptoService,
|
|
|
|
apiService,
|
|
|
|
tokenService,
|
|
|
|
appIdService,
|
|
|
|
platformUtilsService,
|
|
|
|
messagingService,
|
|
|
|
logService,
|
|
|
|
stateService,
|
|
|
|
twoFactorService
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onSuccessfulLogin() {
|
|
|
|
await this.cryptoService.setKey(this.key);
|
|
|
|
await this.cryptoService.setKeyHash(this.localHashedPassword);
|
|
|
|
}
|
|
|
|
|
2022-03-03 02:47:57 +01:00
|
|
|
async logInTwoFactor(
|
|
|
|
twoFactor: TokenRequestTwoFactor,
|
|
|
|
captchaResponse: string
|
|
|
|
): Promise<AuthResult> {
|
|
|
|
this.tokenRequest.captchaResponse = captchaResponse ?? this.captchaBypassToken;
|
|
|
|
return super.logInTwoFactor(twoFactor);
|
|
|
|
}
|
|
|
|
|
2022-02-01 00:51:32 +01:00
|
|
|
async logIn(credentials: PasswordLogInCredentials) {
|
|
|
|
const { email, masterPassword, captchaToken, twoFactor } = credentials;
|
|
|
|
|
|
|
|
this.key = await this.authService.makePreloginKey(masterPassword, email);
|
|
|
|
|
|
|
|
// Hash the password early (before authentication) so we don't persist it in memory in plaintext
|
|
|
|
this.localHashedPassword = await this.cryptoService.hashPassword(
|
|
|
|
masterPassword,
|
|
|
|
this.key,
|
|
|
|
HashPurpose.LocalAuthorization
|
|
|
|
);
|
|
|
|
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, this.key);
|
|
|
|
|
|
|
|
this.tokenRequest = new PasswordTokenRequest(
|
|
|
|
email,
|
|
|
|
hashedPassword,
|
|
|
|
captchaToken,
|
|
|
|
await this.buildTwoFactor(twoFactor),
|
|
|
|
await this.buildDeviceRequest()
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.startLogIn();
|
|
|
|
}
|
|
|
|
}
|