Compare commits
4 Commits
0269313516
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fc99f62c3 | |||
| d6adb5a3ba | |||
| 4dd3056b2f | |||
| 3e2b54ba3c |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@slawek/sk-az-tools",
|
"name": "@slawek/sk-az-tools",
|
||||||
"version": "0.8.0",
|
"version": "0.8.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@slawek/sk-az-tools",
|
"name": "@slawek/sk-az-tools",
|
||||||
"version": "0.8.0",
|
"version": "0.8.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@azure/identity": "^4.13.0",
|
"@azure/identity": "^4.13.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@slawek/sk-az-tools",
|
"name": "@slawek/sk-az-tools",
|
||||||
"version": "0.8.0",
|
"version": "0.8.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const { values } = parseArgs({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!["major", "minor", "patch"].includes(values.bump)) {
|
if (values.bump !== undefined && !["major", "minor", "patch"].includes(values.bump)) {
|
||||||
console.error(`Invalid bump type: ${values.bump}. Allowed values are: major, minor, patch.`);
|
console.error(`Invalid bump type: ${values.bump}. Allowed values are: major, minor, patch.`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ console.log(`SK Azure Tools Locked version: ${skAzToolsPackageLock.version}`);
|
|||||||
// Update package.json if --update flag is set
|
// Update package.json if --update flag is set
|
||||||
// or if the version of @slawek/sk-az-tools in package.json
|
// or if the version of @slawek/sk-az-tools in package.json
|
||||||
// is different than the version in package-lock.json.
|
// is different than the version in package-lock.json.
|
||||||
if (values.update || skAzToolsPackage.version !== skToolsPackage.version) {
|
if (values.update || skAzToolsPackage.version !== skAzToolsPackageLock.version) {
|
||||||
console.log(`Updating package.json...`);
|
console.log(`Updating package.json...`);
|
||||||
skAzToolsPackage.dependencies["@slawek/sk-tools"] = `>=${skToolsPackage.version}`;
|
skAzToolsPackage.dependencies["@slawek/sk-tools"] = `>=${skToolsPackage.version}`;
|
||||||
writeFileSync(skAzToolsPackagePath, JSON.stringify(skAzToolsPackage, null, 4));
|
writeFileSync(skAzToolsPackagePath, JSON.stringify(skAzToolsPackage, null, 4));
|
||||||
|
|||||||
@@ -13,18 +13,18 @@ export type DevOpsClients = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function getDevOpsClients(orgUrl: string, tenantId?: string, clientId?: string): Promise<DevOpsClients> {
|
export async function getDevOpsClients(orgUrl: string, tenantId?: string, clientId?: string): Promise<DevOpsClients> {
|
||||||
const credential = await getTokenCredential(tenantId, clientId);
|
return getTokenCredential(tenantId, clientId)
|
||||||
|
.then((credential) => credential.getToken(RESOURCE_SCOPE_BY_NAME.devops))
|
||||||
const accessToken = await credential.getToken(RESOURCE_SCOPE_BY_NAME.devops);
|
.then(async (accessToken) => {
|
||||||
if (!accessToken?.token) {
|
if (!accessToken?.token) {
|
||||||
throw new Error("Failed to obtain Azure DevOps API token");
|
throw new Error("Failed to obtain Azure DevOps API token");
|
||||||
}
|
}
|
||||||
|
|
||||||
const authHandler = azdev.getBearerHandler(accessToken.token);
|
const connection = new azdev.WebApi(orgUrl, azdev.getBearerHandler(accessToken.token));
|
||||||
const connection = new azdev.WebApi(orgUrl, authHandler);
|
const [coreClient, gitClient] = await Promise.all([
|
||||||
|
connection.getCoreApi(),
|
||||||
const coreClient = await connection.getCoreApi();
|
connection.getGitApi(),
|
||||||
const gitClient = await connection.getGitApi();
|
]);
|
||||||
|
|
||||||
return { coreClient, gitClient };
|
return { coreClient, gitClient };
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
import { Client } from "@microsoft/microsoft-graph-client";
|
|
||||||
import { getAccessToken } from "../azure/index.ts";
|
|
||||||
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
|
|
||||||
|
|
||||||
// export async function getGraphClientUsingMsal(
|
|
||||||
// tenantId: string,
|
|
||||||
// clientId: string,
|
|
||||||
// ): Promise<Client> {
|
|
||||||
// const graphApiToken = await getAccessToken(tenantId, clientId, ["graph"]);
|
|
||||||
|
|
||||||
// return Client.init({
|
|
||||||
// authProvider: (done) => {
|
|
||||||
// done(null, graphApiToken);
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
type GraphAuthProvider = (
|
|
||||||
done: (error: Error | null, accessToken: string | null) => void
|
|
||||||
) => void;
|
|
||||||
|
|
||||||
export function getMsalAuthProvider(
|
|
||||||
tenantId: string,
|
|
||||||
clientId: string,
|
|
||||||
): GraphAuthProvider {
|
|
||||||
return (done) => {
|
|
||||||
void getAccessToken(tenantId, clientId, ["graph"])
|
|
||||||
.then((accessToken) => done(null, accessToken))
|
|
||||||
.catch((err) => done(err as Error, null));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAzureIdentityAuthProvider(tenantId?: string, clientId?: string) : GraphAuthProvider {
|
|
||||||
const credentialOptions =
|
|
||||||
tenantId && clientId
|
|
||||||
? { tenantId, managedIdentityClientId: clientId }
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const credential = credentialOptions
|
|
||||||
? new DefaultAzureCredential(credentialOptions)
|
|
||||||
: new DefaultAzureCredential();
|
|
||||||
|
|
||||||
const getBearerToken = getBearerTokenProvider(
|
|
||||||
credential,
|
|
||||||
"https://graph.microsoft.com/.default",
|
|
||||||
);
|
|
||||||
|
|
||||||
return (done: (error: Error | null, accessToken: string | null) => void) => {
|
|
||||||
void getBearerToken()
|
|
||||||
.then((token) => done(null, token))
|
|
||||||
.catch((err) => done(err as Error, null));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,27 +1,18 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
export * from "./auth.ts";
|
|
||||||
export * from "./app.ts";
|
export * from "./app.ts";
|
||||||
export * from "./sp.ts";
|
export * from "./sp.ts";
|
||||||
|
|
||||||
import { loadAuthConfig, loadConfig } from "../index.ts";
|
import { Client } from "@microsoft/microsoft-graph-client";
|
||||||
import { Client, AuthProvider } from "@microsoft/microsoft-graph-client";
|
import { RESOURCE_SCOPE_BY_NAME, getTokenCredential } from "../azure/index.ts";
|
||||||
|
|
||||||
import { getMsalAuthProvider, getAzureIdentityAuthProvider } from "./auth.ts";
|
|
||||||
|
|
||||||
export async function getGraphClient(): Promise<Client> {
|
export async function getGraphClient(): Promise<Client> {
|
||||||
const config = await loadConfig();
|
|
||||||
|
|
||||||
let authProvider: AuthProvider;
|
|
||||||
|
|
||||||
if (config.authMode === "azure-identity") {
|
|
||||||
authProvider = getAzureIdentityAuthProvider();
|
|
||||||
} else {
|
|
||||||
const authConfig = await loadAuthConfig("public-config");
|
|
||||||
authProvider = getMsalAuthProvider(authConfig.tenantId, authConfig.clientId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Client.init({
|
return Client.init({
|
||||||
authProvider: authProvider,
|
authProvider: (done) => {
|
||||||
|
void getTokenCredential()
|
||||||
|
.then((credential) => credential.getToken(RESOURCE_SCOPE_BY_NAME.graph))
|
||||||
|
.then((accessToken) => done(null, accessToken?.token ?? null))
|
||||||
|
.catch((err) => done(err as Error, null));
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user