Skip to content

Commit d4fcfb7

Browse files
committed
bitbucket: update shared UI helper code for new UI model
Update shared UI helper code for Bitbucket to remove the separate OAuth prompt/command, and allow basic authentication (username/password) options to be hidden based on a command-line option.
1 parent bf068e1 commit d4fcfb7

File tree

7 files changed

+51
-149
lines changed

7 files changed

+51
-149
lines changed

src/shared/Atlassian.Bitbucket.Tests/BitbucketAuthenticationTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public async Task BitbucketAuthentication_GetCredentialsAsync_AllModes_NoUser_BB
122122
["password"] = expectedPassword
123123
};
124124

125-
string expectedArgs = $"userpass --show-oauth";
125+
string expectedArgs = $"prompt --show-basic --show-oauth";
126126

127127
var context = new TestCommandContext();
128128
context.SessionManager.IsDesktopSession = true; // Enable OAuth and UI helper selection
@@ -158,7 +158,7 @@ public async Task BitbucketAuthentication_GetCredentialsAsync_BasicOnly_User_BBC
158158
["password"] = expectedPassword
159159
};
160160

161-
string expectedArgs = $"userpass --username {expectedUserName}";
161+
string expectedArgs = $"prompt --username {expectedUserName} --show-basic";
162162

163163
var context = new TestCommandContext();
164164
context.SessionManager.IsDesktopSession = true; // Enable UI helper selection
@@ -194,7 +194,7 @@ public async Task BitbucketAuthentication_GetCredentialsAsync_AllModes_NoUser_BB
194194
["password"] = expectedPassword
195195
};
196196

197-
string expectedArgs = $"userpass --url {targetUri} --show-oauth";
197+
string expectedArgs = $"prompt --url {targetUri} --show-basic --show-oauth";
198198

199199
var context = new TestCommandContext();
200200
context.SessionManager.IsDesktopSession = true; // Enable OAuth and UI helper selection

src/shared/Atlassian.Bitbucket.Tests/BitbucketHostProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ private void VerifyOAuthFlowRan(InputArguments input, string token)
475475
{
476476
var remoteUri = input.GetRemoteUri();
477477

478-
// use refresh token to get new access token and refresh token
478+
// Get new access token and refresh token
479479
bitbucketAuthentication.Verify(m => m.CreateOAuthCredentialsAsync(remoteUri), Times.Once);
480480

481481
// Check access token works/resolve username

src/shared/Atlassian.Bitbucket.UI/Commands/CredentialsCommand.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Atlassian.Bitbucket.UI.Commands
1313
public abstract class CredentialsCommand : HelperCommand
1414
{
1515
protected CredentialsCommand(ICommandContext context)
16-
: base(context, "userpass", "Show authentication prompt.")
16+
: base(context, "prompt", "Show authentication prompt.")
1717
{
1818
AddOption(
1919
new Option<string>("--url", "Bitbucket Server or Data Center URL")
@@ -27,40 +27,51 @@ protected CredentialsCommand(ICommandContext context)
2727
new Option("--show-oauth", "Show OAuth option.")
2828
);
2929

30-
Handler = CommandHandler.Create<Uri, string, bool>(ExecuteAsync);
30+
AddOption(
31+
new Option("--show-basic", "Show username/password option.")
32+
);
33+
34+
Handler = CommandHandler.Create<Uri, string, bool, bool>(ExecuteAsync);
3135
}
3236

33-
private async Task<int> ExecuteAsync(Uri url, string userName, bool showOAuth)
37+
private async Task<int> ExecuteAsync(Uri url, string userName, bool showOAuth, bool showBasic)
3438
{
3539
var viewModel = new CredentialsViewModel(Context.Environment)
3640
{
3741
Url = url,
3842
UserName = userName,
39-
ShowOAuth = showOAuth
43+
ShowOAuth = showOAuth,
44+
ShowBasic = showBasic
4045
};
4146

4247
await ShowAsync(viewModel, CancellationToken.None);
4348

44-
if (!viewModel.WindowResult)
49+
if (!viewModel.WindowResult || viewModel.SelectedMode == AuthenticationModes.None)
4550
{
4651
throw new Exception("User cancelled dialog.");
4752
}
4853

49-
if (viewModel.UseOAuth)
54+
switch (viewModel.SelectedMode)
5055
{
51-
WriteResult(new Dictionary<string, string>
52-
{
53-
["mode"] = "oauth"
54-
});
55-
}
56-
else
57-
{
58-
WriteResult(new Dictionary<string, string>
59-
{
60-
["mode"] = "basic",
61-
["username"] = viewModel.UserName,
62-
["password"] = viewModel.Password,
63-
});
56+
case AuthenticationModes.OAuth:
57+
WriteResult(new Dictionary<string, string>
58+
{
59+
["mode"] = "oauth"
60+
});
61+
break;
62+
63+
case AuthenticationModes.Basic:
64+
WriteResult(new Dictionary<string, string>
65+
{
66+
["mode"] = "basic",
67+
["username"] = viewModel.UserName,
68+
["password"] = viewModel.Password,
69+
});
70+
break;
71+
72+
default:
73+
throw new ArgumentOutOfRangeException(nameof(AuthenticationModes),
74+
"Unknown authentication mode", viewModel.SelectedMode.ToString());
6475
}
6576

6677
return 0;

src/shared/Atlassian.Bitbucket.UI/Commands/OAuthCommand.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/shared/Atlassian.Bitbucket.UI/ViewModels/CredentialsViewModel.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class CredentialsViewModel : WindowViewModel
1515
private string _userName;
1616
private string _password;
1717
private bool _showOAuth;
18+
private bool _showBasic;
1819

1920
public CredentialsViewModel()
2021
{
@@ -28,7 +29,7 @@ public CredentialsViewModel(IEnvironment environment)
2829
_environment = environment;
2930

3031
Title = "Connect to Bitbucket";
31-
LoginCommand = new RelayCommand(Accept, CanLogin);
32+
LoginCommand = new RelayCommand(AcceptBasic, CanLogin);
3233
CancelCommand = new RelayCommand(Cancel);
3334
OAuthCommand = new RelayCommand(AcceptOAuth, CanAcceptOAuth);
3435
ForgotPasswordCommand = new RelayCommand(ForgotPassword);
@@ -53,9 +54,15 @@ private bool CanLogin()
5354
return !string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password);
5455
}
5556

57+
private void AcceptBasic()
58+
{
59+
SelectedMode = AuthenticationModes.Basic;
60+
Accept();
61+
}
62+
5663
private void AcceptOAuth()
5764
{
58-
UseOAuth = true;
65+
SelectedMode = AuthenticationModes.OAuth;
5966
Accept();
6067
}
6168

@@ -101,7 +108,7 @@ public string Password
101108
}
102109

103110
/// <summary>
104-
/// Show the direct-to-OAuth button.
111+
/// Show the OAuth option.
105112
/// </summary>
106113
public bool ShowOAuth
107114
{
@@ -110,14 +117,16 @@ public bool ShowOAuth
110117
}
111118

112119
/// <summary>
113-
/// User indicated a preference to use OAuth authentication over username/password.
120+
/// Show the basic authentication options.
114121
/// </summary>
115-
public bool UseOAuth
122+
public bool ShowBasic
116123
{
117-
get;
118-
private set;
124+
get => _showBasic;
125+
set => SetAndRaisePropertyChanged(ref _showBasic, value);
119126
}
120127

128+
public AuthenticationModes SelectedMode { get; private set; }
129+
121130
/// <summary>
122131
/// Start the process to validate the username/password
123132
/// </summary>

src/shared/Atlassian.Bitbucket.UI/ViewModels/OAuthViewModel.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/shared/Atlassian.Bitbucket/BitbucketAuthentication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public async Task<CredentialsPromptResult> GetCredentialsAsync(Uri targetUri, st
9090
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession &&
9191
TryFindHelperExecutablePath(out string helperPath))
9292
{
93-
var cmdArgs = new StringBuilder("userpass");
93+
var cmdArgs = new StringBuilder("prompt");
9494
if (!BitbucketHostProvider.IsBitbucketOrg(targetUri))
9595
{
9696
cmdArgs.AppendFormat(" --url {0}", QuoteCmdArg(targetUri.ToString()));

0 commit comments

Comments
 (0)