This repository was archived by the owner on Jan 4, 2019. It is now read-only.
File tree 2 files changed +42
-35
lines changed
2 files changed +42
-35
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python
2
2
# -*- coding: utf8 -*-
3
3
4
- import requests
4
+ """
5
+ Authenticate as a GitHub App:
6
+ https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/#authenticating-as-a-github-app
7
+ """
8
+
9
+ import os
10
+ import time
11
+
12
+ import jwt
5
13
6
- import jsonwebtoken
14
+ import requests
7
15
8
16
9
17
class JWTAuth (requests .auth .AuthBase ):
10
- def __call__ (self , r ):
11
- r .headers ['Authorization' ] = 'bearer {}' .format (jsonwebtoken .generate ())
18
+ def __init__ (self , iss , key , expiration = 10 * 60 ):
19
+ self .iss = iss
20
+ self .key = key
21
+ self .expiration = expiration
22
+
23
+ def generate_token (self ):
24
+ # Generate the JWT
25
+ payload = {
26
+ # issued at time
27
+ 'iat' : int (time .time ()),
28
+ # JWT expiration time (10 minute maximum)
29
+ 'exp' : int (time .time ()) + self .expiration ,
30
+ # GitHub App's identifier
31
+ 'iss' : self .iss
32
+ }
33
+
34
+ tok = jwt .encode (payload , self .key , algorithm = 'RS256' )
35
+
36
+ return tok .decode ('utf-8' )
37
+
38
+ def __call__ (self , r ):
39
+ r .headers ['Authorization' ] = 'bearer {}' .format (self .generate_token ())
12
40
return r
13
41
42
+ with open (os .environ ['PRIVATE_KEY_FILE' ]) as fp :
43
+ private_pem = fp .read ()
44
+
45
+ authorization = JWTAuth (
46
+ iss = os .environ ['APP_ID' ],
47
+ key = private_pem )
48
+
14
49
response = requests .get ('https://api.github.com/app' ,
15
- auth = JWTAuth () ,
50
+ auth = authorization ,
16
51
headers = dict (accept = 'application/vnd.github.machine-man-preview+json' ))
52
+
53
+ # Print the response
17
54
print (response .json ())
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments