This repository was archived by the owner on Jan 4, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +39
-17
lines changed Expand file tree Collapse file tree 2 files changed +39
-17
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 os
5
- import time
6
-
7
4
import requests
8
- import jwt
9
5
6
+ import jsonwebtoken
10
7
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
8
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
24
13
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 ())
Original file line number Diff line number Diff line change
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 ())
You can’t perform that action at this time.
0 commit comments