Skip to content

Commit 80124e3

Browse files
authored
Merge pull request #1021 from bitwiseman/jwt-string
Allow JWT from string
2 parents b212956 + 7aae27e commit 80124e3

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/main/java/org/kohsuke/github/extras/authorization/JWTTokenProvider.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ public class JWTTokenProvider implements AuthorizationProvider {
4242
private final String applicationId;
4343

4444
public JWTTokenProvider(String applicationId, File keyFile) throws GeneralSecurityException, IOException {
45-
this(applicationId, loadPrivateKey(keyFile.toPath()));
45+
this(applicationId, keyFile.toPath());
4646
}
4747

4848
public JWTTokenProvider(String applicationId, Path keyPath) throws GeneralSecurityException, IOException {
49-
this(applicationId, loadPrivateKey(keyPath));
49+
this(applicationId, new String(Files.readAllBytes(keyPath), StandardCharsets.UTF_8));
50+
}
51+
52+
public JWTTokenProvider(String applicationId, String keyString) throws GeneralSecurityException {
53+
this(applicationId, getPrivateKeyFromString(keyString));
5054
}
5155

5256
public JWTTokenProvider(String applicationId, PrivateKey privateKey) {
@@ -64,18 +68,6 @@ public String getEncodedAuthorization() throws IOException {
6468
}
6569
}
6670

67-
/**
68-
* add dependencies for a jwt suite You can generate a key to load in this method with:
69-
*
70-
* <pre>
71-
* openssl pkcs8 -topk8 -inform PEM -outform DER -in ~/github-api-app.private-key.pem -out ~/github-api-app.private-key.der -nocrypt
72-
* </pre>
73-
*/
74-
private static PrivateKey loadPrivateKey(Path keyPath) throws GeneralSecurityException, IOException {
75-
String keyString = new String(Files.readAllBytes(keyPath), StandardCharsets.UTF_8);
76-
return getPrivateKeyFromString(keyString);
77-
}
78-
7971
/**
8072
* Convert a PKCS#8 formatted private key in string format into a java PrivateKey
8173
*

src/test/java/org/kohsuke/github/AbstractGHAppInstallationTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.io.File;
99
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
1012
import java.security.GeneralSecurityException;
1113
import java.security.KeyFactory;
1214
import java.security.PrivateKey;
@@ -34,9 +36,12 @@ public class AbstractGHAppInstallationTest extends AbstractGitHubWireMockTest {
3436
JWT_PROVIDER_1 = new JWTTokenProvider(TEST_APP_ID_1,
3537
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_1).getFile()));
3638
JWT_PROVIDER_2 = new JWTTokenProvider(TEST_APP_ID_2,
37-
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_2).getFile()));
39+
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_2).getFile()).toPath());
3840
JWT_PROVIDER_3 = new JWTTokenProvider(TEST_APP_ID_3,
39-
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_3).getFile()));
41+
new String(
42+
Files.readAllBytes(
43+
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_3).getFile()).toPath()),
44+
StandardCharsets.UTF_8));
4045
} catch (GeneralSecurityException | IOException e) {
4146
throw new RuntimeException("These should never fail", e);
4247
}

0 commit comments

Comments
 (0)