Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit 3e6ab85

Browse files
author
shaendler
committed
private key provided as byte[]
1 parent 6874da0 commit 3e6ab85

2 files changed

Lines changed: 81 additions & 25 deletions

File tree

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838
import com.spotify.github.v3.repos.FolderContent;
3939
import com.spotify.github.v3.repos.Repository;
4040
import com.spotify.github.v3.repos.Status;
41-
import java.io.File;
42-
import java.io.IOException;
43-
import java.io.UncheckedIOException;
41+
42+
import java.io.*;
4443
import java.lang.invoke.MethodHandles;
4544
import java.net.URI;
4645
import java.time.ZonedDateTime;
@@ -56,6 +55,7 @@
5655
import javax.ws.rs.core.MediaType;
5756

5857
import okhttp3.*;
58+
import org.apache.commons.io.FileUtils;
5959
import org.slf4j.Logger;
6060
import org.slf4j.LoggerFactory;
6161

@@ -99,7 +99,7 @@ public class GitHubClient {
9999
private final OkHttpClient client;
100100
private final String token;
101101

102-
private final File privateKey;
102+
private final byte[] privateKey;
103103
private final Integer appId;
104104
private final Integer installationId;
105105

@@ -109,7 +109,7 @@ private GitHubClient(
109109
final OkHttpClient client,
110110
final URI baseUrl,
111111
final String accessToken,
112-
final File privateKey,
112+
final byte[] privateKey,
113113
final Integer appId,
114114
final Integer installationId) {
115115
this.baseUrl = baseUrl;
@@ -141,6 +141,18 @@ public static GitHubClient create(final URI baseUrl, final String token) {
141141
* @return github api client
142142
*/
143143
public static GitHubClient create(final URI baseUrl, final File privateKey, final Integer appId) {
144+
return createOrThrow(new OkHttpClient(), baseUrl, privateKey, appId, null);
145+
}
146+
147+
/**
148+
* Create a github api client with a given base URL and a path to a key.
149+
*
150+
* @param baseUrl base URL
151+
* @param privateKey the private key as byte array
152+
* @param appId the github app ID
153+
* @return github api client
154+
*/
155+
public static GitHubClient create(final URI baseUrl, final byte[] privateKey, final Integer appId) {
144156
return new GitHubClient(new OkHttpClient(), baseUrl, null, privateKey, appId, null);
145157
}
146158

@@ -155,6 +167,20 @@ public static GitHubClient create(final URI baseUrl, final File privateKey, fina
155167
*/
156168
public static GitHubClient create(
157169
final URI baseUrl, final File privateKey, final Integer appId, final Integer installationId) {
170+
return createOrThrow(new OkHttpClient(), baseUrl, privateKey, appId, installationId);
171+
}
172+
173+
/**
174+
* Create a github api client with a given base URL and a path to a key.
175+
*
176+
* @param baseUrl base URL
177+
* @param privateKey the private key as byte array
178+
* @param appId the github app ID
179+
* @param installationId the installationID to be authenticated as
180+
* @return github api client
181+
*/
182+
public static GitHubClient create(
183+
final URI baseUrl, final byte[] privateKey, final Integer appId, final Integer installationId) {
158184
return new GitHubClient(new OkHttpClient(), baseUrl, null, privateKey, appId, installationId);
159185
}
160186

@@ -172,6 +198,23 @@ public static GitHubClient create(
172198
final URI baseUrl,
173199
final File privateKey,
174200
final Integer appId) {
201+
return createOrThrow(httpClient, baseUrl, privateKey, appId, null);
202+
}
203+
204+
/**
205+
* Create a github api client with a given base URL and a path to a key.
206+
*
207+
* @param httpClient an instance of OkHttpClient
208+
* @param baseUrl base URL
209+
* @param privateKey the private key as byte array
210+
* @param appId the github app ID
211+
* @return github api client
212+
*/
213+
public static GitHubClient create(
214+
final OkHttpClient httpClient,
215+
final URI baseUrl,
216+
final byte[] privateKey,
217+
final Integer appId) {
175218
return new GitHubClient(httpClient, baseUrl, null, privateKey, appId, null);
176219
}
177220

@@ -190,6 +233,24 @@ public static GitHubClient create(
190233
final File privateKey,
191234
final Integer appId,
192235
final Integer installationId) {
236+
return createOrThrow(httpClient, baseUrl, privateKey, appId, installationId);
237+
}
238+
239+
/**
240+
* Create a github api client with a given base URL and a path to a key.
241+
*
242+
* @param httpClient an instance of OkHttpClient
243+
* @param baseUrl base URL
244+
* @param privateKey the private key as byte array
245+
* @param appId the github app ID
246+
* @return github api client
247+
*/
248+
public static GitHubClient create(
249+
final OkHttpClient httpClient,
250+
final URI baseUrl,
251+
final byte[] privateKey,
252+
final Integer appId,
253+
final Integer installationId) {
193254
return new GitHubClient(httpClient, baseUrl, null, privateKey, appId, installationId);
194255
}
195256

@@ -215,7 +276,7 @@ public static GitHubClient create(
215276
*/
216277
public static GitHubClient scopeForInstallationId(
217278
final GitHubClient client, final int installationId) {
218-
if (!client.getPrivateKey().isPresent()) {
279+
if (client.getPrivateKey().isEmpty()) {
219280
throw new RuntimeException("Installation ID scoped client needs a private key");
220281
}
221282
return new GitHubClient(
@@ -235,7 +296,7 @@ static String responseBodyUnchecked(final Response response) {
235296
}
236297
}
237298

238-
public Optional<File> getPrivateKey() {
299+
public Optional<byte[]> getPrivateKey() {
239300
return Optional.ofNullable(privateKey);
240301
}
241302

@@ -546,7 +607,7 @@ private String getAuthorizationHeader(final String path) {
546607
} else if (getPrivateKey().isPresent()) {
547608
final String jwtToken;
548609
try {
549-
jwtToken = JwtTokenIssuer.fromFile(privateKey).getToken(appId);
610+
jwtToken = JwtTokenIssuer.fromPrivateKey(privateKey).getToken(appId);
550611
} catch (Exception e) {
551612
throw new RuntimeException("There was an error generating JWT token", e);
552613
}
@@ -693,4 +754,15 @@ CompletableFuture<Response> processPossibleRedirects(
693754

694755
return completedFuture(response);
695756
}
757+
758+
/**
759+
* Wrapper to Constructors that expose File object for the privateKey argument
760+
* */
761+
private static GitHubClient createOrThrow(final OkHttpClient httpClient, final URI baseUrl, final File privateKey, final Integer appId, final Integer installationId) {
762+
try {
763+
return new GitHubClient(httpClient, baseUrl, null, FileUtils.readFileToByteArray(privateKey), appId, installationId);
764+
} catch (IOException e) {
765+
throw new RuntimeException("There was an error generating JWT token", e);
766+
}
767+
}
696768
}

src/main/java/com/spotify/github/v3/clients/JwtTokenIssuer.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222

2323
import io.jsonwebtoken.Jwts;
2424
import io.jsonwebtoken.SignatureAlgorithm;
25-
import java.io.File;
26-
import java.io.IOException;
25+
2726
import java.security.KeyFactory;
2827
import java.security.NoSuchAlgorithmException;
2928
import java.security.PrivateKey;
3029
import java.security.spec.InvalidKeySpecException;
3130
import java.security.spec.KeySpec;
3231
import java.security.spec.PKCS8EncodedKeySpec;
3332
import java.util.Date;
34-
import org.apache.commons.io.FileUtils;
3533

3634
/** The helper Jwt token issuer. */
3735
public class JwtTokenIssuer {
@@ -45,20 +43,6 @@ private JwtTokenIssuer(final PrivateKey signingKey) {
4543
this.signingKey = signingKey;
4644
}
4745

48-
/**
49-
* Instantiates a new Jwt token issuer.
50-
*
51-
* @param privateKeyFile the private key file
52-
* @throws NoSuchAlgorithmException the no such algorithm exception
53-
* @throws InvalidKeySpecException the invalid key spec exception
54-
* @throws IOException the io exception
55-
*/
56-
public static JwtTokenIssuer fromFile(final File privateKeyFile)
57-
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
58-
byte[] apiKeySecretBytes = FileUtils.readFileToByteArray(privateKeyFile);
59-
return fromPrivateKey(apiKeySecretBytes);
60-
}
61-
6246
/**
6347
* Instantiates a new Jwt token issuer.
6448
*

0 commit comments

Comments
 (0)