Skip to content

Commit af54893

Browse files
Switched GH auth to use app + automatically accept org invite (#25)
* add redirect uri env support * github app auth as well as automatically adding people to org * logging for key error to debug * remove github from ldap before deleting to fix desync error * fixed env var name * linting * moved when ldap github user role is removed
1 parent 0936e3a commit af54893

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

config.env.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# OpenID Connect SSO config
1212
OIDC_ISSUER = os.environ.get('OIDC_ISSUER',
1313
'https://sso.csh.rit.edu/auth/realms/csh')
14+
15+
OIDC_REDIRECT_URI = os.environ.get('OIDC_REDIRECT_URI',
16+
'https://eac.csh.rit.edu/redirect_uri')
1417
OIDC_CLIENT_CONFIG = {
1518
'client_id': os.environ.get('OIDC_CLIENT_ID', ''),
1619
'client_secret': os.environ.get('OIDC_CLIENT_SECRET', ''),
@@ -28,9 +31,10 @@
2831
SLACK_SECRET = os.environ.get('SLACK_SECRET', '')
2932

3033
# GitHub secrets
31-
GITHUB_OAUTH_CLIENT_ID = os.environ.get('GITHUB_OAUTH_CLIENT_ID', '')
32-
GITHUB_OAUTH_CLIENT_SECRET = os.environ.get('GITHUB_OAUTH_CLIENT_SECRET', '')
34+
GITHUB_REDIRECT_URI = os.environ.get('GITHUB_REDIRECT_URI',
35+
'https://eac.csh.rit.edu/github/return')
3336
GITHUB_APP_CLIENT_ID = os.environ.get('GITHUB_APP_CLIENT_ID', '')
37+
GITHUB_APP_CLIENT_SECRET = os.environ.get('GITHUB_APP_CLIENT_SECRET', '')
3438
GITHUB_APP_PRIVATE_KEY = os.environ.get('GITHUB_APP_PRIVATE_KEY', '')
3539

3640
# Twitch secrets

eac/__init__.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
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

Comments
 (0)