-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Packaging WIF Handshake for Packaging Authenticate Tasks (#342)
* Add Entra Auth User WIF service connection utility for Packaging tasks * Update module strings and bump version * bump package version * Correct login url * Address PR comments
- Loading branch information
Showing
5 changed files
with
180 additions
and
491 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
common-npm-packages/artifacts-common/EntraWifUserServiceConnectionUtils.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export declare function getFederatedWorkloadIdentityCredentials(serviceConnectionName: string, tenantId?: string) : Promise<string | undefined> | ||
export declare function getFeedTenantId(feedUrl: string) : Promise<string | undefined> |
77 changes: 77 additions & 0 deletions
77
common-npm-packages/artifacts-common/EntraWifUserServiceConnectionUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import path = require("path"); | ||
import * as tl from 'azure-pipelines-task-lib/task'; | ||
import { getSystemAccessToken } from "./webapi"; | ||
import fetch from "node-fetch"; | ||
|
||
tl.setResourcePath(path.join(__dirname, 'module.json'), true); | ||
|
||
const ADO_RESOURCE : string = "499b84ac-1321-427f-aa17-267ca6975798/.default"; | ||
const CLIENT_ASSERTION_TYPE : string = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"; | ||
const GRANT_TYPE = "client_credentials"; | ||
|
||
export async function getFederatedWorkloadIdentityCredentials(serviceConnectionName: string, tenantId?: string) : Promise<string | undefined> | ||
{ | ||
try { | ||
let tenant = tenantId ?? tl.getEndpointAuthorizationParameterRequired(serviceConnectionName, "TenantId"); | ||
tl.debug(tl.loc('Info_UsingTenantId', tenantId)); | ||
const systemAccessToken = getSystemAccessToken(); | ||
const url = process.env["SYSTEM_OIDCREQUESTURI"]+"?api-version=7.1&serviceConnectionId="+serviceConnectionName; | ||
|
||
const ADOResponse: {oidcToken: string} = await (await fetch(url, | ||
{ | ||
method: 'POST', | ||
headers: | ||
{ | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer '+ systemAccessToken | ||
} | ||
})).json() as {oidcToken: string}; | ||
|
||
tl.setSecret(ADOResponse.oidcToken); | ||
let entraURI = "https://login.windows.net/"+tenant+"/oauth2/v2.0/token"; | ||
let clientId = tl.getEndpointAuthorizationParameterRequired(serviceConnectionName, "ServicePrincipalId"); | ||
|
||
let body = { | ||
'scope': ADO_RESOURCE, | ||
'client_id': clientId, | ||
'client_assertion_type': CLIENT_ASSERTION_TYPE, | ||
'client_assertion': ADOResponse.oidcToken, | ||
'grant_type': GRANT_TYPE | ||
}; | ||
|
||
let formBody = Object.keys(body) | ||
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])) | ||
.join('&'); | ||
|
||
const entraResponse: {access_token: string} = await (await fetch(entraURI, | ||
{ | ||
method: 'POST', | ||
body: formBody, | ||
headers: | ||
{ | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
} | ||
})).json() as {access_token: string}; | ||
tl.setSecret(entraResponse.access_token); | ||
return entraResponse.access_token; | ||
} | ||
catch (error) | ||
{ | ||
tl.error(tl.loc("Error_FederatedTokenAquisitionFailed", error)); | ||
return undefined; | ||
} | ||
} | ||
|
||
export async function getFeedTenantId(feedUrl: string) : Promise<string | undefined> | ||
{ | ||
try | ||
{ | ||
const feedResponse = await fetch(feedUrl); | ||
return feedResponse?.headers?.get('X-VSS-ResourceTenant'); | ||
} | ||
catch (error) | ||
{ | ||
tl.warning(tl.loc("Error_GetFeedTenantIdFailed", error)); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.