Skip to content

Commit 99dea8e

Browse files
committed
msauth: let caller select if MSA-PT behaviour is used
Let the caller in to the `IMicrosoftAuthentication` component decide if Microsoft Account Passthrough (MSA-PT) behaviour should be used. Azure DevOps requires MSA-PT, so set that to `true` in usages.
1 parent 685a9f6 commit 99dea8e

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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));
27+
() => msAuth.GetTokenAsync(authority, clientId, redirectUri, scopes, userName, false));
2828
}
2929
}
3030
}

src/shared/Core/Authentication/MicrosoftAuthentication.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace GitCredentialManager.Authentication
2323
public interface IMicrosoftAuthentication
2424
{
2525
Task<IMicrosoftAuthenticationResult> GetTokenAsync(string authority, string clientId, Uri redirectUri,
26-
string[] scopes, string userName);
26+
string[] scopes, string userName, bool msaPt = false);
2727
}
2828

2929
public interface IMicrosoftAuthenticationResult
@@ -59,7 +59,7 @@ public MicrosoftAuthentication(ICommandContext context)
5959
#region IMicrosoftAuthentication
6060

6161
public async Task<IMicrosoftAuthenticationResult> GetTokenAsync(
62-
string authority, string clientId, Uri redirectUri, string[] scopes, string userName)
62+
string authority, string clientId, Uri redirectUri, string[] scopes, string userName, bool msaPt)
6363
{
6464
// Check if we can and should use OS broker authentication
6565
bool useBroker = CanUseBroker();
@@ -70,7 +70,7 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenAsync(
7070
try
7171
{
7272
// Create the public client application for authentication
73-
IPublicClientApplication app = await CreatePublicClientApplicationAsync(authority, clientId, redirectUri, useBroker);
73+
IPublicClientApplication app = await CreatePublicClientApplicationAsync(authority, clientId, redirectUri, useBroker, msaPt);
7474

7575
AuthenticationResult result = null;
7676

@@ -308,7 +308,7 @@ private async Task<AuthenticationResult> GetAccessTokenSilentlyAsync(IPublicClie
308308
}
309309

310310
private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
311-
string authority, string clientId, Uri redirectUri, bool enableBroker)
311+
string authority, string clientId, Uri redirectUri, bool enableBroker, bool msaPt)
312312
{
313313
var httpFactoryAdaptor = new MsalHttpClientFactoryAdaptor(Context.HttpClientFactory);
314314

@@ -370,7 +370,7 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
370370
new BrokerOptions(BrokerOptions.OperatingSystems.Windows)
371371
{
372372
Title = "Git Credential Manager",
373-
MsaPassthrough = true,
373+
MsaPassthrough = msaPt,
374374
}
375375
);
376376
#endif

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))
173+
msAuthMock.Setup(x => x.GetTokenAsync(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))
222+
msAuthMock.Setup(x => x.GetTokenAsync(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))
271+
msAuthMock.Setup(x => x.GetTokenAsync(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))
318+
msAuthMock.Setup(x => x.GetTokenAsync(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))
366+
msAuthMock.Setup(x => x.GetTokenAsync(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))
416+
msAuthMock.Setup(x => x.GetTokenAsync(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))
465+
msAuthMock.Setup(x => x.GetTokenAsync(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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ private async Task<ICredential> GeneratePersonalAccessTokenAsync(InputArguments
202202
GetClientId(),
203203
GetRedirectUri(),
204204
AzureDevOpsConstants.AzureDevOpsDefaultScopes,
205-
null);
205+
null,
206+
msaPt: true);
206207
_context.Trace.WriteLineSecrets(
207208
$"Acquired Azure access token. Account='{result.AccountUpn}' Token='{{0}}'", new object[] {result.AccessToken});
208209

@@ -293,7 +294,8 @@ private async Task<IMicrosoftAuthenticationResult> GetAzureAccessTokenAsync(Inpu
293294
GetClientId(),
294295
GetRedirectUri(),
295296
AzureDevOpsConstants.AzureDevOpsDefaultScopes,
296-
userName);
297+
userName,
298+
msaPt: true);
297299
_context.Trace.WriteLineSecrets(
298300
$"Acquired Azure access token. Account='{result.AccountUpn}' Token='{{0}}'", new object[] {result.AccessToken});
299301

0 commit comments

Comments
 (0)