Skip to content

Commit

Permalink
Merge branch 'main' into user/amitjoshi/webpageNameValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjoshi438 committed Jun 14, 2024
2 parents b8031fb + a256808 commit 54e6e40
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/common/copilot/PowerPagesCopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as vscode from "vscode";
import { sendApiRequest } from "./IntelligenceApiService";
import { dataverseAuthentication, intelligenceAPIAuthentication } from "../services/AuthenticationProvider";
import { dataverseAuthentication, getOIDFromToken, intelligenceAPIAuthentication } from "../services/AuthenticationProvider";
import { v4 as uuidv4 } from 'uuid'
import { PacWrapper } from "../../client/pac/PacWrapper";
import { ITelemetry } from "../../client/telemetry/ITelemetry";
Expand Down Expand Up @@ -341,8 +341,7 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider {
if (session) {
intelligenceApiToken = session.accessToken;
userName = getUserName(session.account.label);
userID = session?.account.id.split("/").pop() ??
session?.account.id;
userID = getOIDFromToken(session.accessToken, this.telemetry);
} else {
intelligenceApiToken = "";
userName = "";
Expand Down Expand Up @@ -406,7 +405,7 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider {

const copilotAvailabilityStatus = checkCopilotAvailability(this.aibEndpoint, orgID, this.telemetry, sessionID, tenantId);

if(!copilotAvailabilityStatus) {
if (!copilotAvailabilityStatus) {
this.sendMessageToWebview({ type: 'Unavailable' });
return;
} else {
Expand Down
34 changes: 19 additions & 15 deletions src/common/services/AuthenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import {
VSCODE_EXTENSION_GRAPH_CLIENT_AUTHENTICATION_FAILED,
VSCODE_EXTENSION_GRAPH_CLIENT_AUTHENTICATION_COMPLETED,
VSCODE_EXTENSION_BAP_SERVICE_AUTHENTICATION_COMPLETED,
VSCODE_EXTENSION_BAP_SERVICE_AUTHENTICATION_FAILED
VSCODE_EXTENSION_BAP_SERVICE_AUTHENTICATION_FAILED,
VSCODE_EXTENSION_DECODE_JWT_TOKEN_FAILED
} from "./TelemetryConstants";
import { ERRORS } from "../ErrorConstants";
import { BAP_SERVICE_SCOPE_DEFAULT, INTELLIGENCE_SCOPE_DEFAULT, PROVIDER_ID, SCOPE_OPTION_CONTACTS_READ, SCOPE_OPTION_DEFAULT, SCOPE_OPTION_OFFLINE_ACCESS, SCOPE_OPTION_USERS_READ_BASIC_ALL } from "./Constants";
import jwt_decode from 'jwt-decode';


export function getCommonHeadersForDataverse(
Expand Down Expand Up @@ -66,9 +68,7 @@ export async function intelligenceAPIAuthentication(telemetry: ITelemetry, sessi
}
accessToken = session?.accessToken ?? '';
user = session.account.label;
userId = session?.account.id.split("/").pop() ??
session?.account.id ??
"";
userId = getOIDFromToken(accessToken, telemetry);
if (!accessToken) {
throw new Error(ERRORS.NO_ACCESS_TOKEN);
}
Expand Down Expand Up @@ -112,9 +112,7 @@ export async function dataverseAuthentication(
}

accessToken = session?.accessToken ?? "";
userId = session?.account.id.split("/").pop() ??
session?.account.id ??
"";
userId = getOIDFromToken(accessToken, telemetry);
if (!accessToken) {
throw new Error(ERRORS.NO_ACCESS_TOKEN);
}
Expand Down Expand Up @@ -219,10 +217,7 @@ export async function graphClientAuthentication(
if (firstTimeAuth) {
sendTelemetryEvent(telemetry, {
eventName: VSCODE_EXTENSION_GRAPH_CLIENT_AUTHENTICATION_COMPLETED,
userId:
session?.account.id.split("/").pop() ??
session?.account.id ??
"",
userId: getOIDFromToken(accessToken, telemetry),
});
}
} catch (error) {
Expand Down Expand Up @@ -268,10 +263,7 @@ export async function bapServiceAuthentication(
if (firstTimeAuth) {
sendTelemetryEvent(telemetry, {
eventName: VSCODE_EXTENSION_BAP_SERVICE_AUTHENTICATION_COMPLETED,
userId:
session?.account.id.split("/").pop() ??
session?.account.id ??
"",
userId: getOIDFromToken(accessToken, telemetry),
});
}
} catch (error) {
Expand All @@ -288,3 +280,15 @@ export async function bapServiceAuthentication(

return accessToken;
}

export function getOIDFromToken(token: string, telemetry: ITelemetry) {
try {
const decoded = jwt_decode(token);
return decoded?.oid ?? "";
} catch (error) {
sendTelemetryEvent(telemetry,
{ eventName: VSCODE_EXTENSION_DECODE_JWT_TOKEN_FAILED, errorMsg: (error as Error).message }
)
}
return "";
}
1 change: 1 addition & 0 deletions src/common/services/TelemetryConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export const VSCODE_EXTENSION_BAP_SERVICE_AUTHENTICATION_COMPLETED = "VSCodeExte
export const VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_FAILED = "VSCodeExtensionGetCrossGeoDataMovementEnabledFlagFailed";
export const VSCODE_EXTENSION_GET_CROSS_GEO_DATA_MOVEMENT_ENABLED_FLAG_COMPLETED = "VSCodeExtensionGetCrossGeoDataMovementEnabledFlagCompleted";
export const VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION = "VSCodeExtensionGetBAPEndpointUnsupportedRegion";
export const VSCODE_EXTENSION_DECODE_JWT_TOKEN_FAILED = "VSCodeExtensionDecodeJWTTokenFailed";
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("Authentication Provider", () => {
"Authorization Failed. Please run again to authorize it"
);

sinon.assert.calledOnce(sendTelemetryEvent);
sinon.assert.calledTwice(sendTelemetryEvent);
sinon.assert.calledOnce(showErrorDialog);
sinon.assert.calledOnce(_mockgetSession);
});
Expand Down

0 comments on commit 54e6e40

Please sign in to comment.