forked from git-ecosystem/git-credential-manager
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCredentialsViewModel.cs
More file actions
155 lines (128 loc) · 4.34 KB
/
CredentialsViewModel.cs
File metadata and controls
155 lines (128 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.ComponentModel;
using System.Windows.Input;
using GitCredentialManager;
using GitCredentialManager.UI;
using GitCredentialManager.UI.ViewModels;
namespace Atlassian.Bitbucket.UI.ViewModels
{
public class CredentialsViewModel : WindowViewModel
{
private readonly ISessionManager _sessionManager;
private Uri _url;
private string _userName;
private string _password;
private bool _showOAuth;
private bool _showBasic;
public CredentialsViewModel()
{
// Constructor the XAML designer
}
public CredentialsViewModel(ISessionManager sessionManager)
{
EnsureArgument.NotNull(sessionManager, nameof(sessionManager));
_sessionManager = sessionManager;
Title = "Connect to Bitbucket";
LoginCommand = new RelayCommand(AcceptBasic, CanLogin);
CancelCommand = new RelayCommand(Cancel);
OAuthCommand = new RelayCommand(AcceptOAuth, CanAcceptOAuth);
ForgotPasswordCommand = new RelayCommand(ForgotPassword);
SignUpCommand = new RelayCommand(SignUp);
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(UserName):
case nameof(Password):
LoginCommand.RaiseCanExecuteChanged();
break;
}
}
private bool CanLogin()
{
return !string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password);
}
private void AcceptBasic()
{
SelectedMode = AuthenticationModes.Basic;
Accept();
}
private void AcceptOAuth()
{
SelectedMode = AuthenticationModes.OAuth;
Accept();
}
private bool CanAcceptOAuth()
{
return ShowOAuth;
}
private void ForgotPassword()
{
Uri passwordResetUri = _url is null
? new Uri(BitbucketConstants.HelpUrls.PasswordReset)
: new Uri(_url, BitbucketConstants.HelpUrls.DataCenterPasswordReset);
_sessionManager.OpenBrowser(passwordResetUri);
}
private void SignUp()
{
Uri signUpUri = _url is null
? new Uri(BitbucketConstants.HelpUrls.SignUp)
: new Uri(_url, BitbucketConstants.HelpUrls.DataCenterLogin);
_sessionManager.OpenBrowser(signUpUri);
}
public Uri Url
{
get => _url;
set => SetAndRaisePropertyChanged(ref _url, value);
}
public string UserName
{
get => _userName;
set => SetAndRaisePropertyChanged(ref _userName, value);
}
public string Password
{
get => _password;
set => SetAndRaisePropertyChanged(ref _password, value);
}
/// <summary>
/// Show the OAuth option.
/// </summary>
public bool ShowOAuth
{
get => _showOAuth;
set => SetAndRaisePropertyChanged(ref _showOAuth, value);
}
/// <summary>
/// Show the basic authentication options.
/// </summary>
public bool ShowBasic
{
get => _showBasic;
set => SetAndRaisePropertyChanged(ref _showBasic, value);
}
public AuthenticationModes SelectedMode { get; private set; }
/// <summary>
/// Start the process to validate the username/password
/// </summary>
public RelayCommand LoginCommand { get; }
/// <summary>
/// Cancel the authentication attempt.
/// </summary>
public ICommand CancelCommand { get; }
/// <summary>
/// Use OAuth authentication instead of username/password.
/// </summary>
public ICommand OAuthCommand { get; }
/// <summary>
/// Hyperlink to the Bitbucket forgotten password process.
/// </summary>
public ICommand ForgotPasswordCommand { get; }
/// <summary>
/// Hyperlink to the Bitbucket sign up process.
/// </summary>
public ICommand SignUpCommand { get; }
}
}