9
9
import java .security .interfaces .RSAPrivateKey ;
10
10
import java .util .Base64 ;
11
11
import java .util .Base64 .Decoder ;
12
+ import java .util .Date ;
12
13
import java .util .Map ;
13
14
14
15
import javax .crypto .Mac ;
@@ -60,7 +61,7 @@ void validateSignature(
60
61
log .info ("-> Signature {} is valid" , signature );
61
62
}
62
63
63
- String generateJWT (
64
+ String generateAuth (
64
65
@ ExchangeProperty (GitHubApp .PEM ) String pem ,
65
66
@ ExchangeProperty ("app_id" ) String appId )
66
67
throws IOException {
@@ -72,8 +73,12 @@ String generateJWT(
72
73
KeyPair kp = converter .getKeyPair ((PEMKeyPair ) parser .readObject ());
73
74
RSAPrivateKey key = (RSAPrivateKey ) kp .getPrivate ();
74
75
76
+ Date date = new Date ();
77
+
75
78
// 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
77
82
.withIssuer (appId )
78
83
.sign (Algorithm .RSA256 (null , key ));
79
84
}
@@ -124,11 +129,17 @@ public void configure() throws Exception {
124
129
// get app secrets
125
130
.to ("direct:get-secret" )
126
131
127
- // validate the event and request for an access token
132
+ // validate the event
128
133
.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
+
132
143
// restore original payload
133
144
.setBody (exchangeProperty ("payload" ))
134
145
.log ("TODO: ${body}" );
0 commit comments