99import java .security .interfaces .RSAPrivateKey ;
1010import java .util .Base64 ;
1111import java .util .Base64 .Decoder ;
12+ import java .util .Date ;
1213import java .util .Map ;
1314
1415import 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