11using System ;
22using System . ComponentModel . Composition ;
33using System . Reactive ;
4+ using System . Reactive . Concurrency ;
45using System . Reactive . Linq ;
56using System . Threading . Tasks ;
67using GitHub . Api ;
@@ -29,6 +30,16 @@ public LoginToGitHubForEnterpriseViewModel(
2930 ISimpleApiClientFactory apiClientFactory ,
3031 IEnterpriseCapabilitiesService enterpriseCapabilities ,
3132 IVisualStudioBrowser browser )
33+ : this ( connectionManager , apiClientFactory , enterpriseCapabilities , browser , Scheduler . Default )
34+ {
35+ }
36+
37+ public LoginToGitHubForEnterpriseViewModel (
38+ IConnectionManager connectionManager ,
39+ ISimpleApiClientFactory apiClientFactory ,
40+ IEnterpriseCapabilitiesService enterpriseCapabilities ,
41+ IVisualStudioBrowser browser ,
42+ IScheduler scheduler )
3243 : base ( connectionManager , browser )
3344 {
3445 Guard . ArgumentNotNull ( connectionManager , nameof ( connectionManager ) ) ;
@@ -44,19 +55,19 @@ public LoginToGitHubForEnterpriseViewModel(
4455 . IfNotUri ( Resources . EnterpriseUrlValidatorInvalid )
4556 . IfGitHubDotComHost ( Resources . EnterpriseUrlValidatorNotAGitHubHost ) ;
4657
47- canLogin = this . WhenAny (
58+ canLogin = this . WhenAnyValue (
4859 x => x . UsernameOrEmailValidator . ValidationResult . IsValid ,
4960 x => x . PasswordValidator . ValidationResult . IsValid ,
50- x => x . EnterpriseUrlValidator . ValidationResult . IsValid ,
51- ( x , y , z ) => x . Value && y . Value && z . Value )
61+ x => x . SupportedLoginMethods ,
62+ ( x , y , z ) => ( x || ( z & EnterpriseLoginMethods . Token ) != 0 ) && y )
5263 . ToProperty ( this , x => x . CanLogin ) ;
5364
5465 canSsoLogin = this . WhenAnyValue (
5566 x => x . EnterpriseUrlValidator . ValidationResult . IsValid )
5667 . ToProperty ( this , x => x . CanLogin ) ;
5768
5869 this . WhenAnyValue ( x => x . EnterpriseUrl , x => x . EnterpriseUrlValidator . ValidationResult )
59- . Throttle ( TimeSpan . FromMilliseconds ( 500 ) )
70+ . Throttle ( TimeSpan . FromMilliseconds ( 500 ) , scheduler )
6071 . ObserveOn ( RxApp . MainThreadScheduler )
6172 . Subscribe ( x => EnterpriseUrlChanged ( x . Item1 , x . Item2 ? . IsValid ?? false ) ) ;
6273
@@ -69,7 +80,14 @@ public LoginToGitHubForEnterpriseViewModel(
6980
7081 protected override Task < IConnection > LogIn ( object args )
7182 {
72- return LogInToHost ( HostAddress . Create ( EnterpriseUrl ) ) ;
83+ if ( string . IsNullOrWhiteSpace ( UsernameOrEmail ) )
84+ {
85+ return LogInWithToken ( HostAddress . Create ( EnterpriseUrl ) , Password ) ;
86+ }
87+ else
88+ {
89+ return LogInToHost ( HostAddress . Create ( EnterpriseUrl ) ) ;
90+ }
7391 }
7492
7593 protected override Task < IConnection > LogInViaOAuth ( object args )
@@ -145,9 +163,27 @@ async void EnterpriseUrlChanged(string url, bool valid)
145163
146164 if ( url == EnterpriseUrl )
147165 {
166+ if ( ( loginMethods & EnterpriseLoginMethods . Token ) != 0 &&
167+ ( loginMethods & EnterpriseLoginMethods . UsernameAndPassword ) != 0 )
168+ {
169+ loginMethods &= ~ EnterpriseLoginMethods . Token ;
170+ }
171+
148172 ProbeStatus = enterpriseInstance ? EnterpriseProbeStatus . Valid : EnterpriseProbeStatus . Invalid ;
149173 SupportedLoginMethods = loginMethods ;
150174 }
151175 }
176+
177+ async Task < IConnection > LogInWithToken ( HostAddress hostAddress , string token )
178+ {
179+ Guard . ArgumentNotNull ( hostAddress , nameof ( hostAddress ) ) ;
180+
181+ if ( await ConnectionManager . GetConnection ( hostAddress ) != null )
182+ {
183+ await ConnectionManager . LogOut ( hostAddress ) ;
184+ }
185+
186+ return await ConnectionManager . LogInWithToken ( hostAddress , token ) ;
187+ }
152188 }
153189}
0 commit comments