Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Backend Functionality for Service Authorization #274

Merged
merged 13 commits into from
Nov 21, 2024
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
"url": "https://github.com/iTwin/auth-clients"
},
"devDependencies": {
"beachball": "^2.43.1",
"lage": "^2.7.18",
"path-browserify": "^1.0.0",
"process": "^0.11.10"
"beachball": "^2.49.1",
"lage": "^2.11.13"
}
}
}
19 changes: 18 additions & 1 deletion packages/oidc-signin-tool/src/certa/certaBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import * as fs from "fs";
import * as path from "path";
import type { AccessToken } from "@itwin/core-bentley";
import { registerBackendCallback } from "@itwin/certa/lib/utils/CallbackUtils";
import { ServiceAuthorizationClient, ServiceAuthorizationClientConfiguration } from "@itwin/service-authorization";
aruniverse marked this conversation as resolved.
Show resolved Hide resolved
import type { TestBrowserAuthorizationClientConfiguration, TestUserCredentials } from "../TestUsers";
import { TestUtility } from "../TestUtility";
import { getTokenCallbackName } from "./certaCommon";
import { getTokenCallbackName, getServiceAuthTokenCallbackName } from "./certaCommon";

// A backend to use within Certa's `backendInitModule` to setup OIDC sign-in.

Expand Down Expand Up @@ -56,12 +57,28 @@ async function signin(user: TestUserCredentials, oidcConfig?: TestBrowserAuthori
if (undefined === token)
throw new Error("Failed to get access token");


return token;
}

async function signinWithServiceAuthClient(oidcConfig: ServiceAuthorizationClientConfiguration): Promise<string> {
const serviceAuthClient = new ServiceAuthorizationClient(oidcConfig);
const accessToken = await serviceAuthClient.getAccessToken();
if (!accessToken) {
throw new Error("Failed to retrieve access token from ServiceAuthorizationClient.");
}

return accessToken;
}

registerBackendCallback(
getTokenCallbackName,
async (user: any, oidcConfig?: any): Promise<string> => {
return signin(user, oidcConfig);
},
);


registerBackendCallback(getServiceAuthTokenCallbackName, async (oidcConfig: ServiceAuthorizationClientConfiguration): Promise<string> => {
return signinWithServiceAuthClient(oidcConfig);
});
8 changes: 8 additions & 0 deletions packages/oidc-signin-tool/src/certa/certaCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
*--------------------------------------------------------------------------------------------*/
import { executeBackendCallback } from "@itwin/certa/lib/utils/CallbackUtils";
import type { TestBrowserAuthorizationClientConfiguration, TestUserCredentials } from "../TestUsers";
import type { ServiceAuthorizationClientConfiguration } from "@itwin/service-authorization";

// Shared by both the frontend and backend side of the tests
export const getTokenCallbackName = "getToken";
export const getServiceAuthTokenCallbackName = "getServiceAuthToken";

/**
* Retrieves an access token from the backend using the specified user credentials.
Expand All @@ -15,3 +17,9 @@ export async function getAccessTokenFromBackend(user: TestUserCredentials, oidcC
const accessToken = await executeBackendCallback(getTokenCallbackName, user, oidcConfig);
return accessToken;
}


export async function getServiceAuthTokenFromBackend(oidcConfig: ServiceAuthorizationClientConfiguration): Promise<string> {
const accessToken = await executeBackendCallback(getServiceAuthTokenCallbackName, oidcConfig);
return accessToken;
}
Loading
Loading