5555
5656_GITHUB_AUTH_URI = 'https://github.com/login/oauth/authorize' \
5757 + '?client_id=%s' \
58- + '&state=%s'
58+ + '&state=%s' \
59+ + '&redirect_uri=%s'
60+
5961_GITHUB_TOKEN_URI = 'https://github.com/login/oauth/access_token' \
6062 + '?client_id=%s' \
6163 + '&client_secret=%s' \
@@ -151,7 +153,8 @@ def _auth_github() -> werkzeug.Response:
151153 # Redirect to github for authorisation
152154 return redirect (
153155 _GITHUB_AUTH_URI %
154- (APP .config ['GITHUB_OAUTH_CLIENT_ID' ], APP .config ['STATE' ]))
156+ (APP .config ['GITHUB_APP_CLIENT_ID' ], APP .config ['STATE' ],
157+ urllib .parse .quote (APP .config ['GITHUB_REDIRECT_URI' ], safe = '' )))
155158
156159
157160@APP .route ('/github/return' , methods = ['GET' ])
@@ -165,8 +168,8 @@ def _github_landing() -> tuple[str, int]:
165168 # Get token from github
166169 resp = requests .post (
167170 _GITHUB_TOKEN_URI %
168- (APP .config ['GITHUB_OAUTH_CLIENT_ID ' ],
169- APP .config ['GITHUB_OAUTH_CLIENT_SECRET ' ], request .args .get ('code' )),
171+ (APP .config ['GITHUB_APP_CLIENT_ID ' ],
172+ APP .config ['GITHUB_APP_CLIENT_SECRET ' ], request .args .get ('code' )),
170173 headers = {'Accept' : 'application/json' },
171174 timeout = APP .config ['REQUEST_TIMEOUT' ])
172175 try :
@@ -176,9 +179,14 @@ def _github_landing() -> tuple[str, int]:
176179 raise e
177180
178181 resp_json = resp .json ()
179- token = resp_json ['access_token' ]
182+ try :
183+ user_token = resp_json ['access_token' ]
184+ except KeyError as e :
185+ print ('error: ' , e , resp_json )
186+ raise e
187+
180188 header = {
181- 'Authorization' : 'token ' + token ,
189+ 'Authorization' : 'Bearer ' + user_token ,
182190 'Accept' : 'application/vnd.github.v3+json'
183191 }
184192
@@ -200,7 +208,7 @@ def _github_landing() -> tuple[str, int]:
200208 uid = str (session ['userinfo' ].get ('preferred_username' , '' ))
201209 member = _LDAP .get_member (uid , uid = True )
202210
203- _link_github (github_username , github_id , member )
211+ _link_github (github_username , github_id , member , user_token )
204212 return render_template ('callback.html' ), 200
205213
206214
@@ -255,7 +263,8 @@ def _auth_github_org() -> str:
255263 return org_token
256264
257265
258- def _link_github (github_username : str , github_id : str , member : Any ) -> None :
266+ def _link_github (github_username : str , github_id : str , member : Any ,
267+ user_token : str ) -> None :
259268 """
260269 Puts a member's github into LDAP and adds them to the org.
261270 :param github_username: the user's github username
@@ -286,6 +295,17 @@ def _link_github(github_username: str, github_id: str, member: Any) -> None:
286295 print ('response:' , resp .json ())
287296 raise e
288297
298+ github_user_headers = {
299+ 'Accept' : 'application/vnd.github.v3+json' ,
300+ 'Authorization' : f'Token { user_token } ' ,
301+ }
302+
303+ requests .patch (
304+ 'https://api.github.com/user/memberships/orgs/ComputerScienceHouse' ,
305+ headers = github_user_headers ,
306+ json = {'state' : 'active' },
307+ timeout = APP .config ['REQUEST_TIMEOUT' ])
308+
289309 member .github = github_username
290310
291311
0 commit comments