通过 API 发现 VSTS PAT 的所有者

Discover owner of VSTS PAT via API

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

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

仅使用 PAT,我可以点击的 VSTS API 中是否有任何 "who-am-i" 类型的端点可以回显用户名、guid 或其他PAT 在其中定义的帐户的详细信息?

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

干杯,

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

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

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

根据 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>"
...

P.S。我在此处发布了请求 "who-am-i" 端点的 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

我已经看到它成功地用于获取 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(不确定有什么区别)