99import hmac
1010from hashlib import sha1
1111import base64
12- import jwt
12+ from typing import Any
1313
14+ import jwt
1415from requests .models import HTTPError
15- from typing import Any
1616
1717import flask
1818import werkzeug
@@ -153,7 +153,8 @@ def _revoke_slack() -> werkzeug.Response:
153153@_AUTH .oidc_auth ('default' )
154154def _auth_github () -> werkzeug .Response :
155155 # Redirect to github for authorisation
156- return redirect (_GITHUB_AUTH_URI % (APP .config ['GITHUB_CLIENT_ID' ], APP .config ['STATE' ]))
156+ return redirect (_GITHUB_AUTH_URI %
157+ (APP .config ['GITHUB_CLIENT_ID' ], APP .config ['STATE' ]))
157158
158159
159160@APP .route ('/github/return' , methods = ['GET' ])
@@ -165,11 +166,12 @@ def _github_landing() -> tuple[str, int]:
165166 return 'Invalid state' , 400
166167
167168 # Get token from github
168- resp = requests .post (_GITHUB_TOKEN_URI %
169- (APP .config ['GITHUB_CLIENT_ID' ], APP .config ['GITHUB_SECRET' ],
170- request .args .get ('code' )),
171- headers = {'Accept' :'application/json' },
172- timeout = APP .config ['REQUEST_TIMEOUT' ])
169+ resp = requests .post (
170+ _GITHUB_TOKEN_URI %
171+ (APP .config ['GITHUB_CLIENT_ID' ], APP .config ['GITHUB_SECRET' ],
172+ request .args .get ('code' )),
173+ headers = {'Accept' : 'application/json' },
174+ timeout = APP .config ['REQUEST_TIMEOUT' ])
173175 try :
174176 resp .raise_for_status ()
175177 except HTTPError as e :
@@ -178,10 +180,14 @@ def _github_landing() -> tuple[str, int]:
178180
179181 resp_json = resp .json ()
180182 token = resp_json ['access_token' ]
181- header = {'Authorization' : 'token ' + token ,
182- 'Accept' : 'application/vnd.github.v3+json' }
183+ header = {
184+ 'Authorization' : 'token ' + token ,
185+ 'Accept' : 'application/vnd.github.v3+json'
186+ }
183187
184- user_resp = requests .get ('https://api.github.com/user' , headers = header , timeout = APP .config ['REQUEST_TIMEOUT' ])
188+ user_resp = requests .get ('https://api.github.com/user' ,
189+ headers = header ,
190+ timeout = APP .config ['REQUEST_TIMEOUT' ])
185191 try :
186192 user_resp .raise_for_status ()
187193 except HTTPError as e :
@@ -200,6 +206,7 @@ def _github_landing() -> tuple[str, int]:
200206 _link_github (github_username , github_id , member )
201207 return render_template ('callback.html' ), 200
202208
209+
203210def _get_github_jwt () -> str :
204211 signing_key = APP .config ["GITHUB_APP_PRIVATE_KEY" ]
205212
@@ -213,15 +220,19 @@ def _get_github_jwt() -> str:
213220
214221 return encoded_jwt
215222
223+
216224def _auth_github_org () -> str :
217225 jwt_auth = _get_github_jwt ()
218226
219227 headers = {
220- 'Accept' : 'application/vnd.github.v3+json' ,
221- 'Authorization' : 'Bearer %s' % jwt_auth ,
228+ 'Accept' : 'application/vnd.github.v3+json' ,
229+ 'Authorization' : f 'Bearer { jwt_auth } ' ,
222230 }
223231
224- org_installation_resp = requests .get ('https://api.github.com/orgs/ComputerScienceHouse/installation' , headers = headers , timeout = APP .config ['REQUEST_TIMEOUT' ])
232+ org_installation_resp = requests .get (
233+ 'https://api.github.com/orgs/ComputerScienceHouse/installation' ,
234+ headers = headers ,
235+ timeout = APP .config ['REQUEST_TIMEOUT' ])
225236 try :
226237 org_installation_resp .raise_for_status ()
227238 except HTTPError as e :
@@ -231,7 +242,10 @@ def _auth_github_org() -> str:
231242 org_installation_json = org_installation_resp .json ()
232243 org_installation_id = org_installation_json ['id' ]
233244
234- org_token_resp = requests .post ('https://api.github.com/app/installations/%s/access_tokens' % org_installation_id , headers = headers , timeout = APP .config ['REQUEST_TIMEOUT' ])
245+ org_token_resp = requests .post (
246+ f'https://api.github.com/app/installations/{ org_installation_id } /access_tokens' ,
247+ headers = headers ,
248+ timeout = APP .config ['REQUEST_TIMEOUT' ])
235249 try :
236250 org_token_resp .raise_for_status ()
237251 except HTTPError as e :
@@ -243,6 +257,7 @@ def _auth_github_org() -> str:
243257
244258 return org_token
245259
260+
246261def _link_github (github_username : str , github_id : str , member : Any ) -> None :
247262 """
248263 Puts a member's github into LDAP and adds them to the org.
@@ -252,18 +267,22 @@ def _link_github(github_username: str, github_id: str, member: Any) -> None:
252267 """
253268 org_token = _auth_github_org ()
254269
255- payload = {
270+ payload = {
256271 'org' : 'ComputerScienceHouse' ,
257272 'invitee_id' : github_id ,
258273 'role' : 'direct_member'
259274 }
260275
261276 github_org_headers = {
262- 'Accept' : 'application/vnd.github.v3+json' ,
263- 'Authorization' : 'Token %s' % org_token ,
277+ 'Accept' : 'application/vnd.github.v3+json' ,
278+ 'Authorization' : f 'Token { org_token } ' ,
264279 }
265280
266- resp = requests .post ('https://api.github.com/orgs/ComputerScienceHouse/invitations' , headers = github_org_headers , json = payload , timeout = APP .config ['REQUEST_TIMEOUT' ])
281+ resp = requests .post (
282+ 'https://api.github.com/orgs/ComputerScienceHouse/invitations' ,
283+ headers = github_org_headers ,
284+ json = payload ,
285+ timeout = APP .config ['REQUEST_TIMEOUT' ])
267286 try :
268287 resp .raise_for_status ()
269288 except HTTPError as e :
@@ -279,27 +298,27 @@ def _revoke_github() -> werkzeug.Response:
279298 """ Clear's a member's github in LDAP and removes them from the org. """
280299 uid = str (session ['userinfo' ].get ('preferred_username' , '' ))
281300 member = _LDAP .get_member (uid , uid = True )
282-
301+
283302 org_token = _auth_github_org ()
284303
285304 headers = {
286- 'Accept' : 'application/vnd.github.v3+json' ,
287- 'Authorization' : 'Token %s' % org_token ,
305+ 'Accept' : 'application/vnd.github.v3+json' ,
306+ 'Authorization' : f 'Token { org_token } ' ,
288307 }
289-
308+
290309 resp = requests .delete (
291310 'https://api.github.com/orgs/ComputerScienceHouse/members/' +
292311 member .github ,
293312 headers = headers ,
294313 timeout = APP .config ['REQUEST_TIMEOUT' ],
295314 )
296-
315+
297316 try :
298317 resp .raise_for_status ()
299318 except HTTPError as e :
300319 print ('response:' , resp .json ())
301320 raise e
302-
321+
303322 member .github = None
304323 return jsonify (success = True )
305324
0 commit comments