Skip to content

Commit d59c2f5

Browse files
hickfordMatthew John Cheetham
andcommitted
Apply suggestions from code review
Co-authored-by: Matthew John Cheetham <[email protected]>
1 parent 96af0f8 commit d59c2f5

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Secure platform credential storage|&#10003;<br/>[(see more)](docs/credstores.md)
2525
Multi-factor authentication support for Azure DevOps|&#10003;|&#10003;|&#10003;
2626
Two-factor authentication support for GitHub|&#10003;|&#10003;|&#10003;
2727
Two-factor authentication support for Bitbucket|&#10003;|&#10003;|&#10003;
28+
Two-factor authentication support for GitLab|&#10003;|&#10003;|&#10003;
2829
Windows Integrated Authentication (NTLM/Kerberos) support|&#10003;|_N/A_|_N/A_
2930
Basic HTTP authentication support|&#10003;|&#10003;|&#10003;
3031
Proxy support|&#10003;|&#10003;|&#10003;

src/shared/GitLab.Tests/GitLabAuthenticationTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ public void GitLabAuthentication_GetAuthenticationAsync_Terminal()
5757
Assert.Equal(AuthenticationModes.Browser, result.AuthenticationMode);
5858
}
5959

60+
[Fact]
61+
public void GitLabAuthentication_GetAuthenticationAsync_ChoosePat()
62+
{
63+
var context = new TestCommandContext();
64+
var auth = new GitLabAuthentication(context);
65+
context.Terminal.Prompts["option (enter for default)"] = "";
66+
context.Terminal.Prompts["Username"] = "username";
67+
context.Terminal.SecretPrompts["Personal access token"] = "token";
68+
var result = auth.GetAuthentication(null, null, AuthenticationModes.All);
69+
Assert.Equal(AuthenticationModes.Pat, result.AuthenticationMode);
70+
Assert.Equal("username", result.Credential.Account);
71+
Assert.Equal("token", result.Credential.Password);
72+
}
73+
74+
[Fact]
75+
public void GitLabAuthentication_GetAuthenticationAsync_ChooseBasic()
76+
{
77+
var context = new TestCommandContext();
78+
var auth = new GitLabAuthentication(context);
79+
context.Terminal.Prompts["option (enter for default)"] = "2";
80+
context.Terminal.Prompts["Username"] = "username";
81+
context.Terminal.SecretPrompts["Password"] = "password";
82+
var result = auth.GetAuthentication(null, null, AuthenticationModes.All);
83+
Assert.Equal(AuthenticationModes.Basic, result.AuthenticationMode);
84+
Assert.Equal("username", result.Credential.Account);
85+
Assert.Equal("password", result.Credential.Password);
86+
}
87+
6088
[Fact]
6189
public void GitLabAuthentication_GetAuthenticationAsync_AuthenticationModesAll_RequiresInteraction()
6290
{

src/shared/GitLab/GitLabAuthentication.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ public AuthenticationPromptResult GetAuthentication(Uri targetUri, string userNa
7171
switch (modes)
7272
{
7373
case AuthenticationModes.Basic:
74+
ThrowIfUserInteractionDisabled();
75+
ThrowIfTerminalPromptsDisabled();
76+
Context.Terminal.WriteLine("Enter GitLab credentials for '{0}'...", targetUri);
77+
78+
if (string.IsNullOrWhiteSpace(userName))
79+
{
80+
userName = Context.Terminal.Prompt("Username");
81+
}
82+
else
83+
{
84+
Context.Terminal.WriteLine("Username: {0}", userName);
85+
}
86+
87+
string password = Context.Terminal.PromptSecret("Password");
88+
return new AuthenticationPromptResult(AuthenticationModes.Basic, new GitCredential(userName, password));
89+
7490
case AuthenticationModes.Pat:
7591
ThrowIfUserInteractionDisabled();
7692
ThrowIfTerminalPromptsDisabled();
@@ -85,8 +101,8 @@ public AuthenticationPromptResult GetAuthentication(Uri targetUri, string userNa
85101
Context.Terminal.WriteLine("Username: {0}", userName);
86102
}
87103

88-
string password_or_token = Context.Terminal.PromptSecret(modes == AuthenticationModes.Basic ? "Password" : "Personal access token");
89-
return new AuthenticationPromptResult(modes, new GitCredential(userName, password_or_token));
104+
string token = Context.Terminal.PromptSecret("Personal access token");
105+
return new AuthenticationPromptResult(AuthenticationModes.Pat, new GitCredential(userName, token));
90106

91107
case AuthenticationModes.Browser:
92108
return new AuthenticationPromptResult(AuthenticationModes.Browser);

src/shared/GitLab/GitLabHostProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public override async Task<ICredential> GetCredentialAsync(InputArguments input)
160160
}
161161

162162
string refreshService = GetRefreshTokenServiceName(input);
163-
string refreshToken = Context.CredentialStore.Get(service, input.UserName)?.Password;
163+
string refreshToken = Context.CredentialStore.Get(refreshService, input.UserName)?.Password;
164164
if (refreshToken != null)
165165
{
166166
Context.Trace.WriteLine("Refreshing OAuth token...");
@@ -183,7 +183,7 @@ public override async Task<ICredential> GetCredentialAsync(InputArguments input)
183183
// store credential, since we know it to be valid (whereas Git will only store credential if git push succeeds)
184184
Context.CredentialStore.AddOrUpdate(service, oAuthCredential.Account, oAuthCredential.AccessToken);
185185
// store refresh token under a separate service
186-
Context.CredentialStore.AddOrUpdate(GetRefreshTokenServiceName(input), oAuthCredential.Account, oAuthCredential.RefreshToken);
186+
Context.CredentialStore.AddOrUpdate(refreshService, oAuthCredential.Account, oAuthCredential.RefreshToken);
187187
}
188188
return credential;
189189
}

0 commit comments

Comments
 (0)