Skip to content

Commit 5e80ead

Browse files
committed
Auth/pm 8882/Add TDE Logging (#9673)
* Added logging behind feature flag. * Added default for new flag. * Additional logging changes. * Consolidated log messages. * Removed unneccessary log. * Fixed test error. * Fixed linting. * Fixed constructor on test. * Updated to remove flag * Moved service. * Added logging to redirect guard. (cherry picked from commit fe1c432)
1 parent f61521d commit 5e80ead

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

libs/angular/src/auth/guards/redirect.guard.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
66
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/auth/abstractions/device-trust.service.abstraction";
77
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
88
import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service";
9+
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
910

1011
export interface RedirectRoutes {
1112
loggedIn: string;
@@ -32,6 +33,7 @@ export function redirectGuard(overrides: Partial<RedirectRoutes> = {}): CanActiv
3233
const authService = inject(AuthService);
3334
const cryptoService = inject(CryptoService);
3435
const deviceTrustService = inject(DeviceTrustServiceAbstraction);
36+
const logService = inject(LogService);
3537
const router = inject(Router);
3638

3739
const authStatus = await authService.getAuthStatus();
@@ -49,6 +51,12 @@ export function redirectGuard(overrides: Partial<RedirectRoutes> = {}): CanActiv
4951
const tdeEnabled = await firstValueFrom(deviceTrustService.supportsDeviceTrust$);
5052
const everHadUserKey = await firstValueFrom(cryptoService.everHadUserKey$);
5153
if (authStatus === AuthenticationStatus.Locked && tdeEnabled && !everHadUserKey) {
54+
logService.info(
55+
"Sending user to TDE decryption options. AuthStatus is %s. TDE support is %s. Ever had user key is %s.",
56+
AuthenticationStatus[authStatus],
57+
tdeEnabled,
58+
everHadUserKey,
59+
);
5260
return router.createUrlTree([routes.notDecrypted], { queryParams: route.queryParams });
5361
}
5462

libs/angular/src/auth/guards/tde-decryption-required.guard.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
1111
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/auth/abstractions/device-trust.service.abstraction";
1212
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
1313
import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service";
14+
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
1415

1516
/**
1617
* Only allow access to this route if the vault is locked and has never been decrypted.
@@ -23,15 +24,30 @@ export function tdeDecryptionRequiredGuard(): CanActivateFn {
2324
const authService = inject(AuthService);
2425
const cryptoService = inject(CryptoService);
2526
const deviceTrustService = inject(DeviceTrustServiceAbstraction);
27+
const logService = inject(LogService);
2628
const router = inject(Router);
2729

2830
const authStatus = await authService.getAuthStatus();
2931
const tdeEnabled = await firstValueFrom(deviceTrustService.supportsDeviceTrust$);
3032
const everHadUserKey = await firstValueFrom(cryptoService.everHadUserKey$);
33+
34+
// We need to determine if we should bypass the decryption options and send the user to the vault.
35+
// The ONLY time that we want to send a user to the decryption options is when:
36+
// 1. The user's auth status is Locked, AND
37+
// 2. TDE is enabled, AND
38+
// 3. The user has never had a user key in state since last logout.
39+
// The inverse of this is when we should send the user to the vault.
3140
if (authStatus !== AuthenticationStatus.Locked || !tdeEnabled || everHadUserKey) {
3241
return router.createUrlTree(["/"]);
3342
}
3443

44+
logService.info(
45+
"Sending user to TDE decryption options. AuthStatus is %s. TDE support is %s. Ever had user key is %s.",
46+
AuthenticationStatus[authStatus],
47+
tdeEnabled,
48+
everHadUserKey,
49+
);
50+
3551
return true;
3652
};
3753
}

libs/auth/src/common/login-strategies/sso-login.strategy.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ export class SsoLoginStrategy extends LoginStrategy {
8787

8888
data.userEnteredEmail = credentials.email;
8989

90+
const deviceRequest = await this.buildDeviceRequest();
91+
92+
this.logService.info("Logging in with appId %s.", deviceRequest.identifier);
93+
9094
data.tokenRequest = new SsoTokenRequest(
9195
credentials.code,
9296
credentials.codeVerifier,
9397
credentials.redirectUrl,
9498
await this.buildTwoFactor(credentials.twoFactor, credentials.email),
95-
await this.buildDeviceRequest(),
99+
deviceRequest,
96100
);
97101

98102
this.cache.next(data);
@@ -195,12 +199,18 @@ export class SsoLoginStrategy extends LoginStrategy {
195199

196200
// Note: TDE and key connector are mutually exclusive
197201
if (userDecryptionOptions?.trustedDeviceOption) {
202+
this.logService.info("Attempting to set user key with approved admin auth request.");
203+
204+
// Try to use the user key from an approved admin request if it exists.
205+
// Using it will clear it from state and future requests will use the device key.
198206
await this.trySetUserKeyWithApprovedAdminRequestIfExists(userId);
199207

200208
const hasUserKey = await this.cryptoService.hasUserKey(userId);
201209

202-
// Only try to set user key with device key if admin approval request was not successful
210+
// Only try to set user key with device key if admin approval request was not successful.
203211
if (!hasUserKey) {
212+
this.logService.info("Attempting to set user key with device key.");
213+
204214
await this.trySetUserKeyWithDeviceKey(tokenResponse, userId);
205215
}
206216
} else if (
@@ -275,11 +285,27 @@ export class SsoLoginStrategy extends LoginStrategy {
275285
): Promise<void> {
276286
const trustedDeviceOption = tokenResponse.userDecryptionOptions?.trustedDeviceOption;
277287

288+
if (!trustedDeviceOption) {
289+
this.logService.error("Unable to set user key due to missing trustedDeviceOption.");
290+
return;
291+
}
292+
278293
const deviceKey = await this.deviceTrustService.getDeviceKey(userId);
279294
const encDevicePrivateKey = trustedDeviceOption?.encryptedPrivateKey;
280295
const encUserKey = trustedDeviceOption?.encryptedUserKey;
281296

282297
if (!deviceKey || !encDevicePrivateKey || !encUserKey) {
298+
if (!deviceKey) {
299+
await this.logService.warning("Unable to set user key due to missing device key.");
300+
}
301+
if (!encDevicePrivateKey) {
302+
await this.logService.warning(
303+
"Unable to set user key due to missing encrypted device private key.",
304+
);
305+
}
306+
if (!encUserKey) {
307+
await this.logService.warning("Unable to set user key due to missing encrypted user key.");
308+
}
283309
return;
284310
}
285311

0 commit comments

Comments
 (0)