Skip to content

Commit

Permalink
Corrected scope of PPAPI access token for preprod env (#1055)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashwani Kumar <[email protected]>
  • Loading branch information
ashwani123p and sparrow1303 authored Nov 12, 2024
1 parent ff6059d commit a38fef0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/common/services/AuthenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
VSCODE_EXTENSION_PPAPI_WEBSITES_AUTHENTICATION_FAILED
} from "./TelemetryConstants";
import { ERROR_CONSTANTS } from "../ErrorConstants";
import { BAP_SERVICE_SCOPE_DEFAULT, INTELLIGENCE_SCOPE_DEFAULT, PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT, PROVIDER_ID, SCOPE_OPTION_CONTACTS_READ, SCOPE_OPTION_DEFAULT, SCOPE_OPTION_OFFLINE_ACCESS, SCOPE_OPTION_USERS_READ_BASIC_ALL } from "./Constants";
import { BAP_SERVICE_SCOPE_DEFAULT, INTELLIGENCE_SCOPE_DEFAULT, PPAPI_PREPROD_WEBSITES_SERVICE_SCOPE_DEFAULT, PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT, PROVIDER_ID, SCOPE_OPTION_CONTACTS_READ, SCOPE_OPTION_DEFAULT, SCOPE_OPTION_OFFLINE_ACCESS, SCOPE_OPTION_USERS_READ_BASIC_ALL, ServiceEndpointCategory } from "./Constants";
import jwt_decode from 'jwt-decode';
import { showErrorDialog } from "../utilities/errorHandlerUtil";

Expand Down Expand Up @@ -296,20 +296,22 @@ export function getOIDFromToken(token: string, telemetry: ITelemetry) {

export async function powerPlatformAPIAuthentication(
telemetry: ITelemetry,
serviceEndpointStamp: ServiceEndpointCategory,
firstTimeAuth = false
): Promise<string> {
let accessToken = "";
const PPAPI_WEBSITES_ENDPOINT = [ServiceEndpointCategory.TEST, ServiceEndpointCategory.PREPROD].includes(serviceEndpointStamp) ? PPAPI_PREPROD_WEBSITES_SERVICE_SCOPE_DEFAULT : PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT;
try {
let session = await vscode.authentication.getSession(
PROVIDER_ID,
[PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT],
[PPAPI_WEBSITES_ENDPOINT],
{ silent: true }
);

if (!session) {
session = await vscode.authentication.getSession(
PROVIDER_ID,
[PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT],
[PPAPI_WEBSITES_ENDPOINT],
{ createIfNone: true }
);
}
Expand Down
1 change: 1 addition & 0 deletions src/common/services/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const BAP_SERVICE_COPILOT_CROSS_GEO_FLAG_RELATIVE_URL = `scopes/admin/env
// PPAPI constants
export const PPAPI_WEBSITES_API_VERSION = '2022-03-01-preview';
export const PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT = "https://api.powerplatform.com/.default";
export const PPAPI_PREPROD_WEBSITES_SERVICE_SCOPE_DEFAULT = "https://api.preprod.powerplatform.com/.default";
export const PPAPI_WEBSITES_ENDPOINT = `{rootURL}/powerpages/environments/{environmentId}/websites`;

export enum ServiceEndpointCategory {
Expand Down
2 changes: 1 addition & 1 deletion src/common/services/PPAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class PPAPIService {
public static async getWebsiteDetailsById(serviceEndpointStamp: ServiceEndpointCategory, environmentId: string, websitePreviewId: string, telemetry: ITelemetry): Promise<IWebsiteDetails | null> { // websitePreviewId aka portalId

try {
const accessToken = await powerPlatformAPIAuthentication(telemetry, true);
const accessToken = await powerPlatformAPIAuthentication(telemetry, serviceEndpointStamp, true);
const response = await fetch(await PPAPIService.getPPAPIServiceEndpoint(serviceEndpointStamp, telemetry, environmentId, websitePreviewId), {
method: 'GET',
headers: getCommonHeaders(accessToken)
Expand Down

0 comments on commit a38fef0

Please sign in to comment.