Skip to content

Commit 7dec930

Browse files
committed
gitlab: simplify terminal/interaction checks
Simplify where we check for user interaction and/or terminal prompts during authentication, to only need to check at the start of each `GetAuthenticationAsync` call. Note that is is a behaviour change - we now require user interaction to be allowed to perform authentication even with only 1 mode available. This is technically more 'correct' because when the user disables interactivity, we should never require interaction to continue. Although we are not asking the user to select a mode of authentication, we still either need them to enter a username/password, or respond to a browser OAuth flow.
1 parent 2a37974 commit 7dec930

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

src/shared/GitLab.Tests/GitLabAuthenticationTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ await Assert.ThrowsAsync<ArgumentException>("modes",
2020

2121
[Theory]
2222
[InlineData(AuthenticationModes.Browser)]
23-
public async Task GitLabAuthentication_GetAuthenticationAsync_SingleChoice_TerminalAndInteractionNotRequired(GitLab.AuthenticationModes modes)
23+
public async Task GitLabAuthentication_GetAuthenticationAsync_SingleChoice_InteractionStillRequired(GitLab.AuthenticationModes modes)
2424
{
2525
var context = new TestCommandContext();
26-
context.Settings.IsTerminalPromptsEnabled = false;
27-
context.Settings.IsInteractionAllowed = false;
26+
context.Settings.IsInteractionAllowed = true;
2827
context.SessionManager.IsDesktopSession = true; // necessary for browser
2928
context.Settings.IsGuiPromptsEnabled = false;
3029
var auth = new GitLabAuthentication(context);

src/shared/GitLab/GitLabAuthentication.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public async Task<AuthenticationPromptResult> GetAuthenticationAsync(Uri targetU
7070
throw new ArgumentException(@$"Must specify at least one {nameof(AuthenticationModes)}", nameof(modes));
7171
}
7272

73+
ThrowIfUserInteractionDisabled();
74+
7375
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession)
7476
{
7577
if (TryFindHelperCommand(out string helperCommand, out string args))
@@ -132,11 +134,11 @@ private async Task<AuthenticationPromptResult> GetAuthenticationViaUiAsync(
132134

133135
private AuthenticationPromptResult GetAuthenticationViaTty(Uri targetUri, string userName, AuthenticationModes modes)
134136
{
137+
ThrowIfTerminalPromptsDisabled();
138+
135139
switch (modes)
136140
{
137141
case AuthenticationModes.Basic:
138-
ThrowIfUserInteractionDisabled();
139-
ThrowIfTerminalPromptsDisabled();
140142
Context.Terminal.WriteLine("Enter GitLab credentials for '{0}'...", targetUri);
141143

142144
if (string.IsNullOrWhiteSpace(userName))
@@ -152,8 +154,6 @@ private AuthenticationPromptResult GetAuthenticationViaTty(Uri targetUri, string
152154
return new AuthenticationPromptResult(AuthenticationModes.Basic, new GitCredential(userName, password));
153155

154156
case AuthenticationModes.Pat:
155-
ThrowIfUserInteractionDisabled();
156-
ThrowIfTerminalPromptsDisabled();
157157
Context.Terminal.WriteLine("Enter GitLab credentials for '{0}'...", targetUri);
158158

159159
if (string.IsNullOrWhiteSpace(userName))
@@ -176,8 +176,6 @@ private AuthenticationPromptResult GetAuthenticationViaTty(Uri targetUri, string
176176
@$"At least one {nameof(AuthenticationModes)} must be supplied");
177177

178178
default:
179-
ThrowIfUserInteractionDisabled();
180-
ThrowIfTerminalPromptsDisabled();
181179
var menuTitle = $"Select an authentication method for '{targetUri}'";
182180
var menu = new TerminalMenu(Context.Terminal, menuTitle);
183181

0 commit comments

Comments
 (0)