-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into implement_apps
- Loading branch information
Showing
19 changed files
with
332 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/kohsuke/github/authorization/AppInstallationAuthorizationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.kohsuke.github.authorization; | ||
|
||
import org.kohsuke.github.BetaApi; | ||
import org.kohsuke.github.GHApp; | ||
import org.kohsuke.github.GHAppInstallation; | ||
import org.kohsuke.github.GHAppInstallationToken; | ||
import org.kohsuke.github.GitHub; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* An AuthorizationProvider that performs automatic token refresh for an organization's AppInstallation. | ||
*/ | ||
public class AppInstallationAuthorizationProvider extends GitHub.DependentAuthorizationProvider { | ||
|
||
private final AppInstallationProvider appInstallationProvider; | ||
|
||
private String authorization; | ||
|
||
@Nonnull | ||
private Instant validUntil = Instant.MIN; | ||
|
||
/** | ||
* Provides an AuthorizationProvider that performs automatic token refresh, based on an previously authenticated | ||
* github client. | ||
* | ||
* @param appInstallationProvider | ||
* An AppInstallationProvider that the authorization provider will use to retrieve the App. | ||
* @param authorizationProvider | ||
* A authorization provider that returns a JWT token that can be used to refresh the App Installation | ||
* token from GitHub. | ||
*/ | ||
@BetaApi | ||
public AppInstallationAuthorizationProvider(AppInstallationProvider appInstallationProvider, | ||
AuthorizationProvider authorizationProvider) { | ||
super(authorizationProvider); | ||
this.appInstallationProvider = appInstallationProvider; | ||
} | ||
|
||
@Override | ||
public String getEncodedAuthorization() throws IOException { | ||
synchronized (this) { | ||
if (authorization == null || Instant.now().isAfter(this.validUntil)) { | ||
String token = refreshToken(); | ||
authorization = String.format("token %s", token); | ||
} | ||
return authorization; | ||
} | ||
} | ||
|
||
private String refreshToken() throws IOException { | ||
GitHub gitHub = this.gitHub(); | ||
GHAppInstallation installationByOrganization = appInstallationProvider.getAppInstallation(gitHub.getApp()); | ||
GHAppInstallationToken ghAppInstallationToken = installationByOrganization.createToken().create(); | ||
this.validUntil = ghAppInstallationToken.getExpiresAt().toInstant().minus(Duration.ofMinutes(5)); | ||
return Objects.requireNonNull(ghAppInstallationToken.getToken()); | ||
} | ||
|
||
/** | ||
* Provides an interface that returns an app to be used by an AppInstallationAuthorizationProvider | ||
*/ | ||
@FunctionalInterface | ||
public interface AppInstallationProvider { | ||
/** | ||
* Provides a GHAppInstallation for the given GHApp | ||
* | ||
* @param app | ||
* The GHApp to use | ||
* @return The GHAppInstallation | ||
* @throws IOException | ||
* on error | ||
*/ | ||
GHAppInstallation getAppInstallation(GHApp app) throws IOException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
...onAuthorizationProviderTest/wiremock/validJWTTokenWhenLookingUpAppById/__files/app-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"id": 82994, | ||
"slug": "ghapi-test-app-1", | ||
"node_id": "MDM6QXBwODI5OTQ=", | ||
"owner": { | ||
"login": "hub4j-test-org", | ||
"id": 7544739, | ||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/hub4j-test-org", | ||
"html_url": "https://github.com/hub4j-test-org", | ||
"followers_url": "https://api.github.com/users/hub4j-test-org/followers", | ||
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions", | ||
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs", | ||
"repos_url": "https://api.github.com/users/hub4j-test-org/repos", | ||
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events", | ||
"type": "Organization", | ||
"site_admin": false | ||
}, | ||
"name": "GHApi Test app 1", | ||
"description": "", | ||
"external_url": "http://localhost", | ||
"html_url": "https://github.com/apps/ghapi-test-app-1", | ||
"created_at": "2020-09-30T13:40:56Z", | ||
"updated_at": "2020-09-30T13:40:56Z", | ||
"permissions": { | ||
"contents": "read", | ||
"metadata": "read" | ||
}, | ||
"events": [], | ||
"installations_count": 1 | ||
} |
40 changes: 40 additions & 0 deletions
40
...Test/wiremock/validJWTTokenWhenLookingUpAppById/__files/app_installations_12129901-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"id": 12129901, | ||
"account": { | ||
"login": "hub4j-test-org", | ||
"id": 7544739, | ||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/hub4j-test-org", | ||
"html_url": "https://github.com/hub4j-test-org", | ||
"followers_url": "https://api.github.com/users/hub4j-test-org/followers", | ||
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions", | ||
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs", | ||
"repos_url": "https://api.github.com/users/hub4j-test-org/repos", | ||
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events", | ||
"type": "Organization", | ||
"site_admin": false | ||
}, | ||
"repository_selection": "selected", | ||
"access_tokens_url": "https://api.github.com/app/installations/12129901/access_tokens", | ||
"repositories_url": "https://api.github.com/installation/repositories", | ||
"html_url": "https://github.com/organizations/hub4j-test-org/settings/installations/12129901", | ||
"app_id": 82994, | ||
"app_slug": "ghapi-test-app-1", | ||
"target_id": 7544739, | ||
"target_type": "Organization", | ||
"permissions": {}, | ||
"events": [], | ||
"created_at": "2020-09-30T13:41:32.000Z", | ||
"updated_at": "2023-03-21T14:39:11.000Z", | ||
"single_file_name": null, | ||
"has_multiple_single_files": false, | ||
"single_file_paths": [], | ||
"suspended_by": null, | ||
"suspended_at": null | ||
} |
42 changes: 42 additions & 0 deletions
42
...nAuthorizationProviderTest/wiremock/validJWTTokenWhenLookingUpAppById/mappings/app-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"id": "44e77d23-3e8f-4c80-a5fc-98546576386a", | ||
"name": "app", | ||
"request": { | ||
"url": "/app", | ||
"method": "GET", | ||
"headers": { | ||
"Accept": { | ||
"equalTo": "application/vnd.github.machine-man-preview+json" | ||
} | ||
} | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "app-1.json", | ||
"headers": { | ||
"Server": "GitHub.com", | ||
"Date": "Fri, 14 Apr 2023 09:15:08 GMT", | ||
"Content-Type": "application/json; charset=utf-8", | ||
"Cache-Control": "public, max-age=60, s-maxage=60", | ||
"Vary": [ | ||
"Accept", | ||
"Accept-Encoding, Accept, X-Requested-With" | ||
], | ||
"ETag": "W/\"662c8ea184a852f605e0c94a9789fe43965f939e16e02ff0b3a8cc1043078a62\"", | ||
"X-GitHub-Media-Type": "github.v3; param=machine-man-preview; format=json", | ||
"x-github-api-version-selected": "2022-11-28", | ||
"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", | ||
"Access-Control-Allow-Origin": "*", | ||
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", | ||
"X-Frame-Options": "deny", | ||
"X-Content-Type-Options": "nosniff", | ||
"X-XSS-Protection": "0", | ||
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", | ||
"Content-Security-Policy": "default-src 'none'", | ||
"X-GitHub-Request-Id": "D5E4:64DB:14B8E4B:2A5B09C:6439199C" | ||
} | ||
}, | ||
"uuid": "44e77d23-3e8f-4c80-a5fc-98546576386a", | ||
"persistent": true, | ||
"insertionIndex": 1 | ||
} |
Oops, something went wrong.