forked from jupyterhub/oauthenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
275 lines (231 loc) · 9.86 KB
/
github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Authenticator to use GitHub OAuth with JupyterHub
"""
import json
import os
import warnings
from jupyterhub.auth import LocalAuthenticator
from tornado import web
from tornado.httpclient import HTTPRequest
from tornado.httputil import url_concat
from traitlets import default
from traitlets import Set
from traitlets import Unicode
from .oauth2 import OAuthenticator
def _api_headers(access_token):
return {
"Accept": "application/json",
"User-Agent": "JupyterHub",
"Authorization": "token {}".format(access_token),
}
class GitHubOAuthenticator(OAuthenticator):
# see github_scopes.md for details about scope config
# set scopes via config, e.g.
# c.GitHubOAuthenticator.scope = ['read:org']
_deprecated_oauth_aliases = {
"github_organization_whitelist": ("allowed_organizations", "0.12.0"),
**OAuthenticator._deprecated_oauth_aliases,
}
login_service = "GitHub"
github_url = Unicode("https://github.com", config=True)
@default("github_url")
def _github_url_default(self):
github_url = os.environ.get("GITHUB_URL")
if not github_url:
# fallback on older GITHUB_HOST config,
# treated the same as GITHUB_URL
host = os.environ.get("GITHUB_HOST")
if host:
if os.environ.get("GITHUB_HTTP"):
protocol = "http"
warnings.warn(
'Use of GITHUB_HOST with GITHUB_HTTP might be deprecated in the future. '
'Use GITHUB_URL=http://{} to set host and protocol together.'.format(
host
),
PendingDeprecationWarning,
)
else:
protocol = "https"
github_url = "{}://{}".format(protocol, host)
if github_url:
if '://' not in github_url:
# ensure protocol is included, assume https if missing
github_url = 'https://' + github_url
return github_url
else:
# nothing specified, this is the true default
github_url = "https://github.com"
# ensure no trailing slash
return github_url.rstrip("/")
github_api = Unicode("https://api.github.com", config=True)
@default("github_api")
def _github_api_default(self):
if self.github_url == "https://github.com":
return "https://api.github.com"
else:
return self.github_url + "/api/v3"
@default("authorize_url")
def _authorize_url_default(self):
return "%s/login/oauth/authorize" % (self.github_url)
@default("token_url")
def _token_url_default(self):
return "%s/login/oauth/access_token" % (self.github_url)
# deprecated names
github_client_id = Unicode(config=True, help="DEPRECATED")
def _github_client_id_changed(self, name, old, new):
self.log.warning("github_client_id is deprecated, use client_id")
self.client_id = new
github_client_secret = Unicode(config=True, help="DEPRECATED")
def _github_client_secret_changed(self, name, old, new):
self.log.warning("github_client_secret is deprecated, use client_secret")
self.client_secret = new
client_id_env = 'GITHUB_CLIENT_ID'
client_secret_env = 'GITHUB_CLIENT_SECRET'
github_organization_whitelist = Set(
help="Deprecated, use `GitHubOAuthenticator.allowed_organizations`",
config=True,
)
allowed_organizations = Set(
config=True, help="Automatically allow members of selected organizations"
)
async def authenticate(self, handler, data=None):
"""We set up auth_state based on additional GitHub info if we
receive it.
"""
code = handler.get_argument("code")
# Exchange the OAuth code for a GitHub Access Token
#
# See: https://developer.github.com/v3/oauth/
# GitHub specifies a POST request yet requires URL parameters
params = dict(
client_id=self.client_id, client_secret=self.client_secret, code=code
)
url = url_concat(self.token_url, params)
req = HTTPRequest(
url,
method="POST",
headers={"Accept": "application/json"},
body='', # Body is required for a POST...
validate_cert=self.validate_server_cert,
)
resp_json = await self.fetch(req)
if 'access_token' in resp_json:
access_token = resp_json['access_token']
elif 'error_description' in resp_json:
raise web.HTTPError(
403,
"An access token was not returned: {}".format(
resp_json['error_description']
),
)
else:
raise web.HTTPError(500, "Bad response: {}".format(resp_json))
granted_scopes = []
if resp_json.get("scope"):
granted_scopes = resp_json["scope"].split(",")
# Determine who the logged-in user is
req = HTTPRequest(
self.github_api + "/user",
method="GET",
headers=_api_headers(access_token),
validate_cert=self.validate_server_cert,
)
resp_json = await self.fetch(req, "fetching user info")
username = resp_json["login"]
# username is now the GitHub userid.
if not username:
return None
# Check if user is a member of any allowed organizations.
# This check is performed here, as it requires `access_token`.
if self.allowed_organizations:
for org in self.allowed_organizations:
user_in_org = await self._check_membership_allowed_organizations(
org, username, access_token
)
if user_in_org:
break
else: # User not found in member list for any organisation
self.log.warning("User %s is not in allowed org list", username)
return None
userdict = {"name": username}
# Now we set up auth_state
userdict["auth_state"] = auth_state = {}
# Save the access token and full GitHub reply (name, id, email) in auth state
# These can be used for user provisioning in the Lab/Notebook environment.
# e.g.
# 1) stash the access token
# 2) use the GitHub ID as the id
# 3) set up name/email for .gitconfig
auth_state['access_token'] = access_token
# store the whole user model in auth_state.github_user
auth_state['github_user'] = resp_json
# If a public email is not available, an extra API call has to be made
# to a /user/emails using the access token to retrieve emails. The
# scopes relevant for this are checked based on this documentation:
# - about scopes: https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps#available-scopes
# - about /user/emails: https://docs.github.com/en/rest/reference/users#list-email-addresses-for-the-authenticated-user
#
# Note that the read:user scope does not imply the user:emails scope!
if not auth_state['github_user']['email'] and (
'user' in granted_scopes or 'user:email' in granted_scopes
):
req = HTTPRequest(
self.github_api + "/user/emails",
method="GET",
headers=_api_headers(access_token),
validate_cert=self.validate_server_cert,
)
resp_json = await self.fetch(req, "fetching user emails")
for val in resp_json:
if val["primary"]:
auth_state['github_user']['email'] = val['email']
break
return userdict
async def _check_membership_allowed_organizations(
self, org, username, access_token
):
headers = _api_headers(access_token)
# Check membership of user `username` for organization `org` via api [check-membership](https://developer.github.com/v3/orgs/members/#check-membership)
# With empty scope (even if authenticated by an org member), this
# will only await public org members. You want 'read:org' in order
# to be able to iterate through all members. If you would only like to
# allow certain teams within an organisation, specify
# allowed_organisations = {org_name:team_name}
check_membership_url = self._build_check_membership_url(org, username)
req = HTTPRequest(
check_membership_url,
method="GET",
headers=headers,
validate_cert=self.validate_server_cert,
)
self.log.debug(
"Checking GitHub organization membership: %s in %s?", username, org
)
resp = await self.fetch(req, raise_error=False, parse_json=False)
if resp.code == 204:
self.log.info("Allowing %s as member of %s", username, org)
return True
else:
try:
resp_json = json.loads((resp.body or b'').decode('utf8', 'replace'))
message = resp_json.get('message', '')
except ValueError:
message = ''
self.log.debug(
"%s does not appear to be a member of %s (status=%s): %s",
username,
org,
resp.code,
message,
)
return False
def _build_check_membership_url(self, org: str, username: str) -> str:
if ":" in org:
org, team = org.split(":")
return f"{self.github_api}/orgs/{org}/teams/{team}/members/{username}"
else:
return f"{self.github_api}/orgs/{org}/members/{username}"
class LocalGitHubOAuthenticator(LocalAuthenticator, GitHubOAuthenticator):
"""A version that mixes in local system user creation"""
pass