【问题标题】:Discover owner of VSTS PAT via API通过 API 发现 VSTS PAT 的所有者
【发布时间】:2020-05-24 00:00:09
【问题描述】:

我继承了一个与 VSTS API 集成的 PowerShell 脚本,并使用当前存储在我们团队密码保险箱中的 PAT(个人访问令牌)进行身份验证。

但是,PAT 的起源已经被时间的迷雾迷失了,我不知道最初是哪个团队成员创建了它(他们甚至可能还不能在这里工作!),或者哪个用户的 VSTS定义它的帐户。

使用 just PAT,我可以点击 VSTS API 中的任何“who-am-i”类型的端点来回显用户名、guid 或其他详细信息帐户中定义了 PAT?

有几个原因我特别想找出 PAT 的定义位置,而不是仅仅从另一个帐户发布一个新的:

  • 这样当当前帐户到期时,我可以在同一帐户中获得新的帐户
  • 所以我们可以让所有者在必要时撤销现有的

干杯,

M

【问题讨论】:

    标签: authentication azure-devops-rest-api


    【解决方案1】:

    根据 Marina 的回答,我编写了以下 PowerShell 脚本来创建一个使用 PAT 进行身份验证的新任务。然后,您可以检查 json 响应中的“fields -> System.CreatedBy”属性以查看 PAT 属于哪个帐户。

    $ErrorActionPreference = "Stop";
    $ProgressPreference = "SilentlyContinue";
    Set-StrictMode -Version "Latest";
    
    
    $vstsAccount     = "myaccount";     # e.g. "myaccount" in "https://myaccount.visualstudio.com"
    $vstsProjectName = "myprojectname"; # e.g. "myprojectname" in "https://myaccount.visualstudio.com/myprojectname"
    $vstsPat         = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
    
    function Invoke-VstsWebRequest
    {
        param
        (
            [string] $Uri,
            [string] $Pat,
            [string] $Method,
            [string] $Body,
            [string] $ContentType
        )
        write-host "uri = '$Uri'";
        $splat = @{
            "Uri"     = $Uri
            "Headers" = @{
                "Authorization" = "Basic " +
                    [System.Convert]::ToBase64String(
                        [System.Text.ASCIIEncoding]::ASCII.GetBytes(
                           [string]::Format("{0}:{1}", "", $Pat)
                        ) 
                    )
            }
            "Method" = $Method
            "UseBasicParsing" = $true
        }
        if( -not [string]::IsNullOrEmpty($Body) )
        {
            write-host "body = ";
            write-host $Body;
            $splat.Add("ContentType", $ContentType); 
            $splat.Add("Body", $Body);
        }
        $response = Invoke-WebRequest @splat;
        write-host "response = ";
        write-host ($response.Content | ConvertFrom-Json | ConvertTo-Json);
        return $response.Content;
    }
    
    
    # get existing work items in the specified project
    # (not necessary, but useful for testing)
    $uri  = "https://$vstsAccount.visualstudio.com/$vstsProjectName/_apis/wit/wiql?%24top=50&api-version=4.1";
    $query = @"
    SELECT [System.ID],
           [System.Title]
    FROM workitems
    WHERE [System.TeamProject] = '{0}'
    ORDER BY [System.Title]
    "@;
    $body = ConvertTo-Json ([ordered] @{
        "query" = [string]::Format($query, $vstsProjectName)
    });
    $type = "application/json";
    $json = Invoke-VstsWebRequest -Uri $uri -Pat $vstsPat -Method "Post" -Body $body -ContentType $type;
    
    
    # create a new "Task" work item in the specified project
    # (the response will show the user account associated with the PAT)
    $uri  = "https://$vstsAccount.visualstudio.com/$vstsProjectName/_apis/wit/workitems/`$Task?api-version=4.1";
    $body = ConvertTo-Json @([ordered] @{
        "op"    = "add"
        "path"  = "/fields/System.Title"
        "value" = "my sample task"
    });
    $type = "application/json-patch+json";
    $json = Invoke-VstsWebRequest -Uri $uri -Pat $vstsPat -Method "Post" -Body $body -ContentType $type;
    

    输出:

    ...
    "System.CreatedBy":"My Username <my.username@example.org>"
    ...
    

    附:我在此处发布了请求“我是谁”端点的 UserVoice 票证:https://visualstudio.uservoice.com/forums/330519-visual-studio-team-services/suggestions/34509568-provide-a-who-am-i-endpoint-in-the-vsts-api-to-i

    【讨论】:

    • 您可以标记自己的答案。它也将使其他有类似问题的人受益:)
    • 完成。我一直在等着看看有没有其他人先有更有创意的解决方法,但看起来不像。
    【解决方案2】:

    我已经看到它成功用于获取 PAT 的所有者: https://docs.microsoft.com/en-us/javascript/api/azure-devops-extension-api/connectiondata

    例如使用 API 调用: https://dev.azure.com/domoreexp/_apis/connectionData?api-version=5.0-preview

    您可以获得authenticatedUser和authorizedUser(不确定有什么区别)

    【讨论】:

      【解决方案3】:

      VSTS 本身无法显示谁创建了 PAT。

      但是如果你知道 PAT 的价值,你可以通过创建一个 wotk 项来找到创建 PAT 的用户。比如你可以使用 PAT 创建一个 Task wotk 项,那么创建 Task wotj 项的用户就是创建 PAT 的个人。

      此外,您可以通过创建user voice 来反馈此功能。

      【讨论】:

      • 不错的解决方法 - 谢谢!我已经发布了一个使用这种方法的脚本的答案。
      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2013-11-01
      • 2018-03-25
      相关资源
      最近更新 更多