Skip to content

Commit 5fe5b35

Browse files
committed
github: support multiple accounts with explicit prompt
Check for multiple existing accounts, and if there is more than one account with credentials in the store AND no user name hint provided in the remote URL, then show the new account selection prompt.
1 parent 483d6d3 commit 5fe5b35

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/shared/GitHub/GitHubHostProvider.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,53 @@ private static string GetServiceName(Uri baseUri)
126126

127127
public async Task<ICredential> GetCredentialAsync(InputArguments input)
128128
{
129-
// Try and locate an existing credential in the OS credential store
130129
string service = GetServiceName(input);
131-
_context.Trace.WriteLine($"Looking for existing credential in store with service={service} account={input.UserName}...");
130+
Uri remoteUri = input.GetRemoteUri();
131+
132+
// If we have a specific username then we can try and find an existing credential for that account.
133+
// If not, we should check what accounts are available in the store and prompt the user if there
134+
// are multiple options.
135+
string userName = null;
136+
bool addAccount = false;
137+
if (string.IsNullOrWhiteSpace(input.UserName))
138+
{
139+
IList<string> accounts = _context.CredentialStore.GetAccounts(service);
140+
_context.Trace.WriteLine($"Found {accounts.Count} accounts in the store for service={service}.");
141+
142+
switch (accounts.Count)
143+
{
144+
case 1:
145+
userName = accounts[0];
146+
break;
147+
148+
case > 1:
149+
userName = await _gitHubAuth.SelectAccountAsync(remoteUri, accounts);
150+
addAccount = userName is null;
151+
break;
152+
}
153+
}
154+
155+
// Always try and locate an existing credential in the OS credential store
156+
// unless we're being told to explicitly add a new account. If the account lookup
157+
// failed above we should still try to lookup an existing credential.
158+
ICredential credential = null;
159+
if (addAccount)
160+
{
161+
_context.Trace.WriteLine("Adding a new account!");
162+
}
163+
else
164+
{
165+
_context.Trace.WriteLine($"Looking for existing credential in store with service={service} account={userName}...");
166+
credential = _context.CredentialStore.Get(service, userName);
167+
}
132168

133-
ICredential credential = _context.CredentialStore.Get(service, input.UserName);
134169
if (credential == null)
135170
{
136171
_context.Trace.WriteLine("No existing credentials found.");
137172

138173
// No existing credential was found, create a new one
139174
_context.Trace.WriteLine("Creating new credential...");
140-
credential = await GenerateCredentialAsync(input.GetRemoteUri(), input.UserName);
175+
credential = await GenerateCredentialAsync(remoteUri, input.UserName);
141176
_context.Trace.WriteLine("Credential created.");
142177
}
143178
else

0 commit comments

Comments
 (0)