Allow JWT from string

This commit is contained in:
Liam Newman
2021-01-14 14:01:19 -08:00
parent b212956fbb
commit 7aae27e36f
2 changed files with 13 additions and 16 deletions

View File

@@ -42,11 +42,15 @@ public class JWTTokenProvider implements AuthorizationProvider {
private final String applicationId;
public JWTTokenProvider(String applicationId, File keyFile) throws GeneralSecurityException, IOException {
this(applicationId, loadPrivateKey(keyFile.toPath()));
this(applicationId, keyFile.toPath());
}
public JWTTokenProvider(String applicationId, Path keyPath) throws GeneralSecurityException, IOException {
this(applicationId, loadPrivateKey(keyPath));
this(applicationId, new String(Files.readAllBytes(keyPath), StandardCharsets.UTF_8));
}
public JWTTokenProvider(String applicationId, String keyString) throws GeneralSecurityException {
this(applicationId, getPrivateKeyFromString(keyString));
}
public JWTTokenProvider(String applicationId, PrivateKey privateKey) {
@@ -64,18 +68,6 @@ public class JWTTokenProvider implements AuthorizationProvider {
}
}
/**
* add dependencies for a jwt suite You can generate a key to load in this method with:
*
* <pre>
* openssl pkcs8 -topk8 -inform PEM -outform DER -in ~/github-api-app.private-key.pem -out ~/github-api-app.private-key.der -nocrypt
* </pre>
*/
private static PrivateKey loadPrivateKey(Path keyPath) throws GeneralSecurityException, IOException {
String keyString = new String(Files.readAllBytes(keyPath), StandardCharsets.UTF_8);
return getPrivateKeyFromString(keyString);
}
/**
* Convert a PKCS#8 formatted private key in string format into a java PrivateKey
*

View File

@@ -7,6 +7,8 @@ import org.kohsuke.github.extras.authorization.JWTTokenProvider;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
@@ -34,9 +36,12 @@ public class AbstractGHAppInstallationTest extends AbstractGitHubWireMockTest {
JWT_PROVIDER_1 = new JWTTokenProvider(TEST_APP_ID_1,
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_1).getFile()));
JWT_PROVIDER_2 = new JWTTokenProvider(TEST_APP_ID_2,
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_2).getFile()));
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_2).getFile()).toPath());
JWT_PROVIDER_3 = new JWTTokenProvider(TEST_APP_ID_3,
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_3).getFile()));
new String(
Files.readAllBytes(
new File(this.getClass().getResource(PRIVATE_KEY_FILE_APP_3).getFile()).toPath()),
StandardCharsets.UTF_8));
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException("These should never fail", e);
}