Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit 2de3bd9

Browse files
committed
Fixed bug in Enterprise login when not using OAuth
1 parent 4a1d914 commit 2de3bd9

11 files changed

Lines changed: 132 additions & 63 deletions

File tree

CodeHub.Core/Data/GitHubAccount.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ public class GitHubAccount : Account
88
/// <summary>
99
/// Gets or sets the OAuth string
1010
/// </summary>
11-
public string OAuth { get; set; }
11+
public string OAuth { get; set; }
12+
13+
/// <summary>
14+
/// The password which is only used on Enterprise accounts since oAuth does not work.
15+
/// </summary>
16+
/// <value>The password.</value>
17+
public string Password { get; set; }
1218

1319
/// <summary>
1420
/// Gets or sets the domain (API)
@@ -21,6 +27,12 @@ public class GitHubAccount : Account
2127
/// </summary>
2228
/// <value>The web domain.</value>
2329
public string WebDomain { get; set; }
30+
31+
/// <summary>
32+
/// Gets whether this account is enterprise or not
33+
/// </summary>
34+
/// <value><c>true</c> if enterprise; otherwise, <c>false</c>.</value>
35+
public bool IsEnterprise { get; set; }
2436

2537
/// <summary>
2638
/// Gets or sets whether orgs should be listed in the menu controller under 'events'

CodeHub.Core/Services/ILoginService.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ namespace CodeHub.Core.Services
55
{
66
public interface ILoginService
77
{
8-
Task<GitHubSharp.Client> LoginWithToken(string clientId, string clientSecret, string code, string redirect, string requestDomain, string apiDomain);
8+
Task<LoginData> LoginWithToken(string clientId, string clientSecret, string code, string redirect, string requestDomain, string apiDomain, GitHubAccount existingAccount);
99

1010
Task<GitHubSharp.Client> LoginAccount(GitHubAccount account);
1111

12-
GitHubAccount Authenticate(string domain, string user, string pass, string twoFactor);
13-
14-
bool NeedsAuthentication(GitHubAccount account);
12+
LoginData Authenticate(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount existingAccount);
1513
}
14+
15+
public class LoginData
16+
{
17+
public GitHubSharp.Client Client { get; set; }
18+
public GitHubAccount Account { get; set; }
19+
}
1620
}

CodeHub.Core/Services/LoginService.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ public LoginService(IAccountsService accounts)
1616
_accounts = accounts;
1717
}
1818

19-
public async Task<Client> LoginWithToken(string clientId, string clientSecret, string code, string redirect, string requestDomain, string apiDomain)
19+
public async Task<LoginData> LoginWithToken(string clientId, string clientSecret, string code, string redirect, string requestDomain, string apiDomain, GitHubAccount account)
2020
{
2121
var token = await Task.Run(() => Client.RequestAccessToken(clientId, clientSecret, code, redirect, requestDomain));
2222
var client = Client.BasicOAuth(token.AccessToken, apiDomain);
2323
var info = await client.ExecuteAsync(client.AuthenticatedUser.GetInfo());
2424
var username = info.Data.Login;
2525

2626
//Does this user exist?
27-
var account = (GitHubAccount)_accounts.Find(username);
2827
var exists = account != null;
29-
if (!exists)
28+
if (!exists)
3029
account = new GitHubAccount { Username = username };
3130
account.OAuth = token.AccessToken;
3231
account.AvatarUrl = info.Data.AvatarUrl;
@@ -38,13 +37,22 @@ public async Task<Client> LoginWithToken(string clientId, string clientSecret, s
3837
_accounts.Update(account);
3938
else
4039
_accounts.Insert(account);
41-
return client;
40+
return new LoginData { Client = client, Account = account };
4241
}
4342

4443
public async Task<Client> LoginAccount(GitHubAccount account)
4544
{
4645
//Create the client
47-
var client = Client.BasicOAuth(account.OAuth, account.Domain ?? Client.DefaultApi);
46+
Client client = null;
47+
if (!string.IsNullOrEmpty(account.OAuth))
48+
{
49+
client = Client.BasicOAuth(account.OAuth, account.Domain ?? Client.DefaultApi);
50+
}
51+
else if (account.IsEnterprise || !string.IsNullOrEmpty(account.Password))
52+
{
53+
client = Client.Basic(account.Username, account.Password, account.Domain ?? Client.DefaultApi);
54+
}
55+
4856
var data = await client.ExecuteAsync(client.AuthenticatedUser.GetInfo());
4957
var userInfo = data.Data;
5058
account.Username = userInfo.Login;
@@ -54,7 +62,7 @@ public async Task<Client> LoginAccount(GitHubAccount account)
5462
return client;
5563
}
5664

57-
public GitHubAccount Authenticate(string domain, string user, string pass, string twoFactor)
65+
public LoginData Authenticate(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount account)
5866
{
5967
//Fill these variables in during the proceeding try/catch
6068
var apiUrl = domain;
@@ -70,22 +78,30 @@ public GitHubAccount Authenticate(string domain, string user, string pass, strin
7078
throw new ArgumentException("Domain is invalid");
7179

7280
//Does this user exist?
73-
var account = (GitHubAccount)_accounts.Find(user);
7481
bool exists = account != null;
7582
if (!exists)
7683
account = new GitHubAccount { Username = user };
7784

7885
account.Domain = apiUrl;
86+
account.IsEnterprise = enterprise;
7987
var client = twoFactor == null ? Client.Basic(user, pass, apiUrl) : Client.BasicTwoFactorAuthentication(user, pass, twoFactor, apiUrl);
80-
var auth = client.Execute(client.Authorizations.GetOrCreate("72f4fb74bdba774b759d", "9253ab615f8c00738fff5d1c665ca81e581875cb", new System.Collections.Generic.List<string>(Scopes), "CodeHub", null));
81-
account.OAuth = auth.Data.Token;
88+
89+
if (enterprise)
90+
{
91+
account.Password = pass;
92+
}
93+
else
94+
{
95+
var auth = client.Execute(client.Authorizations.GetOrCreate("72f4fb74bdba774b759d", "9253ab615f8c00738fff5d1c665ca81e581875cb", new System.Collections.Generic.List<string>(Scopes), "CodeHub", null));
96+
account.OAuth = auth.Data.Token;
97+
}
8298

8399
if (exists)
84100
_accounts.Update(account);
85101
else
86102
_accounts.Insert(account);
87103

88-
return account;
104+
return new LoginData { Client = client, Account = account };
89105
}
90106
catch (StatusCodeException ex)
91107
{
@@ -96,12 +112,6 @@ public GitHubAccount Authenticate(string domain, string user, string pass, strin
96112
}
97113
}
98114

99-
public bool NeedsAuthentication(GitHubAccount account)
100-
{
101-
var exists = _accounts.Find(account.Username) != null;
102-
return exists == false || string.IsNullOrEmpty(account.OAuth);
103-
}
104-
105115
/// <summary>
106116
/// Goofy exception so we can catch and do two factor auth
107117
/// </summary>

CodeHub.Core/ViewModels/Accounts/AccountsViewModel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ protected async override void SelectAccount(IAccount account)
3030

3131
if (githubAccount.DontRemember)
3232
{
33-
ShowViewModel<LoginViewModel>(LoginViewModel.NavObject.CreateDontRemember(githubAccount));
33+
//Hack for now
34+
if (githubAccount.IsEnterprise || !string.IsNullOrEmpty(githubAccount.Password))
35+
{
36+
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
37+
}
38+
else
39+
{
40+
ShowViewModel<LoginViewModel>(LoginViewModel.NavObject.CreateDontRemember(githubAccount));
41+
}
42+
3443
return;
3544
}
3645

CodeHub.Core/ViewModels/Accounts/AddAccountViewModel.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using System.Windows.Input;
44
using Cirrious.MvvmCross.ViewModels;
@@ -67,8 +67,8 @@ public AddAccountViewModel(IApplicationService application, ILoginService loginS
6767

6868
public void Init(NavObject navObject)
6969
{
70-
if (!string.IsNullOrEmpty(navObject.AttemptedAccountName))
71-
_attemptedAccount = this.GetApplication().Accounts.Find(navObject.AttemptedAccountName) as GitHubAccount;
70+
if (navObject.AttemptedAccountId >= 0)
71+
_attemptedAccount = this.GetApplication().Accounts.Find(navObject.AttemptedAccountId) as GitHubAccount;
7272

7373
if (_attemptedAccount != null)
7474
{
@@ -109,9 +109,10 @@ private async void Login()
109109
try
110110
{
111111
IsLoggingIn = true;
112-
var account = await Task.Run(() => _loginService.Authenticate(apiUrl, Username, Password, TwoFactor));
113-
var client = await Task.Run(() => _loginService.LoginAccount(account));
114-
_application.ActivateUser(account, client);
112+
Console.WriteLine(apiUrl);
113+
var loginData = await Task.Run(() => _loginService.Authenticate(apiUrl, Username, Password, TwoFactor, IsEnterprise, _attemptedAccount));
114+
var client = await Task.Run(() => _loginService.LoginAccount(loginData.Account));
115+
_application.ActivateUser(loginData.Account, client);
115116
}
116117
catch (Exception e)
117118
{
@@ -121,7 +122,7 @@ private async void Login()
121122
if (!(e is LoginService.TwoFactorRequiredException))
122123
{
123124
Password = null;
124-
ReportError("Error attempting to login via new User", e);
125+
ReportError(e);
125126
}
126127

127128
exception = e;
@@ -138,7 +139,12 @@ private async void Login()
138139
public class NavObject
139140
{
140141
public bool IsEnterprise { get; set; }
141-
public string AttemptedAccountName { get; set; }
142+
public int AttemptedAccountId { get; set; }
143+
144+
public NavObject()
145+
{
146+
AttemptedAccountId = int.MinValue;
147+
}
142148
}
143149
}
144150
}

CodeHub.Core/ViewModels/Accounts/LoginViewModel.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ public class LoginViewModel : BaseViewModel
1414
public static readonly string RedirectUri = "http://dillonbuchanan.com/";
1515
private readonly ILoginService _loginService;
1616

17-
private string _username;
18-
public string Username
19-
{
20-
get { return _username; }
21-
set
22-
{
23-
_username = value;
24-
RaisePropertyChanged(() => Username);
25-
}
26-
}
27-
2817
private bool _isLoggingIn;
2918
public bool IsLoggingIn
3019
{
@@ -54,23 +43,21 @@ public string LoginUrl
5443

5544
public ICommand GoToOldLoginWaysCommand
5645
{
57-
get { return new MvxCommand(() => ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = IsEnterprise, AttemptedAccountName = Username })); }
46+
get { return new MvxCommand(() => ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = IsEnterprise })); }
5847
}
5948

6049
public ICommand GoBackCommand
6150
{
6251
get { return new MvxCommand(() => ChangePresentation(new MvxClosePresentationHint(this))); }
6352
}
6453

65-
6654
public LoginViewModel(ILoginService loginService)
6755
{
6856
_loginService = loginService;
6957
}
7058

7159
public void Init(NavObject navObject)
7260
{
73-
Username = navObject.Username;
7461
IsEnterprise = navObject.IsEnterprise;
7562
WebDomain = navObject.WebDomain;
7663

@@ -79,9 +66,9 @@ public void Init(NavObject navObject)
7966
WebDomain = GitHubSharp.Client.AccessTokenUri;
8067
}
8168

82-
if (navObject.Revalidation)
69+
if (navObject.AttemptedAccountId >= 0)
8370
{
84-
AttemptedAccount = this.GetApplication().Accounts.Find(navObject.Username) as GitHubAccount;
71+
AttemptedAccount = this.GetApplication().Accounts.Find(navObject.AttemptedAccountId) as GitHubAccount;
8572

8673
//This is a hack to get around the fact that WebDomain will be null for Enterprise users since the last version did not contain the variable
8774
if (WebDomain == null && IsEnterprise)
@@ -121,9 +108,9 @@ public async void Login(string code)
121108
try
122109
{
123110
IsLoggingIn = true;
124-
var client = await _loginService.LoginWithToken(ClientId, ClientSecret, code, RedirectUri, WebDomain, apiUrl);
125-
var account = this.GetApplication().Accounts.Find(client.Username) as GitHubAccount;
126-
this.GetApplication().ActivateUser(account, client);
111+
var account = AttemptedAccount;
112+
var data = await _loginService.LoginWithToken(ClientId, ClientSecret, code, RedirectUri, WebDomain, apiUrl, account);
113+
this.GetApplication().ActivateUser(data.Account, data.Client);
127114
}
128115
catch (Exception e)
129116
{
@@ -139,17 +126,22 @@ public class NavObject
139126
{
140127
public string Username { get; set; }
141128
public bool IsEnterprise { get; set; }
142-
public bool Revalidation { get; set; }
143129
public string WebDomain { get; set; }
130+
public int AttemptedAccountId { get; set; }
131+
132+
public NavObject()
133+
{
134+
AttemptedAccountId = int.MinValue;
135+
}
144136

145137
public static NavObject CreateDontRemember(GitHubAccount account)
146138
{
147139
return new NavObject
148140
{
149141
WebDomain = account.WebDomain,
150142
IsEnterprise = !string.Equals(account.Domain, GitHubSharp.Client.DefaultApi),
151-
Revalidation = true,
152-
Username = account.Username
143+
Username = account.Username,
144+
AttemptedAccountId = account.Id
153145
};
154146
}
155147
}

CodeHub.Core/ViewModels/Accounts/NewAccountViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public ICommand GoToDotComLoginCommand
1313

1414
public ICommand GoToEnterpriseLoginCommand
1515
{
16-
get { return new MvxCommand(() => this.ShowViewModel<LoginViewModel>(new LoginViewModel.NavObject { IsEnterprise = true })); }
16+
get { return new MvxCommand(() => this.ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true })); }
1717
}
1818
}
1919
}

CodeHub.Core/ViewModels/App/StartupViewModel.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ protected async override void Startup()
3636
if (account.DontRemember)
3737
{
3838
ShowViewModel<Accounts.AccountsViewModel>();
39-
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
39+
40+
//Hack for now
41+
if (account.IsEnterprise || !string.IsNullOrEmpty(account.Password))
42+
{
43+
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
44+
}
45+
else
46+
{
47+
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
48+
}
49+
4050
return;
4151
}
4252

CodeHub.iOS/CodeHub.iOS.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@
228228
<Reference Include="UserVoice">
229229
<HintPath>..\lib\CodeFramework\lib\iOS\UserVoice.dll</HintPath>
230230
</Reference>
231+
<Reference Include="Cirrious.MvvmCross.Plugins.Messenger">
232+
<HintPath>..\lib\CodeFramework\lib\iOS\Cirrious.MvvmCross.Plugins.Messenger.dll</HintPath>
233+
</Reference>
231234
</ItemGroup>
232235
<ItemGroup>
233236
<Content Include="Images\anonymous%402x.png" />

0 commit comments

Comments
 (0)