Skip to content

Commit 238331d

Browse files
authored
Merge pull request #1680 from emrebasar/prepare_request_on_retries
Re-Prepare request for every retry
2 parents a84a000 + 3544ce6 commit 238331d

File tree

11 files changed

+523
-1
lines changed

11 files changed

+523
-1
lines changed

src/main/java/org/kohsuke/github/GitHubClient.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ public <T> GitHubResponse<T> sendRequest(GitHubRequest request, @CheckForNull Bo
423423
throws IOException {
424424
int retries = CONNECTION_ERROR_RETRIES;
425425
GitHubConnectorRequest connectorRequest = prepareConnectorRequest(request);
426-
427426
do {
428427
GitHubConnectorResponse connectorResponse = null;
429428
try {
@@ -461,6 +460,7 @@ private void detectKnownErrors(GitHubConnectorResponse connectorResponse,
461460
boolean detectStatusCodeError) throws IOException {
462461
detectOTPRequired(connectorResponse);
463462
detectInvalidCached404Response(connectorResponse, request);
463+
detectExpiredToken(connectorResponse, request);
464464
detectRedirect(connectorResponse);
465465
if (rateLimitHandler.isError(connectorResponse)) {
466466
rateLimitHandler.onError(connectorResponse);
@@ -474,6 +474,22 @@ private void detectKnownErrors(GitHubConnectorResponse connectorResponse,
474474
}
475475
}
476476

477+
private void detectExpiredToken(GitHubConnectorResponse connectorResponse, GitHubRequest request)
478+
throws IOException {
479+
if (connectorResponse.statusCode() != HTTP_UNAUTHORIZED) {
480+
return;
481+
}
482+
String originalAuthorization = connectorResponse.request().header("Authorization");
483+
if (Objects.isNull(originalAuthorization) || originalAuthorization.isEmpty()) {
484+
return;
485+
}
486+
GitHubConnectorRequest updatedRequest = prepareConnectorRequest(request);
487+
String updatedAuthorization = updatedRequest.header("Authorization");
488+
if (!originalAuthorization.equals(updatedAuthorization)) {
489+
throw new RetryRequestException(updatedRequest);
490+
}
491+
}
492+
477493
private void detectRedirect(GitHubConnectorResponse connectorResponse) throws IOException {
478494
if (connectorResponse.statusCode() == HTTP_MOVED_PERM || connectorResponse.statusCode() == HTTP_MOVED_TEMP) {
479495
// GitHubClient depends on GitHubConnector implementations to follow any redirects automatically
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.kohsuke.github.extras.authorization;
2+
3+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4+
import org.junit.Test;
5+
import org.kohsuke.github.AbstractGitHubWireMockTest;
6+
import org.kohsuke.github.GHUser;
7+
import org.kohsuke.github.RateLimitHandler;
8+
import org.kohsuke.github.authorization.AuthorizationProvider;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* Test authorization token refresh.
14+
*/
15+
public class AuthorizationTokenRefreshTest extends AbstractGitHubWireMockTest {
16+
17+
/**
18+
* Instantiates a new test.
19+
*/
20+
public AuthorizationTokenRefreshTest() {
21+
useDefaultGitHub = false;
22+
}
23+
24+
/**
25+
* Gets the wire mock options.
26+
*
27+
* @return the wire mock options
28+
*/
29+
@Override
30+
protected WireMockConfiguration getWireMockOptions() {
31+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
32+
}
33+
34+
/**
35+
* Retried request should get new token when the old one expires.
36+
*
37+
* @throws IOException
38+
* the exception
39+
*/
40+
@Test
41+
public void testRetriedRequestGetsNewAuthorizationTokenWhenOldOneExpires() throws IOException {
42+
snapshotNotAllowed();
43+
gitHub = getGitHubBuilder().withAuthorizationProvider(new RefreshingAuthorizationProvider())
44+
.withEndpoint(mockGitHub.apiServer().baseUrl())
45+
.withRateLimitHandler(RateLimitHandler.WAIT)
46+
.build();
47+
final GHUser kohsuke = gitHub.getUser("kohsuke");
48+
assertThat("Usernames match", "kohsuke".equals(kohsuke.getLogin()));
49+
}
50+
51+
/**
52+
* Retried request should not get new token when the old one is still valid.
53+
*
54+
* @throws IOException
55+
* the exception
56+
*/
57+
@Test
58+
public void testRetriedRequestDoesNotGetNewAuthorizationTokenWhenOldOneIsStillValid() throws IOException {
59+
gitHub = getGitHubBuilder().withAuthorizationProvider(() -> "original token")
60+
.withEndpoint(mockGitHub.apiServer().baseUrl())
61+
.withRateLimitHandler(RateLimitHandler.WAIT)
62+
.build();
63+
final GHUser kohsuke = gitHub.getUser("kohsuke");
64+
assertThat("Usernames match", "kohsuke".equals(kohsuke.getLogin()));
65+
}
66+
67+
static class RefreshingAuthorizationProvider implements AuthorizationProvider {
68+
private boolean used = false;
69+
70+
@Override
71+
public String getEncodedAuthorization() {
72+
if (used) {
73+
return "refreshed token";
74+
}
75+
used = true;
76+
return "original token";
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "kohsuke",
3+
"id": 50003,
4+
"node_id": "MDQ6VXNlcjUwMDAz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/50003?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/kohsuke",
8+
"html_url": "https://github.com/kohsuke",
9+
"followers_url": "https://api.github.com/users/kohsuke/followers",
10+
"following_url": "https://api.github.com/users/kohsuke/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/kohsuke/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/kohsuke/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/kohsuke/subscriptions",
14+
"organizations_url": "https://api.github.com/users/kohsuke/orgs",
15+
"repos_url": "https://api.github.com/users/kohsuke/repos",
16+
"events_url": "https://api.github.com/users/kohsuke/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/kohsuke/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Kohsuke Kawaguchi",
21+
"company": "@launchableinc ",
22+
"blog": "https://www.kohsuke.org/",
23+
"location": "San Jose, California",
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 263,
29+
"public_gists": 113,
30+
"followers": 2110,
31+
"following": 3,
32+
"created_at": "2009-01-28T18:53:21Z",
33+
"updated_at": "2023-08-09T13:37:19Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"id": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
3+
"name": "users_kohsuke",
4+
"scenarioName": "Retry with valid credentials",
5+
"requiredScenarioState": "Started",
6+
"newScenarioState": "Retry with the same credentials",
7+
"request": {
8+
"url": "/users/kohsuke",
9+
"method": "GET",
10+
"headers": {
11+
"Accept": {
12+
"equalTo": "application/vnd.github.v3+json"
13+
},
14+
"Authorization": {
15+
"equalTo": "original token"
16+
}
17+
}
18+
},
19+
"response": {
20+
"status": 403,
21+
"headers": {
22+
"Server": "github.com",
23+
"Date": "Thu, 10 Aug 2023 09:12:37 GMT",
24+
"Content-Type": "application/json; charset=utf-8",
25+
"Cache-Control": "public, max-age=60, s-maxage=60",
26+
"Vary": [
27+
"Accept",
28+
"Accept-Encoding, Accept, X-Requested-With"
29+
],
30+
"ETag": "W/\"e2b462e6fe85486beb06033fe196d7c7b7669a96ddbb8f411b91f56288b31b3b\"",
31+
"Last-Modified": "Wed, 09 Aug 2023 13:37:19 GMT",
32+
"X-GitHub-Media-Type": "github.v3; format=json",
33+
"x-github-api-version-selected": "2022-11-28",
34+
"X-RateLimit-Limit": "60",
35+
"X-RateLimit-Remaining": "0",
36+
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
37+
"X-RateLimit-Used": "1",
38+
"X-RateLimit-Resource": "core",
39+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
40+
"Access-Control-Allow-Origin": "*",
41+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
42+
"X-Frame-Options": "deny",
43+
"X-Content-Type-Options": "nosniff",
44+
"X-XSS-Protection": "0",
45+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
46+
"Content-Security-Policy": "default-src 'none'",
47+
"X-GitHub-Request-Id": "BDE4:73C9:BF78362:C13B808:64D4AA05"
48+
}
49+
},
50+
"uuid": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
51+
"persistent": true,
52+
"insertionIndex": 1
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"id": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
3+
"name": "users_kohsuke",
4+
"requiredScenarioState": "Retry with the same credentials",
5+
"request": {
6+
"url": "/users/kohsuke",
7+
"method": "GET",
8+
"headers": {
9+
"Accept": {
10+
"equalTo": "application/vnd.github.v3+json"
11+
},
12+
"Authorization": {
13+
"equalTo": "original token"
14+
}
15+
}
16+
},
17+
"response": {
18+
"status": 200,
19+
"bodyFileName": "users_kohsuke-1.json",
20+
"headers": {
21+
"Server": "github.com",
22+
"Date": "Thu, 10 Aug 2023 09:12:37 GMT",
23+
"Content-Type": "application/json; charset=utf-8",
24+
"Cache-Control": "public, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept",
27+
"Accept-Encoding, Accept, X-Requested-With"
28+
],
29+
"ETag": "W/\"e2b462e6fe85486beb06033fe196d7c7b7669a96ddbb8f411b91f56288b31b3b\"",
30+
"Last-Modified": "Wed, 09 Aug 2023 13:37:19 GMT",
31+
"X-GitHub-Media-Type": "github.v3; format=json",
32+
"x-github-api-version-selected": "2022-11-28",
33+
"X-RateLimit-Limit": "60",
34+
"X-RateLimit-Remaining": "59",
35+
"X-RateLimit-Reset": "{{testStartDate offset='1 hours' format='unix'}}",
36+
"X-RateLimit-Used": "1",
37+
"X-RateLimit-Resource": "core",
38+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
39+
"Access-Control-Allow-Origin": "*",
40+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
41+
"X-Frame-Options": "deny",
42+
"X-Content-Type-Options": "nosniff",
43+
"X-XSS-Protection": "0",
44+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
45+
"Content-Security-Policy": "default-src 'none'",
46+
"X-GitHub-Request-Id": "BDE4:73C9:BF78362:C13B808:64D4AA05"
47+
}
48+
},
49+
"uuid": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
50+
"persistent": true,
51+
"insertionIndex": 1
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "emrebasar",
3+
"id": 3469498,
4+
"node_id": "MDQ6VXNlcjM0Njk0OTg=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/3469498?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/emrebasar",
8+
"html_url": "https://github.com/emrebasar",
9+
"followers_url": "https://api.github.com/users/emrebasar/followers",
10+
"following_url": "https://api.github.com/users/emrebasar/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/emrebasar/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/emrebasar/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/emrebasar/subscriptions",
14+
"organizations_url": "https://api.github.com/users/emrebasar/orgs",
15+
"repos_url": "https://api.github.com/users/emrebasar/repos",
16+
"events_url": "https://api.github.com/users/emrebasar/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/emrebasar/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "R. Emre Basar",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 3,
29+
"public_gists": 0,
30+
"followers": 2,
31+
"following": 0,
32+
"created_at": "2013-02-04T08:26:33Z",
33+
"updated_at": "2023-06-29T21:22:45Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "kohsuke",
3+
"id": 50003,
4+
"node_id": "MDQ6VXNlcjUwMDAz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/50003?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/kohsuke",
8+
"html_url": "https://github.com/kohsuke",
9+
"followers_url": "https://api.github.com/users/kohsuke/followers",
10+
"following_url": "https://api.github.com/users/kohsuke/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/kohsuke/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/kohsuke/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/kohsuke/subscriptions",
14+
"organizations_url": "https://api.github.com/users/kohsuke/orgs",
15+
"repos_url": "https://api.github.com/users/kohsuke/repos",
16+
"events_url": "https://api.github.com/users/kohsuke/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/kohsuke/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Kohsuke Kawaguchi",
21+
"company": "@launchableinc ",
22+
"blog": "https://www.kohsuke.org/",
23+
"location": "San Jose, California",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 263,
29+
"public_gists": 113,
30+
"followers": 2098,
31+
"following": 3,
32+
"created_at": "2009-01-28T18:53:21Z",
33+
"updated_at": "2023-05-16T02:35:24Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"id": "34cadfa1-f969-4d8d-a855-d4073878d0d8",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "application/vnd.github.v3+json"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "user-1.json",
16+
"headers": {
17+
"Server": "github.com",
18+
"Date": "Tue, 04 Jul 2023 09:27:51 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"1ccca51d2d215d0aa63c9c0e7912237f2e1f42bd17ac8e9ab30f539d9673fd4e\"",
26+
"Last-Modified": "Thu, 29 Jun 2023 21:22:45 GMT",
27+
"X-OAuth-Scopes": "gist, read:org, repo",
28+
"X-Accepted-OAuth-Scopes": "",
29+
"x-oauth-client-id": "178c6fc778ccc68e1d6a",
30+
"X-GitHub-Media-Type": "github.v3; format=json",
31+
"x-github-api-version-selected": "2022-11-28",
32+
"X-RateLimit-Limit": "5000",
33+
"X-RateLimit-Remaining": "4989",
34+
"X-RateLimit-Reset": "1688466174",
35+
"X-RateLimit-Used": "11",
36+
"X-RateLimit-Resource": "core",
37+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
38+
"Access-Control-Allow-Origin": "*",
39+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
40+
"X-Frame-Options": "deny",
41+
"X-Content-Type-Options": "nosniff",
42+
"X-XSS-Protection": "0",
43+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
44+
"Content-Security-Policy": "default-src 'none'",
45+
"X-GitHub-Request-Id": "BC5B:03C1:15BF2AD:1603E7D:64A3E617"
46+
}
47+
},
48+
"uuid": "34cadfa1-f969-4d8d-a855-d4073878d0d8",
49+
"persistent": true,
50+
"insertionIndex": 1
51+
}

0 commit comments

Comments
 (0)