|
6 | 6 | import org.kohsuke.github.GitHub; |
7 | 7 |
|
8 | 8 | import java.io.IOException; |
| 9 | +import java.net.URL; |
9 | 10 | import java.time.Duration; |
10 | 11 | import java.time.Instant; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
11 | 15 | import java.util.Objects; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
12 | 18 |
|
13 | 19 | import javax.annotation.Nonnull; |
14 | 20 |
|
|
17 | 23 | */ |
18 | 24 | public class OrgAppInstallationAuthorizationProvider extends GitHub.DependentAuthorizationProvider { |
19 | 25 |
|
20 | | - private final String organizationName; |
| 26 | + private static final Pattern pattern = Pattern.compile("/repos/(.*)/.*"); |
21 | 27 |
|
22 | | - private String latestToken; |
| 28 | + private Map<String, String> latestToken = new HashMap<>(); |
23 | 29 |
|
24 | 30 | @Nonnull |
25 | | - private Instant validUntil = Instant.MIN; |
| 31 | + private Map<String, Instant> validUntil = new HashMap<>(); |
26 | 32 |
|
27 | 33 | /** |
28 | 34 | * Provides an AuthorizationProvider that performs automatic token refresh, based on an previously authenticated |
29 | 35 | * github client. |
30 | 36 | * |
31 | | - * @param organizationName |
32 | | - * The name of the organization where the application is installed |
33 | 37 | * @param authorizationProvider |
34 | 38 | * A authorization provider that returns a JWT token that can be used to refresh the App Installation |
35 | 39 | * token from GitHub. |
36 | 40 | */ |
37 | 41 | @BetaApi |
38 | 42 | @Deprecated |
39 | | - public OrgAppInstallationAuthorizationProvider(String organizationName, |
40 | | - AuthorizationProvider authorizationProvider) { |
| 43 | + public OrgAppInstallationAuthorizationProvider(AuthorizationProvider authorizationProvider) { |
41 | 44 | super(authorizationProvider); |
42 | | - this.organizationName = organizationName; |
43 | 45 | } |
44 | 46 |
|
45 | 47 | @Override |
46 | | - public String getEncodedAuthorization() throws IOException { |
| 48 | + public String getEncodedAuthorization(URL url) throws IOException { |
47 | 49 | synchronized (this) { |
48 | | - if (latestToken == null || Instant.now().isAfter(this.validUntil)) { |
49 | | - refreshToken(); |
| 50 | + String org = getOrgFromURL(url); |
| 51 | + if (latestToken.get(org) == null || this.validUntil.get(org) == null |
| 52 | + || Instant.now().isAfter(this.validUntil.get(org))) { |
| 53 | + refreshToken(url); |
| 54 | + } |
| 55 | + return String.format("token %s", latestToken.get(org)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getEncodedAuthorization() throws IOException { |
| 61 | + return getEncodedAuthorization(null); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Try to figure out what org is this url trying to access so we can use the correct App installation for that org. |
| 66 | + * |
| 67 | + * @param url |
| 68 | + * @return the organization or "" if it cannot be computed |
| 69 | + */ |
| 70 | + private String getOrgFromURL(URL url) { |
| 71 | + if (url != null) { |
| 72 | + Matcher matcher = pattern.matcher(url.getPath()); |
| 73 | + if (matcher.matches()) { |
| 74 | + return matcher.group(1); |
50 | 75 | } |
51 | | - return String.format("token %s", latestToken); |
52 | 76 | } |
| 77 | + return ""; |
53 | 78 | } |
54 | 79 |
|
55 | | - private void refreshToken() throws IOException { |
56 | | - GitHub gitHub = this.gitHub(); |
57 | | - GHAppInstallation installationByOrganization = gitHub.getApp() |
58 | | - .getInstallationByOrganization(this.organizationName); |
59 | | - GHAppInstallationToken ghAppInstallationToken = installationByOrganization.createToken().create(); |
60 | | - this.validUntil = ghAppInstallationToken.getExpiresAt().toInstant().minus(Duration.ofMinutes(5)); |
61 | | - this.latestToken = Objects.requireNonNull(ghAppInstallationToken.getToken()); |
| 80 | + private void refreshToken(URL url) throws IOException { |
| 81 | + List<GHAppInstallation> installations = this.gitHub().getApp().listInstallations().asList(); |
| 82 | + // take the first one if no one matches |
| 83 | + GHAppInstallation installation = installations.get(0); |
| 84 | + String org = getOrgFromURL(url); |
| 85 | + for (GHAppInstallation ghAppInstallation : installations) { |
| 86 | + if (org.equals(installation.getAccount().getLogin())) { |
| 87 | + System.out.println( |
| 88 | + String.format("Found installation for path %s: %s", url.getPath(), installation.getHtmlUrl())); |
| 89 | + installation = ghAppInstallation; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + GHAppInstallationToken ghAppInstallationToken = installation.createToken().create(); |
| 94 | + this.validUntil.put(org, ghAppInstallationToken.getExpiresAt().toInstant().minus(Duration.ofMinutes(5))); |
| 95 | + this.latestToken.put(org, Objects.requireNonNull(ghAppInstallationToken.getToken())); |
62 | 96 | } |
63 | 97 | } |
0 commit comments