Skip to content

Commit

Permalink
- added online validation of local devops pat
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-schutte committed Jun 18, 2024
1 parent eb92593 commit ad45f8a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Nox.Cli.PersonalAccessToken/AzDevOpsPatProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<string> GetPat(string accessToken)
if (!string.IsNullOrEmpty(cachedPat))
{
var pat = JsonConvert.DeserializeObject<AzDevOpsPat>(cachedPat);
if (pat!.ValidTo < DateTime.Now)
if (pat!.ValidTo < DateTime.Now || !await ValidatePat(accessToken, pat.Token!))
{
pat = await GetOrCreateOnlinePat(accessToken);
}
Expand Down Expand Up @@ -64,6 +64,16 @@ private async Task<AzDevOpsPat> GetOrCreateOnlinePat(string accessToken)
}
}

private async Task<bool> ValidatePat(string accessToken, string pat)
{
var onlinePats = await GetOnlinePatList(accessToken);
if (onlinePats == null) return false;
var existing = onlinePats.PatTokens!.FirstOrDefault(p => p.Token == pat);
if (existing == null) return false;
if (existing.ValidTo < DateTime.Now) return false;
return true;
}

private async Task<AzDevOpsPatList?> GetOnlinePatList(string accessToken)
{
var client = new RestClient($"https://vssps.dev.azure.com/{_organization}/_apis/tokens/pats?api-version=7.1-preview.1", options =>
Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Nox.Cli": {
"commandName": "Project",
"commandLineArgs": "new solution",
"commandLineArgs": "new dop",
"workingDirectory": "/home/jan/demo/CliDemo",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down

0 comments on commit ad45f8a

Please sign in to comment.