Skip to content

Commit b3711de

Browse files
User/mstannah/ nouser should not disable IWA authmode (#155)
* Enable IWA for Non interactive auth * Adding a period * Uuse IWA only on windows * Remove platform specific condition * Combined auth mode return platform specific auth mode * Updating platform specific test case * log warnings specific to platform * Removing the platform win attributes
1 parent 3b0bfd3 commit b3711de

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

src/AzureAuth.Test/CommandMainTest.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,29 +725,57 @@ public void TestGenerateEvent_From_AuthFlowResult_With_TokenResult_And_Null_Erro
725725
[TestCase("non-empty-string", false)]
726726
[TestCase("true", false)]
727727
[TestCase("", false)]
728-
public void TestPCA_IsDisabledOnCorextEnvVar(string corextNonInteractive, bool expected)
728+
public void InteractiveAuth_IsDisabledOnCorextEnvVar(string corextNonInteractive, bool expected)
729729
{
730730
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
731731
this.envMock.Setup(e => e.Get("Corext_NonInteractive")).Returns(corextNonInteractive);
732-
subject.PCADisabled().Should().Be(expected);
732+
subject.InteractiveAuthDisabled().Should().Be(expected);
733733
}
734734

735735
[TestCase("1", true)]
736736
[TestCase("non-empty-string", true)]
737737
[TestCase("true", true)]
738738
[TestCase("", false)]
739-
public void TestPCA_IsDisabledOnNoUserEnvVar(string noUser, bool expected)
739+
public void InteractiveAuth_IsDisabledOnNoUserEnvVar(string noUser, bool expected)
740740
{
741741
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
742742
this.envMock.Setup(e => e.Get("AZUREAUTH_NO_USER")).Returns(noUser);
743-
subject.PCADisabled().Should().Be(expected);
743+
subject.InteractiveAuthDisabled().Should().Be(expected);
744744
}
745745

746746
[Test]
747-
public void PCA_IsEnabledIfEnvVarsAreNotSet()
747+
public void InteractiveAuth_IsEnabledIfEnvVarsAreNotSet()
748748
{
749749
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
750-
subject.PCADisabled().Should().BeFalse();
750+
subject.InteractiveAuthDisabled().Should().BeFalse();
751+
}
752+
753+
#if PlatformWindows
754+
[TestCase("non-empty-string")]
755+
public void GetCombinedAuthMode_withInteractiveAuthDisabled(string noUser)
756+
{
757+
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
758+
this.envMock.Setup(e => e.Get("AZUREAUTH_NO_USER")).Returns(noUser);
759+
subject.CombinedAuthMode.Should().Be(AuthMode.IWA);
760+
}
761+
762+
public void GetCombinedAuthMode_withInteractiveAuthEnabled()
763+
{
764+
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
765+
var authModes = new List<AuthMode>();
766+
authModes.Add(AuthMode.Broker);
767+
subject.AuthModes = authModes;
768+
subject.CombinedAuthMode.Should().Be(AuthMode.Broker);
769+
}
770+
#endif
771+
772+
public void GetCombinedAuthMode_withInteractiveAuthEnabled_NonWindowsPlatform()
773+
{
774+
CommandMain subject = this.serviceProvider.GetService<CommandMain>();
775+
var authModes = new List<AuthMode>();
776+
authModes.Add(AuthMode.Web);
777+
subject.AuthModes = authModes;
778+
subject.CombinedAuthMode.Should().Be(AuthMode.Web);
751779
}
752780

753781
/// <summary>

src/AzureAuth/CommandMain.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,25 @@ public Alias TokenFetcherOptions
217217
get { return this.authSettings; }
218218
}
219219

220-
private AuthMode CombinedAuthMode => this.AuthModes.Aggregate((a1, a2) => a1 | a2);
220+
/// <summary>
221+
/// Gets the CombinedAuthMode depending on env variables to disable interactive auth modes.
222+
/// </summary>
223+
public AuthMode CombinedAuthMode
224+
{
225+
get
226+
{
227+
if (this.InteractiveAuthDisabled())
228+
{
229+
#if PlatformWindows
230+
return AuthMode.IWA;
231+
#else
232+
return 0;
233+
#endif
234+
}
235+
236+
return this.AuthModes.Aggregate((a1, a2) => a1 | a2);
237+
}
238+
}
221239

222240
/// <summary>
223241
/// Combine the <see cref="PromptHintPrefix"/> with the caller provided prompt hint.
@@ -359,11 +377,14 @@ public int OnExecute()
359377
// Small bug in Lasso - Add does not accept a null IEnumerable here.
360378
this.eventData.Add("settings_scopes", this.authSettings.Scopes ?? new List<string>());
361379

362-
if (this.PCADisabled())
380+
if (this.InteractiveAuthDisabled())
363381
{
364-
this.eventData.Add("no_user", true);
365-
this.logger.LogCritical($"User based authentication is disabled");
366-
return 1;
382+
this.eventData.Add(EnvVars.CorextNonInteractive, this.env.Get(EnvVars.CorextNonInteractive));
383+
this.eventData.Add(EnvVars.NoUser, this.env.Get(EnvVars.NoUser));
384+
this.logger.LogWarning($"Interactive authentication is disabled.");
385+
#if PlatformWindows
386+
this.logger.LogWarning($"Supported auth mode is Integrated Windows Authentication");
387+
#endif
367388
}
368389

369390
return this.ClearCache ? this.ClearLocalCache() : this.GetToken();
@@ -373,7 +394,7 @@ public int OnExecute()
373394
/// Determines whether Public Client Authentication (PCA) is disabled or not.
374395
/// </summary>
375396
/// <returns>A boolean to indicate PCA is disabled.</returns>
376-
public bool PCADisabled()
397+
public bool InteractiveAuthDisabled()
377398
{
378399
return !string.IsNullOrEmpty(this.env.Get(EnvVars.NoUser)) ||
379400
string.Equals("1", this.env.Get(EnvVars.CorextNonInteractive));

0 commit comments

Comments
 (0)