@@ -74,22 +74,23 @@ public async Task<LoginResultData> Login(
74
74
keychain . Connect ( host ) ;
75
75
keychain . SetCredentials ( new Credential ( host , username , password ) ) ;
76
76
77
- string token ;
78
77
try
79
78
{
80
- token = await TryLogin ( host , username , password ) ;
81
- if ( string . IsNullOrEmpty ( token ) )
79
+ var loginResultData = await TryLogin ( host , username , password ) ;
80
+ if ( loginResultData . Code == LoginResultCodes . Success || loginResultData . Code == LoginResultCodes . CodeRequired )
82
81
{
83
- throw new InvalidOperationException ( "Returned token is null or empty" ) ;
82
+ if ( string . IsNullOrEmpty ( loginResultData . Token ) )
83
+ {
84
+ throw new InvalidOperationException ( "Returned token is null or empty" ) ;
85
+ }
86
+
87
+ keychain . SetToken ( host , loginResultData . Token ) ;
88
+ await keychain . Save ( host ) ;
89
+
90
+ return loginResultData ;
84
91
}
85
- }
86
- catch ( TwoFactorRequiredException e )
87
- {
88
- LoginResultCodes result ;
89
- result = LoginResultCodes . CodeRequired ;
90
- logger . Trace ( "2FA TwoFactorAuthorizationException: {0} {1}" , LoginResultCodes . CodeRequired , e . Message ) ;
91
92
92
- return new LoginResultData ( result , e . Message , host , password ) ;
93
+ return loginResultData ;
93
94
}
94
95
catch ( Exception e )
95
96
{
@@ -98,41 +99,40 @@ public async Task<LoginResultData> Login(
98
99
await keychain . Clear ( host , false ) ;
99
100
return new LoginResultData ( LoginResultCodes . Failed , Localization . LoginFailed , host ) ;
100
101
}
101
-
102
- keychain . SetToken ( host , token ) ;
103
- await keychain . Save ( host ) ;
104
-
105
- return new LoginResultData ( LoginResultCodes . Success , "Success" , host ) ;
106
102
}
107
103
108
104
public async Task < LoginResultData > ContinueLogin ( LoginResultData loginResultData , string twofacode )
109
105
{
110
- var token = loginResultData . Token ;
111
106
var host = loginResultData . Host ;
112
107
var keychainAdapter = keychain . Connect ( host ) ;
113
108
var username = keychainAdapter . Credential . Username ;
114
109
var password = keychainAdapter . Credential . Token ;
115
110
try
116
111
{
117
112
logger . Trace ( "2FA Continue" ) ;
118
- token = await TryContinueLogin ( host , username , password , twofacode ) ;
113
+ loginResultData = await TryContinueLogin ( host , username , password , twofacode ) ;
119
114
120
- if ( string . IsNullOrEmpty ( token ) )
115
+ if ( loginResultData . Code == LoginResultCodes . Success )
121
116
{
122
- throw new InvalidOperationException ( "Returned token is null or empty" ) ;
123
- }
117
+ if ( string . IsNullOrEmpty ( loginResultData . Token ) )
118
+ {
119
+ throw new InvalidOperationException ( "Returned token is null or empty" ) ;
120
+ }
124
121
125
- keychain . SetToken ( host , token ) ;
126
- await keychain . Save ( host ) ;
122
+ keychain . SetToken ( host , loginResultData . Token ) ;
123
+ await keychain . Save ( host ) ;
127
124
128
- return new LoginResultData ( LoginResultCodes . Success , "" , host ) ;
125
+ return loginResultData ;
126
+ }
127
+
128
+ return loginResultData ;
129
129
}
130
130
catch ( Exception e )
131
131
{
132
- logger . Trace ( e , "Exception: {0}" , e . Message ) ;
132
+ logger . Warning ( e , "Login Exception" ) ;
133
133
134
134
await keychain . Clear ( host , false ) ;
135
- return new LoginResultData ( LoginResultCodes . Failed , e . Message , host ) ;
135
+ return new LoginResultData ( LoginResultCodes . Failed , Localization . LoginFailed , host ) ;
136
136
}
137
137
}
138
138
@@ -144,7 +144,7 @@ public async Task Logout(UriString hostAddress)
144
144
await new ActionTask ( keychain . Clear ( hostAddress , true ) ) . StartAwait ( ) ;
145
145
}
146
146
147
- private async Task < string > TryLogin (
147
+ private async Task < LoginResultData > TryLogin (
148
148
UriString host ,
149
149
string username ,
150
150
string password
@@ -174,35 +174,33 @@ string password
174
174
175
175
if ( ret . IsSuccess )
176
176
{
177
- return ret . Output [ 0 ] ;
177
+ return new LoginResultData ( LoginResultCodes . Success , null , host , ret . Output [ 0 ] ) ;
178
178
}
179
179
180
180
if ( ret . IsTwoFactorRequired )
181
181
{
182
- keychain . SetToken ( host , ret . Output [ 0 ] ) ;
183
- await keychain . Save ( host ) ;
184
- throw new TwoFactorRequiredException ( ) ;
182
+ return new LoginResultData ( LoginResultCodes . CodeRequired , "Two Factor Required." , host , ret . Output [ 0 ] ) ;
185
183
}
186
184
187
185
if ( ret . IsBadCredentials )
188
186
{
189
- throw new Exception ( "Bad credentials." ) ;
187
+ return new LoginResultData ( LoginResultCodes . Failed , "Bad credentials." , host , ret . Output [ 0 ] ) ;
190
188
}
191
189
192
190
if ( ret . IsLocked )
193
191
{
194
- throw new Exception ( "Account locked." ) ;
192
+ return new LoginResultData ( LoginResultCodes . LockedOut , "Account locked." , host , ret . Output [ 0 ] ) ;
195
193
}
196
194
197
195
if ( ret . Output . Any ( ) )
198
196
{
199
- throw new Exception ( string . Join ( Environment . NewLine , ret . Output ) ) ;
197
+ return new LoginResultData ( LoginResultCodes . Failed , "Failed." , host , ret . Output [ 0 ] ) ;
200
198
}
201
199
202
- throw new Exception ( "Authentication failed" ) ;
200
+ return new LoginResultData ( LoginResultCodes . Failed , "Failed." , host ) ;
203
201
}
204
202
205
- private async Task < string > TryContinueLogin (
203
+ private async Task < LoginResultData > TryContinueLogin (
206
204
UriString host ,
207
205
string username ,
208
206
string password ,
@@ -234,32 +232,30 @@ string code
234
232
235
233
if ( ret . IsSuccess )
236
234
{
237
- return ret . Output [ 0 ] ;
235
+ return new LoginResultData ( LoginResultCodes . Success , null , host , ret . Output [ 0 ] ) ;
238
236
}
239
237
240
238
if ( ret . IsTwoFactorRequired )
241
239
{
242
- keychain . SetToken ( host , ret . Output [ 0 ] ) ;
243
- await keychain . Save ( host ) ;
244
- throw new TwoFactorRequiredException ( ) ;
240
+ return new LoginResultData ( LoginResultCodes . CodeFailed , "Incorrect code. Two Factor Required." , host , ret . Output [ 0 ] ) ;
245
241
}
246
242
247
243
if ( ret . IsBadCredentials )
248
244
{
249
- throw new Exception ( "Bad credentials." ) ;
245
+ return new LoginResultData ( LoginResultCodes . Failed , "Bad credentials." , host , ret . Output [ 0 ] ) ;
250
246
}
251
247
252
248
if ( ret . IsLocked )
253
249
{
254
- throw new Exception ( "Account locked." ) ;
250
+ return new LoginResultData ( LoginResultCodes . LockedOut , "Account locked." , host , ret . Output [ 0 ] ) ;
255
251
}
256
252
257
253
if ( ret . Output . Any ( ) )
258
254
{
259
- throw new Exception ( string . Join ( Environment . NewLine , ret . Output ) ) ;
255
+ return new LoginResultData ( LoginResultCodes . Failed , "Failed." , host , ret . Output [ 0 ] ) ;
260
256
}
261
257
262
- throw new Exception ( "Authentication failed" ) ;
258
+ return new LoginResultData ( LoginResultCodes . Failed , "Failed." , host ) ;
263
259
}
264
260
}
265
261
@@ -284,10 +280,4 @@ internal LoginResultData(LoginResultCodes code, string message, UriString host)
284
280
{
285
281
}
286
282
}
287
-
288
- class TwoFactorRequiredException : Exception
289
- {
290
- public TwoFactorRequiredException ( ) : base ( "Two-Factor authentication required." )
291
- { }
292
- }
293
283
}
0 commit comments