如何从 powershell 打开 azure devops 登录屏幕?

问题描述 投票:0回答:1

我正在编写一个 powershell 脚本来轮换我的 devbox 中的 PAT。这是我所做的:

PS> .\Rotate-Pat.ps1

这是 Check-PatValidity.ps1 中的代码:

$env:PAT | az devops login --organization "https://dev.azure.com/$organization"

    $body = @{
        displayName = $organization
        scope = "vso.build vso.code_full vso.tokens vso.profile"
        validTo = (Get-Date).AddDays(7).ToString("yyyy-MM-ddTHH:mm:ssZ")
        allOrgs = $false
    } | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "https://vssps.dev.azure.com/$organization/_apis/tokens/pats?api-version=7.1-preview.1" -Headers $headers -Method Post -Body $body -ContentType "application/json"
    
    $prettyJson = $response | ConvertTo-Json -Depth 2
    $prettyJson | Out-file $env:USERPROFILE\Downloads\patgenoutput.json -Encoding UTF8

    if ($response) {
        $newPat = $response.patToken.token
        Write-Output "New PAT: $newPat"
    }

这是我看到的输出(与 patgenoutput.json 完全相同): enter image description here

看起来像 Azure DevOps 登录屏幕的 html。

问题:

  1. 我走在正确的道路上吗?如果没有,请指出我正确的方向。
  2. 如何使 Azure DevOps 登录为交互式会话并获取访问令牌来生成 PAT?
azure powershell azure-devops azure-devops-rest-api
1个回答
0
投票

根据您的描述,您正在尝试使用 Pats - Create REST API 使用旧 PAT 令牌创建新的 PAT 令牌。但是,不允许使用旧的 PAT 令牌创建新令牌。

您可以查看此文档使用 REST API 管理个人访问令牌 (PAT) 以及以下常见问题 (FAQ)。

问:为什么我需要使用 Microsoft Entra 令牌进行身份验证?为什么 PAT 还不够?

答:通过此 PAT 生命周期管理 API,我们开放了创建新 PAT 和撤销现有 PAT 的功能。如果落入坏人之手,恶意行为者可能会使用此 API 创建多个进入组织 Azure DevOps 资源的入口点。通过强制执行 Microsoft Entra 身份验证,我们希望这个强大的 API 能够更安全地防止这种未经授权的使用。

问:如何通过 API 重新生成/轮换 PAT?我在 UI 中看到了该选项,但在 API 中没有看到类似的方法。

答:好问题! UI 中可用的“重新生成”功能实际上完成了一些操作,这些操作可以通过 API 完全复制。 要轮换您的 PAT,请执行以下步骤:

  • 使用 GET 调用了解 PAT 的元数据,
  • 使用 POST 调用使用旧 PAT 的元数据创建新 PAT,
  • 使用 DELETE 调用撤销旧的 PAT

以下是创建 Azure AD 应用程序和创建 PAT 的步骤。

  1. 如果您没有,请在 Azure 门户中的 Microsoft Entra ID -> 应用程序注册下注册一个新应用程序。

    App Registration

  2. 选择您的应用程序并导航到 API 权限,然后选择 Azure DevOps -> 检查 user_impersonation -> 选择添加权限。

    API Permissions

    Azure DevOps

  3. 添加 https://jwt.ms 作为重定向 URI。

    Redirect URI

  4. 在证书和机密中创建客户端机密

    Client secret

  5. 在浏览器中粘贴并访问以下链接以发出授权请求,以获取结果 URL 中的

    code

https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
?client_id=<appID>
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope= 499b84ac-1321-427f-aa17-267ca6975798/.default
&state=12345

结果: code

  1. 然后您可以使用步骤 4 中的
    code
    和以下 powershell 脚本生成访问令牌:
$tenantId = ''     
$clientId = ''   # check Application (client) ID and tenant Id from the app overview page
$clientSecret = ''  # the clientSecret from the step  4
$redirectUri="https://jwt.ms"

$organization = ""

$code=""   # the code from the step 5

$tokenUrl = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"

# Prepare the body of the request
$body = @{
    grant_type    = "authorization_code"
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = "499b84ac-1321-427f-aa17-267ca6975798/.default"
    redirect_uri  = $redirectUri
    code          = $code
}

# Prepare the headers
$headers = @{
    "Content-Type" = "application/x-www-form-urlencoded"
}

# Make the POST request
$response = Invoke-RestMethod -Method Post -Uri $tokenUrl -Headers $headers -Body $body
echo $response.access_token


$body = @{
        displayName = $organization
        scope = "vso.build vso.code_full vso.tokens vso.profile"
        validTo = (Get-Date).AddDays(7).ToString("yyyy-MM-ddTHH:mm:ssZ")
        allOrgs = $false
    } | ConvertTo-Json
$accessHeader = @{
    'Authorization' = 'Bearer ' + $response.access_token
}
$PATresponse = Invoke-RestMethod -Uri "https://vssps.dev.azure.com/$organization/_apis/tokens/pats?api-version=7.1-preview.1" -Headers $accessHeader -Method Post -Body $body -ContentType "application/json"


if ($PATresponse) {
   $newPat = $PATresponse.patToken.token
   Write-Output "New PAT: $newPat"
}

请注意,授权码的有效期很短,通常会在大约 10 分钟后过期。所以,如果你想用它

refresh_token
来获取新的令牌,而不是再次手动获取授权码,你可以参考这个问题。更多详情请参考Microsoft身份平台和OAuth 2.0授权码流程

© www.soinside.com 2019 - 2024. All rights reserved.