...
1<#
2.DESCRIPTION
3Get the corresponding ms alias from github identity
4.PARAMETER AadToken
5The aad access token.
6.PARAMETER GithubName
7Github identity. E.g sima-zhu
8.PARAMETER ContentType
9Content type of http requests.
10.PARAMETER AdditionalHeaders
11Additional parameters for http request headers in key-value pair format, e.g. @{ key1 = val1; key2 = val2; key3 = val3}
12#>
13[CmdletBinding(SupportsShouldProcess = $true)]
14param(
15 [Parameter(Mandatory = $true)]
16 [string]$TenantId,
17
18 [Parameter(Mandatory = $true)]
19 [string]$ClientId,
20
21 [Parameter(Mandatory = $true)]
22 [string]$ClientSecret,
23
24 [Parameter(Mandatory = $true)]
25 [string]$GithubUser
26)
27Set-StrictMode -Version 3
28
29. "${PSScriptRoot}\common.ps1"
30
31$OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links/github/$GithubUser"
32
33function Generate-AadToken ($TenantId, $ClientId, $ClientSecret) {
34 $LoginAPIBaseURI = "https://login.microsoftonline.com/$TenantId/oauth2/token"
35 try {
36 $headers = @{
37 "content-type" = "application/x-www-form-urlencoded"
38 }
39
40 $body = @{
41 "grant_type" = "client_credentials"
42 "client_id" = $ClientId
43 "client_secret" = $ClientSecret
44 "resource" = "api://repos.opensource.microsoft.com/audience/7e04aa67"
45 }
46 Write-Host "Generating aad token..."
47 $resp = Invoke-RestMethod $LoginAPIBaseURI -Method 'POST' -Headers $headers -Body $body
48 }
49 catch {
50 LogError $_
51 exit 1
52 }
53
54 return $resp.access_token
55}
56
57$Headers = @{
58 "Content-Type" = "application/json"
59 "api-version" = "2019-10-01"
60}
61
62try {
63 $opsAuthToken = Generate-AadToken -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
64 $Headers["Authorization"] = "Bearer $opsAuthToken"
65 Write-Host "Fetching aad identity for github user: $GithubName"
66 $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers
67}
68catch {
69 LogError $_
70 exit 1
71}
72
73$resp | Write-Verbose
74
75if ($resp.aad) {
76 Write-Host "Fetched aad identity $($resp.aad.alias) for github user $GithubName."
77 return $resp.aad.alias
78}
79
80LogError "Failed to retrieve the aad identity from given github user: $GithubName"
81exit 1
View as plain text