Skip to content

Commit

Permalink
Instrument account type (#7049)
Browse files Browse the repository at this point in the history
- Instrument account type
- Fix acquireTokenByCode native flow instrumentation
  • Loading branch information
konstantin-msft authored Apr 23, 2024
1 parent 236cc4a commit 23df98f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Instrument account type #7049",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Instrument account type #7049",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
67 changes: 47 additions & 20 deletions lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ import { ClearCacheRequest } from "../request/ClearCacheRequest";
import { createNewGuid } from "../crypto/BrowserCrypto";
import { initializeSilentRequest } from "../request/RequestHelpers";

function getAccountType(
account?: AccountInfo
): "AAD" | "MSA" | "B2C" | undefined {
const idTokenClaims = account?.idTokenClaims;
if (idTokenClaims?.tfp || idTokenClaims?.acr) {
return "B2C";
}

if (!idTokenClaims?.tid) {
return undefined;
} else if (idTokenClaims?.tid === "9188040d-6c67-4c5b-b112-36a304b66dad") {
return "MSA";
}
return "AAD";
}

export class StandardController implements IController {
// OperatingContext
protected readonly operatingContext: StandardOperatingContext;
Expand Down Expand Up @@ -472,7 +488,10 @@ export class StandardController implements IController {
"handleRedirectResponse returned result, acquire token success"
);
}
rootMeasurement.end({ success: true });
rootMeasurement.end({
success: true,
accountType: getAccountType(result.account),
});
}
this.eventHandler.emitEvent(
EventType.HANDLE_REDIRECT_END,
Expand Down Expand Up @@ -631,7 +650,10 @@ export class StandardController implements IController {
correlationId
);

atPopupMeasurement.add({ scenarioId: request.scenarioId });
atPopupMeasurement.add({
scenarioId: request.scenarioId,
accountType: getAccountType(request.account),
});

try {
this.logger.verbose("acquireTokenPopup called", correlationId);
Expand Down Expand Up @@ -674,6 +696,7 @@ export class StandardController implements IController {
success: true,
isNativeBroker: true,
requestId: response.requestId,
accountType: getAccountType(response.account),
});
return response;
})
Expand Down Expand Up @@ -723,13 +746,12 @@ export class StandardController implements IController {
);
}

atPopupMeasurement.add({
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
});
atPopupMeasurement.end({
success: true,
requestId: result.requestId,
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
accountType: getAccountType(result.account),
});
return result;
})
Expand Down Expand Up @@ -813,7 +835,10 @@ export class StandardController implements IController {
this.ssoSilentMeasurement?.increment({
visibilityChangeCount: 0,
});
this.ssoSilentMeasurement?.add({ scenarioId: request.scenarioId });
this.ssoSilentMeasurement?.add({
scenarioId: request.scenarioId,
accountType: getAccountType(request.account),
});

document.addEventListener(
"visibilitychange",
Expand Down Expand Up @@ -857,14 +882,13 @@ export class StandardController implements IController {
InteractionType.Silent,
response
);
this.ssoSilentMeasurement?.add({
accessTokenSize: response.accessToken.length,
idTokenSize: response.idToken.length,
});
this.ssoSilentMeasurement?.end({
success: true,
isNativeBroker: response.fromNativeBroker,
requestId: response.requestId,
accessTokenSize: response.accessToken.length,
idTokenSize: response.idToken.length,
accountType: getAccountType(response.account),
});
return response;
})
Expand Down Expand Up @@ -943,14 +967,13 @@ export class StandardController implements IController {
result
);
this.hybridAuthCodeResponses.delete(hybridAuthCode);
atbcMeasurement.add({
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
});
atbcMeasurement.end({
success: true,
isNativeBroker: result.fromNativeBroker,
requestId: result.requestId,
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
accountType: getAccountType(result.account),
});
return result;
})
Expand Down Expand Up @@ -981,7 +1004,7 @@ export class StandardController implements IController {
return await response;
} else if (request.nativeAccountId) {
if (this.canUseNative(request, request.nativeAccountId)) {
return await this.acquireTokenNative(
const result = await this.acquireTokenNative(
{
...request,
correlationId,
Expand All @@ -998,6 +1021,11 @@ export class StandardController implements IController {
}
throw e;
});
atbcMeasurement.end({
accountType: getAccountType(result.account),
success: true,
});
return result;
} else {
throw createBrowserAuthError(
BrowserAuthErrorCodes.unableToAcquireTokenFromNativePlatform
Expand Down Expand Up @@ -1871,6 +1899,7 @@ export class StandardController implements IController {
if (!account) {
throw createBrowserAuthError(BrowserAuthErrorCodes.noAccountError);
}
atsMeasurement.add({ accountType: getAccountType(account) });

const thumbprint: RequestThumbprint = {
clientId: this.config.auth.clientId,
Expand Down Expand Up @@ -1910,16 +1939,14 @@ export class StandardController implements IController {
)
.then((result) => {
this.activeSilentTokenRequests.delete(silentRequestKey);
atsMeasurement.add({
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
});
atsMeasurement.end({
success: true,
fromCache: result.fromCache,
isNativeBroker: result.fromNativeBroker,
cacheLookupPolicy: request.cacheLookupPolicy,
requestId: result.requestId,
accessTokenSize: result.accessToken.length,
idTokenSize: result.idToken.length,
});
return result;
})
Expand Down
Loading

0 comments on commit 23df98f

Please sign in to comment.