Skip to content

Commit

Permalink
Refactor authentication and PPAPI service endpoints
Browse files Browse the repository at this point in the history
This commit refactors the authentication and PPAPI service endpoints in the code. It updates the import statements and removes unnecessary constants. Additionally, it modifies the `powerPlatformAPIAuthentication` function to accept the `ppapiEndpoint` parameter and uses it in the fetch requests.
  • Loading branch information
amitjoshi committed Oct 22, 2024
1 parent 0d5e508 commit 958a713
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion 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, 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';
import { showErrorDialog } from "../utilities/errorHandlerUtil";

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

export async function powerPlatformAPIAuthentication(
telemetry: ITelemetry,
ppapiEndpoint: string,
firstTimeAuth = false
): Promise<string> {
let accessToken = "";
try {
const PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT = `${ppapiEndpoint}${SCOPE_OPTION_DEFAULT}`;
let session = await vscode.authentication.getSession(
PROVIDER_ID,
[PPAPI_WEBSITES_SERVICE_SCOPE_DEFAULT],
Expand Down
13 changes: 7 additions & 6 deletions src/common/services/PPAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ 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 response = await fetch(await PPAPIService.getPPAPIServiceEndpoint(serviceEndpointStamp, telemetry, environmentId, websitePreviewId), {
const ppapiEndpoint = await PPAPIService.getPPAPIServiceEndpoint(serviceEndpointStamp, telemetry, environmentId);
const accessToken = await powerPlatformAPIAuthentication(telemetry, ppapiEndpoint, true);
const response = await fetch(ppapiEndpoint, {
method: 'GET',
headers: getCommonHeaders(accessToken)
});
Expand All @@ -41,11 +42,13 @@ export class PPAPIService {
websiteLanguage: number,
telemetry: ITelemetry) { // websitePreviewId aka portalId

const ppapiEndpoint = await PPAPIService.getPPAPIServiceEndpoint(serviceEndpointStamp, telemetry, environmentId);

try {
console.log("Creating website");
const accessToken = await powerPlatformAPIAuthentication(telemetry, true);
const accessToken = await powerPlatformAPIAuthentication(telemetry, ppapiEndpoint, true);
const siteSuffix = uuidv4();
const response = await fetch(await PPAPIService.getPPAPIServiceEndpoint(serviceEndpointStamp, telemetry, environmentId), {
const response = await fetch(ppapiEndpoint, {
method: 'POST',
headers: getCommonHeaders(accessToken),
body: JSON.stringify({
Expand All @@ -58,8 +61,6 @@ export class PPAPIService {
})
});

console.log(response);

if (response.ok) {
sendTelemetryEvent(telemetry, { eventName: VSCODE_EXTENSION_PPAPI_CREATE_WEBSITE_COMPLETED, data: `environmentId:${environmentId}, orgId:${orgId}, websiteName:${websiteName}` });
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/client/utilities/commonUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export function getRangeForMultilineMatch(text: string, pattern: string, index:
}

export async function validateWebsitePreviewURL(): Promise<boolean> {
const envId = WebExtensionContext.urlParametersMap?.get(queryParameters.ENV_ID);
const envId = getEnvironmentIdFromUrl();
const serviceEndpointStamp = WebExtensionContext.serviceEndpointCategory;
const websitePreviewId = WebExtensionContext.urlParametersMap?.get(queryParameters.PORTAL_ID);

Expand Down

0 comments on commit 958a713

Please sign in to comment.