forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubappjwtauth.html
More file actions
262 lines (183 loc) · 9.11 KB
/
Copy pathgithubappjwtauth.html
File metadata and controls
262 lines (183 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>GitHub API for Java</title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/1024px.css");
@import url("./css/site.css");
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="wrap2"><div id="wrap">
<div id="header">
<p id="toplinks">Skip to: <a href="#content">Content</a> | <a href="#sidebar">Navigation</a> | <a href="#footer">Footer</a></p>
<h1 id="bannerLeft">
<a href="./">
GitHub API for Java
</a>
</h1>
<p id="slogan">GitHub API for Java</p>
</div>
<div id="breadcrumbs">
<div class="xright"> </div>
<div class="clear">
<hr/>
</div>
</div>
<div id="sidebar">
<div id="navcolumn">
<h2>
Git Hub API for Java
</h2><ul>
<li class="none">
<a href="index.html">Introduction</a>
</li>
<li class="none">
<a href="https://mvnrepository.com/artifact/org.kohsuke/github-api">Download</a>
</li>
<li class="none">
<a href="https://github.com/hub4j/github-api">Source code</a>
</li>
<li class="none">
<a href="https://groups.google.com/forum/#!forum/github-api">Mailing List</a>
</li>
</ul>
<h2>
Guides
</h2><ul>
<li class="expanded">
<a href="githubappflow.html">GitHub App Auth Flow</a>
<ul>
<li class="none">
<strong>JWT Authentication</strong>
</li>
<li class="none">
<a href="githubappappinsttokenauth.html">App Installation Token</a>
</li>
</ul>
</li>
<li class="none">
<a href="createorglevelresources.html">Working with organizations</a>
</li>
</ul>
<h2>
References
</h2><ul>
<li class="none">
<a href="apidocs/index.html">Javadoc</a>
</li>
</ul>
<h2>
Project Documentation
</h2><ul>
<li class="collapsed">
<a href="project-info.html">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html">Project Reports</a>
</li>
</ul>
</div>
</div>
<div id="content">
<div id="contentBox">
<section><a id="GitHub_App_Authentication_via_JWT_token"></a>
<h1>GitHub App Authentication via JWT token</h1>
<p>In order to authenticate to GitHub as a GitHub App, you must use the JWT token authentication mechanism. This can be easily achieved with this library by obtaining a <code>GitHub</code> instance like this:</p>
<pre><code>GitHub github = new GitHubBuilder().withJwtToken("my_jwt_token").build();</code></pre>
<p>Authenticating as a GitHub App lets you do a couple of things:</p>
<ul>
<li>You can retrieve high-level management information about your GitHub App.</li>
<li>You can request access tokens for an installation of the app.</li></ul></section><section><a id="Where_do_I_get_the_JWT_token_from.3F"></a>
<h1>Where do I get the JWT token from?</h1>
<p>To generate the JWT token required to authenticate as a GitHub app you have to:</p>
<ul>
<li>Sign the JWT token using the private key you configured on your GitHub app as described <a class="externalLink" href="https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#generating-a-private-key">here</a></li>
<li>Encode it using the <code>RS256</code> algorithm.</li></ul>
<p>GitHub checks that the request is authenticated by verifying the token with the app's stored public key.</p></section><section><a id="Converting_the_private_key_into_a_Java_friendly_format"></a>
<h1>Converting the private key into a Java friendly format</h1>
<p><b>Note:</b> GitHub let's you download the GitHub App private key in the <code>PEM</code> format which isn't natively supported by the JVM unless you leverage a third-party library such as <a class="externalLink" href="https://www.bouncycastle.org/">BouncyCastle</a>. In this guide we will convert it to <code>DER</code> using the <code>openssl</code> utility.</p>
<pre><code>openssl pkcs8 -topk8 -inform PEM -outform DER -in ~/github-api-app.private-key.pem -out ~/github-api-app.private-key.der -nocrypt</code></pre></section><section><a id="How_can_I_generate_the_JWT_token.3F"></a>
<h1>How can I generate the JWT token?</h1>
<p>Once you have the private key converted to the <code>DER</code> format, you will need 2 more things before you are able to generate JWT tokens:</p>
<p><b>GitHub App Id:</b></p>
<p>You can obtain the GitHub App Id from your app settings webpage as shown below:</p><figure><img src="images/Github_App_Id.png" /><figcaption>Github_App_Id</figcaption></figure>
<p><b>JWT library:</b></p>
<p>In order to generate the JWT, you will have to likely use a JWT library. In this guide we will use <a class="externalLink" href="https://github.com/jwtk/jjwt">jjwt</a> to that matter.</p>
<p>Having said that, add on your <code>pom.xml</code> the following dependencies:</p>
<pre><code><dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency></code></pre>
<p>Now we have everything we need so let's generate the JWT token:</p>
<pre><code>static PrivateKey get(String filename) throws Exception {
byte[] keyBytes = Files.toByteArray(new File(filename));
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
static String createJWT(String githubAppId, long ttlMillis) throws Exception {
//The JWT signature algorithm we will be using to sign the token
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
//We will sign our JWT with our private key
Key signingKey = get("github-api-app.private-key.der");
//Let's set the JWT Claims
JwtBuilder builder = Jwts.builder()
.setIssuedAt(now)
.setIssuer(githubAppId)
.signWith(signingKey, signatureAlgorithm);
//if it has been specified, let's add the expiration
if (ttlMillis > 0) {
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}
//Builds the JWT and serializes it to a compact, URL-safe string
return builder.compact();
}
public static void main(String[] args) throws Exception {
String jwtToken = createJWT("44435", 600000); //sdk-github-api-app-test
GitHub gitHubApp = new GitHubBuilder().withJwtToken(jwtToken).build();
}</code></pre></section><section><a id="How_do_I_get_a_specific_app_installation.3F"></a>
<h1>How do I get a specific app installation?</h1>
<pre><code>String jwtToken = createJWT("44435", 600000); //sdk-github-api-app-test
GitHub gitHubApp = new GitHubBuilder().withJwtToken(jwtToken).build();
GHAppInstallation appInstallation = gitHubApp.getApp().getInstallationById(111111); // Installation Id</code></pre></section><section><a id="What_next.3F"></a>
<h1>What next?</h1>
<ul>
<li>Authenticating as an installation via the <a href="/githubappappinsttokenauth.html">App Installation Token</a></li></ul></section>
</div>
</div>
<div id="footer">
<div class="xright">©
${currentYear}
<a href="http://kohsuke.org/">Kohsuke Kawaguchi</a> and other contributors
$dateFormat.applyPattern( $format )
| $i18n.getString( "site-renderer", $locale, "template.lastpublished" ): $dateToday
| $i18n.getString( "site-renderer", $locale, "template.version" ): 2.0-rc.7
</div>
<div class="clear">
<hr/>
</div>
</div>
</div></div>
</body>
</html>