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

Commit 083d8a2

Browse files
committed
Add a delay to the login process.
We're getting a lot of reports of failed logins, and looking at the logs it appears that using a token immediately after it's given out can fail. Add a delay of 1s (I tried less but this seems to give the best results) before reading `/user` after authorization.
1 parent cdd69b3 commit 083d8a2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/GitHub.App/Models/RepositoryHost.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
122122
// Try to get an authorization token, save it, then get the user to log in:
123123
.SelectMany(fingerprint => ApiClient.GetOrCreateApplicationAuthenticationCode(interceptingTwoFactorChallengeHandler))
124124
.SelectMany(saveAuthorizationToken)
125-
.SelectMany(_ => GetUserFromApi())
125+
.SelectMany(_ => GetUserFromApiWithDelay())
126126
.Catch<UserAndScopes, ApiException>(firstTryEx =>
127127
{
128128
var exception = firstTryEx as AuthorizationException;
@@ -163,7 +163,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
163163
})
164164
// Then save the authorization token (if there is one) and get the user:
165165
.SelectMany(saveAuthorizationToken)
166-
.SelectMany(_ => GetUserFromApi());
166+
.SelectMany(_ => GetUserFromApiWithDelay());
167167
}
168168

169169
return Observable.Throw<UserAndScopes>(firstTryEx);
@@ -178,7 +178,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
178178
// instead of 404 to signal that it's not allowed. In the name of backwards compatibility we
179179
// test for both 404 (NotFoundException) and 403 (ForbiddenException) here.
180180
if (isEnterprise && (retryEx is NotFoundException || retryEx is ForbiddenException || retryEx.StatusCode == (HttpStatusCode)422))
181-
return GetUserFromApi();
181+
return GetUserFromApiWithDelay();
182182

183183
// Other errors are "real" so we pass them along:
184184
return Observable.Throw<UserAndScopes>(retryEx);
@@ -268,6 +268,15 @@ IObservable<AuthenticationResult> LoginWithApiUser(UserAndScopes userAndScopes)
268268
});
269269
}
270270

271+
IObservable<UserAndScopes> GetUserFromApiWithDelay()
272+
{
273+
// It seems that attempting to use a token immediately sometimes fails, add a delay
274+
// of 1s to allow the token to propagate.
275+
return Observable.Defer(() =>
276+
Observable.Timer(TimeSpan.FromMilliseconds(1000))
277+
.SelectMany(_ => ApiClient.GetUser()));
278+
}
279+
271280
IObservable<UserAndScopes> GetUserFromApi()
272281
{
273282
return Observable.Defer(() => ApiClient.GetUser());

0 commit comments

Comments
 (0)