Skip to content

Commit 61f0320

Browse files
committed
get access token
Signed-off-by: Andrew Mak <[email protected]>
1 parent 1d5a2d3 commit 61f0320

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/main/java/EventsHandler.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.security.interfaces.RSAPrivateKey;
1010
import java.util.Base64;
1111
import java.util.Base64.Decoder;
12+
import java.util.Date;
1213
import java.util.Map;
1314

1415
import javax.crypto.Mac;
@@ -60,7 +61,7 @@ void validateSignature(
6061
log.info("-> Signature {} is valid", signature);
6162
}
6263

63-
String generateJWT(
64+
String generateAuth(
6465
@ExchangeProperty(GitHubApp.PEM) String pem,
6566
@ExchangeProperty("app_id") String appId)
6667
throws IOException {
@@ -72,8 +73,12 @@ String generateJWT(
7273
KeyPair kp = converter.getKeyPair((PEMKeyPair) parser.readObject());
7374
RSAPrivateKey key = (RSAPrivateKey) kp.getPrivate();
7475

76+
Date date = new Date();
77+
7578
// create and sign JWT with private key
76-
return JWT.create()
79+
return "Bearer " + JWT.create()
80+
.withIssuedAt(date)
81+
.withExpiresAt(new Date(date.getTime() + (10 * 60 * 1000))) // 10 mins
7782
.withIssuer(appId)
7883
.sign(Algorithm.RSA256(null, key));
7984
}
@@ -124,11 +129,17 @@ public void configure() throws Exception {
124129
// get app secrets
125130
.to("direct:get-secret")
126131

127-
// validate the event and request for an access token
132+
// validate the event
128133
.bean(this, "validateSignature")
129-
.setProperty("jwt", method(this, "generateJWT"))
130-
.log("TODO: ${exchangeProperty[jwt]}") // TODO: request access token
131-
134+
135+
// request for an access token
136+
.removeHeaders("*")
137+
.setHeader("Authorization", method(this, "generateAuth"))
138+
.setBody(simple("${null}"))
139+
.toD("https://api.github.com/app/installations/${exchangeProperty[id]}/access_tokens?httpMethod=POST")
140+
.unmarshal().json()
141+
.setProperty("token", simple("${body[token]}"))
142+
132143
// restore original payload
133144
.setBody(exchangeProperty("payload"))
134145
.log("TODO: ${body}");

0 commit comments

Comments
 (0)