Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f506e42

Browse files
committed
Merge branch 'fixes/1332-validate-scopes' into feature/sso
2 parents b929d55 + b0ff058 commit f506e42

File tree

7 files changed

+36
-13
lines changed

7 files changed

+36
-13
lines changed

src/GitHub.App/ViewModels/LoginControlViewModel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ void UpdateLoginMode()
8686

8787
foreach (var connection in connectionManager.Connections)
8888
{
89-
result &= ~((connection.HostAddress == HostAddress.GitHubDotComHostAddress) ?
90-
LoginMode.DotComOnly : LoginMode.EnterpriseOnly);
89+
if (connection.IsLoggedIn)
90+
{
91+
result &= ~((connection.HostAddress == HostAddress.GitHubDotComHostAddress) ?
92+
LoginMode.DotComOnly : LoginMode.EnterpriseOnly);
93+
}
9194
}
9295

9396
LoginMode = result;

src/GitHub.App/ViewModels/LoginTabViewModel.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,20 @@ protected IObservable<AuthenticationResult> LogInToHost(HostAddress hostAddress)
144144
{
145145
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
146146

147-
return Observable.Defer(() =>
147+
return Observable.Defer(async () =>
148148
{
149-
return hostAddress != null ?
150-
ConnectionManager.LogIn(hostAddress, UsernameOrEmail, Password)
151-
.ToObservable()
152-
.Select(_ => AuthenticationResult.Success)
153-
: Observable.Return(AuthenticationResult.CredentialFailure);
149+
if (hostAddress != null)
150+
{
151+
if (await ConnectionManager.GetConnection(hostAddress) != null)
152+
{
153+
await ConnectionManager.LogOut(hostAddress);
154+
}
155+
156+
await ConnectionManager.LogIn(hostAddress, UsernameOrEmail, Password);
157+
return Observable.Return(AuthenticationResult.Success);
158+
}
159+
160+
return Observable.Return(AuthenticationResult.CredentialFailure);
154161
})
155162
.ObserveOn(RxApp.MainThreadScheduler)
156163
.Do(authResult => {

src/GitHub.Exports.Reactive/Extensions/ConnectionManagerExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public static IObservable<bool> IsLoggedIn(this IConnection connection)
4242
return Observable.Return(connection?.IsLoggedIn ?? false);
4343
}
4444

45+
public static IObservable<IConnection> GetConnection(this IConnectionManager cm, HostAddress address)
46+
{
47+
Guard.ArgumentNotNull(cm, nameof(cm));
48+
Guard.ArgumentNotNull(address, nameof(address));
49+
50+
return cm.GetLoadedConnections()
51+
.ToObservable()
52+
.Select(x => x.FirstOrDefault(y => y.HostAddress == address));
53+
}
54+
4555
public static IObservable<IConnection> GetLoggedInConnections(this IConnectionManager cm)
4656
{
4757
Guard.ArgumentNotNull(cm, nameof(cm));

src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void RefreshConnections(object sender, NotifyCollectionChangedEventArgs e)
174174

175175
protected void Refresh(IConnection connection)
176176
{
177-
if (connection == null)
177+
if (connection == null || !connection.IsLoggedIn)
178178
{
179179
LoggedIn = false;
180180
IsVisible = false;

src/GitHub.TeamFoundation.14/Connect/GitHubInvitationSection.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Windows;
1212
using System.Windows.Media;
1313
using GitHub.VisualStudio.UI;
14+
using System.Linq;
1415

1516
namespace GitHub.VisualStudio.TeamExplorer.Connect
1617
{
@@ -40,9 +41,9 @@ public GitHubInvitationSection(IGitHubServiceProvider serviceProvider, IConnecti
4041
OnThemeChanged();
4142
};
4243

43-
IsVisible = cm.Connections.Count == 0;
44+
IsVisible = !cm.Connections.Where(x => x.IsLoggedIn).Any();
4445

45-
cm.Connections.CollectionChanged += (s, e) => IsVisible = cm.Connections.Count == 0;
46+
cm.Connections.CollectionChanged += (s, e) => IsVisible = !cm.Connections.Where(x => x.IsLoggedIn).Any();
4647
}
4748

4849
public override void Connect()

src/GitHub.VisualStudio/Services/ConnectionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public async Task<IConnection> LogIn(HostAddress address, string userName, strin
7575

7676
if (conns.Any(x => x.HostAddress == address))
7777
{
78-
throw new InvalidOperationException($"A connection to {address} already exists.");
78+
throw new InvalidOperationException($"A connection to {address.Title} already exists.");
7979
}
8080

8181
var client = CreateClient(address);
@@ -116,7 +116,7 @@ public async Task LogOut(HostAddress address)
116116

117117
if (connection == null)
118118
{
119-
throw new KeyNotFoundException($"Could not find a connection to {address}.");
119+
throw new KeyNotFoundException($"Could not find a connection to {address.Title}.");
120120
}
121121

122122
var client = CreateClient(address);

test/UnitTests/GitHub.App/ViewModels/LoginControlViewModelTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public void LoginModeTracksAvailableConnections()
9696
connectionManager.Connections.Returns(connections);
9797
gitHubConnection.HostAddress.Returns(HostAddress.GitHubDotComHostAddress);
9898
enterpriseConnection.HostAddress.Returns(HostAddress.Create("https://enterprise.url"));
99+
gitHubConnection.IsLoggedIn.Returns(true);
100+
enterpriseConnection.IsLoggedIn.Returns(true);
99101

100102
var loginViewModel = new LoginControlViewModel(connectionManager, gitHubLogin, enterpriseLogin);
101103

0 commit comments

Comments
 (0)