Skip to content

Commit b627044

Browse files
committed
msauth: rename GetTokenAsync to GetTokenForUserAsync
Rename the lone GetToken method to clarify that this is for user principals (regular user identities). This is in preparation for adding extra principal types including service principals, and managed identities. Also add some XML doc comments to the method.
1 parent 6676986 commit b627044

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/shared/Core.Tests/Authentication/MicrosoftAuthenticationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitCredentialManager.Tests.Authentication
88
public class MicrosoftAuthenticationTests
99
{
1010
[Fact]
11-
public async System.Threading.Tasks.Task MicrosoftAuthentication_GetAccessTokenAsync_NoInteraction_ThrowsException()
11+
public async System.Threading.Tasks.Task MicrosoftAuthentication_GetTokenForUserAsync_NoInteraction_ThrowsException()
1212
{
1313
const string authority = "https://login.microsoftonline.com/common";
1414
const string clientId = "C9E8FDA6-1D46-484C-917C-3DBD518F27C3";
@@ -24,7 +24,7 @@ public async System.Threading.Tasks.Task MicrosoftAuthentication_GetAccessTokenA
2424
var msAuth = new MicrosoftAuthentication(context);
2525

2626
await Assert.ThrowsAsync<Trace2InvalidOperationException>(
27-
() => msAuth.GetTokenAsync(authority, clientId, redirectUri, scopes, userName, false));
27+
() => msAuth.GetTokenForUserAsync(authority, clientId, redirectUri, scopes, userName, false));
2828
}
2929
}
3030
}

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ namespace GitCredentialManager.Authentication
2323
{
2424
public interface IMicrosoftAuthentication
2525
{
26-
Task<IMicrosoftAuthenticationResult> GetTokenAsync(string authority, string clientId, Uri redirectUri,
26+
/// <summary>
27+
/// Acquire an access token for a user principal.
28+
/// </summary>
29+
/// <param name="authority">Azure authority.</param>
30+
/// <param name="clientId">Client ID.</param>
31+
/// <param name="redirectUri">Redirect URI for the client.</param>
32+
/// <param name="scopes">Set of scopes to request.</param>
33+
/// <param name="userName">Optional user name for an existing account.</param>
34+
/// <param name="msaPt">Use MSA-Passthrough behavior when authenticating.</param>
35+
/// <returns>Authentication result.</returns>
36+
Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(string authority, string clientId, Uri redirectUri,
2737
string[] scopes, string userName, bool msaPt = false);
2838
}
2939

@@ -59,7 +69,7 @@ public MicrosoftAuthentication(ICommandContext context)
5969

6070
#region IMicrosoftAuthentication
6171

62-
public async Task<IMicrosoftAuthenticationResult> GetTokenAsync(
72+
public async Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
6373
string authority, string clientId, Uri redirectUri, string[] scopes, string userName, bool msaPt)
6474
{
6575
// Check if we can and should use OS broker authentication

src/shared/Microsoft.AzureRepos.Tests/AzureReposHostProviderTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_CachedAuthority_
170170
azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl);
171171

172172
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
173-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, urlAccount, true))
173+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, urlAccount, true))
174174
.ReturnsAsync(authResult);
175175

176176
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -219,7 +219,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_CachedAuthority_
219219
azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl);
220220

221221
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
222-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, urlAccount, true))
222+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, urlAccount, true))
223223
.ReturnsAsync(authResult);
224224

225225
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -268,7 +268,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_CachedAuthority_
268268
azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl);
269269

270270
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
271-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
271+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
272272
.ReturnsAsync(authResult);
273273

274274
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -315,7 +315,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_CachedAuthority_
315315
var azDevOpsMock = new Mock<IAzureDevOpsRestApi>(MockBehavior.Strict);
316316

317317
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
318-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
318+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
319319
.ReturnsAsync(authResult);
320320

321321
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -363,7 +363,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_CachedAuthority_
363363
var azDevOpsMock = new Mock<IAzureDevOpsRestApi>(MockBehavior.Strict);
364364

365365
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
366-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, account, true))
366+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, account, true))
367367
.ReturnsAsync(authResult);
368368

369369
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -413,7 +413,7 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_NoCachedAuthorit
413413
azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl);
414414

415415
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
416-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
416+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
417417
.ReturnsAsync(authResult);
418418

419419
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
@@ -462,7 +462,7 @@ public async Task AzureReposProvider_GetCredentialAsync_PatMode_NoExistingPat_Ge
462462
.ReturnsAsync(personalAccessToken);
463463

464464
var msAuthMock = new Mock<IMicrosoftAuthentication>(MockBehavior.Strict);
465-
msAuthMock.Setup(x => x.GetTokenAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
465+
msAuthMock.Setup(x => x.GetTokenForUserAsync(authorityUrl, expectedClientId, expectedRedirectUri, expectedScopes, null, true))
466466
.ReturnsAsync(authResult);
467467

468468
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);

src/shared/Microsoft.AzureRepos/AzureReposHostProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private async Task<ICredential> GeneratePersonalAccessTokenAsync(InputArguments
197197

198198
// Get an AAD access token for the Azure DevOps SPS
199199
_context.Trace.WriteLine("Getting Azure AD access token...");
200-
IMicrosoftAuthenticationResult result = await _msAuth.GetTokenAsync(
200+
IMicrosoftAuthenticationResult result = await _msAuth.GetTokenForUserAsync(
201201
authAuthority,
202202
GetClientId(),
203203
GetRedirectUri(),
@@ -289,7 +289,7 @@ private async Task<IMicrosoftAuthenticationResult> GetAzureAccessTokenAsync(Inpu
289289

290290
// Get an AAD access token for the Azure DevOps SPS
291291
_context.Trace.WriteLine("Getting Azure AD access token...");
292-
IMicrosoftAuthenticationResult result = await _msAuth.GetTokenAsync(
292+
IMicrosoftAuthenticationResult result = await _msAuth.GetTokenForUserAsync(
293293
authAuthority,
294294
GetClientId(),
295295
GetRedirectUri(),

0 commit comments

Comments
 (0)