3838import com .spotify .github .v3 .repos .FolderContent ;
3939import com .spotify .github .v3 .repos .Repository ;
4040import 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 .*;
4443import java .lang .invoke .MethodHandles ;
4544import java .net .URI ;
4645import java .time .ZonedDateTime ;
5655import javax .ws .rs .core .MediaType ;
5756
5857import okhttp3 .*;
58+ import org .apache .commons .io .FileUtils ;
5959import org .slf4j .Logger ;
6060import 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}
0 commit comments