8000 Complete flow for authenticating as a GitHub App · swinton/github-app-demo.py@7d6a210 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit 7d6a210

Browse files
committed
1 parent 7d6ae91 commit 7d6a210

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

auth.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33

4-
import os
5-
import time
6-
74
import requests
8-
import jwt
95

6+
import jsonwebtoken
107

11-
# Generate the JWT
12-
payload = {
13-
# issued at time
14-
'iat': int(time.time()),
15-
# JWT expiration time (10 minute maximum)
16-
'exp': int(time.time()) + (10 * 60),
17-
# GitHub App's identifier
18-
'iss': os.environ['APP_ID']
19-
}
208

21-
with open(os.environ['PRIVATE_KEY_FILE']) as fp:
22-
private_key = fp.read()
23-
encoded = jwt.encode(payload, private_key, algorithm='RS256')
9+
class JWTAuth(requests.auth.AuthBase):
10+
def __call__(self, r):
11+
r.headers['Authorization'] = 'bearer {}'.format(jsonwebtoken.generate())
12+
return r
2413

25-
print "👋", encoded
14+
response = requests.get('https://api.github.com/app',
15+
auth=JWTAuth(),
16+
headers=dict(accept='application/vnd.github.machine-man-preview+json'))
17+
print(response.json())

jsonwebtoken.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf8 -*-
3+
4+
import os
5+
import time
6+
7+
import jwt
8+
9+
10+
def generate():
11+
# Generate the JWT
12+
payload = {
13+
# issued at time
14+
'iat': int(time.time()),
15+
# JWT expiration time (10 minute maximum)
16+
'exp': int(time.time()) + (10 * 60),
17+
# GitHub App's identifier
18+
'iss': os.environ['APP_ID']
19+
}
20+
21+
with open(os.environ['PRIVATE_KEY_FILE']) as fp:
22+
private_pem = fp.read()
23+
24+
jwt_token = jwt.encode(payload, private_pem, algorithm='RS256')
25+
26+
return jwt_token.decode('utf-8')
27+
28+
29+
if __name__ == '__main__':
30+
print(generate())

0 commit comments

Comments
 (0)
0