/
auth.py
732 lines (576 loc) · 22.1 KB
/
auth.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import codecs
import json
import logging
import os
import pkg_resources
import re
import uuid
from urllib.parse import urlencode
import tornado
from bokeh.server.auth_provider import AuthProvider
from tornado.auth import OAuth2Mixin
from tornado.httpclient import HTTPRequest, HTTPError
from tornado.httputil import url_concat
from .config import config
from .io import state
from .util import base64url_encode, base64url_decode
log = logging.getLogger(__name__)
STATE_COOKIE_NAME = 'panel-oauth-state'
def decode_response_body(response):
"""
Decodes the JSON-format response body
Arguments
---------
response: tornado.httpclient.HTTPResponse
Returns
-------
Decoded response content
"""
# Fix GitHub response.
try:
body = codecs.decode(response.body, 'ascii')
except Exception:
body = codecs.decode(response.body, 'utf-8')
body = re.sub('"', '\"', body)
body = re.sub("'", '"', body)
body = json.loads(body)
return body
def decode_id_token(id_token):
"""
Decodes a signed ID JWT token.
"""
signing_input, _ = id_token.encode('utf-8').rsplit(b".", 1)
_, payload_segment = signing_input.split(b".", 1)
return json.loads(base64url_decode(payload_segment).decode('utf-8'))
def extract_urlparam(name, urlparam):
"""
Attempts to extract a url parameter embedded in another URL
parameter.
"""
if urlparam is None:
return None
query = name+'='
if query in urlparam:
split_args = urlparam[urlparam.index(query):].replace(query, '').split('&')
return split_args[0] if split_args else None
else:
return None
class OAuthLoginHandler(tornado.web.RequestHandler):
_API_BASE_HEADERS = {
'Accept': 'application/json',
'User-Agent': 'Tornado OAuth'
}
_access_token_header = None
_EXTRA_TOKEN_PARAMS = {}
_SCOPE = None
_state_cookie = None
async def get_authenticated_user(self, redirect_uri, client_id, state,
client_secret=None, code=None):
"""
Fetches the authenticated user
Arguments
---------
redirect_uri: (str)
The OAuth redirect URI
client_id: (str)
The OAuth client ID
state: (str)
The unguessable random string to protect against
cross-site request forgery attacks
client_secret: (str, optional)
The client secret
code: (str, optional)
The response code from the server
"""
if code:
return await self._fetch_access_token(
code,
redirect_uri,
client_id,
client_secret
)
params = {
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret,
'response_type': 'code',
'extra_params': {
'state': state,
},
}
if self._SCOPE is not None:
params['scope'] = self._SCOPE
if 'scope' in config.oauth_extra_params:
params['scope'] = config.oauth_extra_params['scope']
log.debug("%s making authorize request", type(self).__name__)
self.authorize_redirect(**params)
async def _fetch_access_token(self, code, redirect_uri, client_id, client_secret):
"""
Fetches the access token.
Arguments
---------
code:
The response code from the server
redirect_uri:
The redirect URI
client_id:
The client ID
client_secret:
The client secret
state:
The unguessable random string to protect against cross-site
request forgery attacks
"""
if not client_secret:
raise ValueError('The client secret is undefined.')
log.debug("%s making access token request.", type(self).__name__)
params = {
'code': code,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret,
**self._EXTRA_TOKEN_PARAMS
}
http = self.get_auth_http_client()
# Request the access token.
req = HTTPRequest(
self._OAUTH_ACCESS_TOKEN_URL,
method='POST',
body=urlencode(params),
headers=self._API_BASE_HEADERS
)
try:
response = await http.fetch(req)
except HTTPError as e:
return self._on_error(e.response)
body = decode_response_body(response)
if not body:
return
if 'access_token' not in body:
return self._on_error(response, body)
user_headers = dict(self._API_BASE_HEADERS)
if self._access_token_header:
user_url = self._OAUTH_USER_URL
user_headers['Authorization'] = self._access_token_header.format(
body['access_token']
)
else:
user_url = '{}{}'.format(self._OAUTH_USER_URL, body['access_token'])
user_response = await http.fetch(user_url, headers=user_headers)
user = decode_response_body(user_response)
if not user:
return
log.debug("%s received user information.", type(self).__name__)
return self._on_auth(user, body['access_token'])
def get_state_cookie(self):
"""Get OAuth state from cookies
To be compared with the value in redirect URL
"""
if self._state_cookie is None:
self._state_cookie = (
self.get_secure_cookie(STATE_COOKIE_NAME, max_age_days=config.oauth_expiry) or b''
).decode('utf8', 'replace')
self.clear_cookie(STATE_COOKIE_NAME)
return self._state_cookie
def set_state_cookie(self, state):
self.set_secure_cookie(
STATE_COOKIE_NAME, state, expires_days=config.oauth_expiry, httponly=True
)
async def get(self):
log.debug("%s received login request", type(self).__name__)
if config.oauth_redirect_uri:
redirect_uri = config.oauth_redirect_uri
else:
redirect_uri = "{0}://{1}".format(
self.request.protocol,
self.request.host
)
params = {
'redirect_uri': redirect_uri,
'client_id': config.oauth_key,
}
# Some OAuth2 backends do not correctly return code
next_arg = self.get_argument('next', None)
url_state = self.get_argument('state', None)
code = self.get_argument('code', extract_urlparam('code', next_arg))
url_state = self.get_argument('state', extract_urlparam('state', next_arg))
# Seek the authorization
cookie_state = self.get_state_cookie()
if code:
if cookie_state != url_state:
log.warning("OAuth state mismatch: %s != %s", cookie_state, url_state)
raise HTTPError(400, "OAuth state mismatch")
# For security reason, the state value (cross-site token) will be
# retrieved from the query string.
params.update({
'client_secret': config.oauth_secret,
'code': code,
'state': url_state
})
user = await self.get_authenticated_user(**params)
if user is None:
raise HTTPError(403)
log.debug("%s authorized user, redirecting to app.", type(self).__name__)
self.redirect('/')
else:
# Redirect for user authentication
state = uuid.uuid4().hex
params['state'] = state
self.set_state_cookie(state)
await self.get_authenticated_user(**params)
def _on_auth(self, user_info, access_token):
user_key = config.oauth_jwt_user or self._USER_KEY
user = user_info[user_key]
self.set_secure_cookie('user', user, expires_days=config.oauth_expiry)
id_token = base64url_encode(json.dumps(user_info))
if state.encryption:
access_token = state.encryption.encrypt(access_token.encode('utf-8'))
id_token = state.encryption.encrypt(id_token.encode('utf-8'))
self.set_secure_cookie('access_token', access_token, expires_days=config.oauth_expiry)
self.set_secure_cookie('id_token', id_token, expires_days=config.oauth_expiry)
return user
def _on_error(self, response, body=None):
self.clear_all_cookies()
try:
body = body or decode_response_body(response)
except json.decoder.JSONDecodeError:
body = body
provider = self.__class__.__name__.replace('LoginHandler', '')
if response.error:
log.error(f"{provider} OAuth provider returned a {response.error} "
f"error. The full response was: {body}")
else:
log.warning(f"{provider} OAuth provider failed to fully "
f"authenticate returning the following response:"
f"{body}.")
raise HTTPError(500, f"{provider} authentication failed")
class GenericLoginHandler(OAuthLoginHandler, OAuth2Mixin):
_access_token_header = 'Bearer {}'
_EXTRA_TOKEN_PARAMS = {
'grant_type': 'authorization_code'
}
@property
def _OAUTH_ACCESS_TOKEN_URL(self):
return config.oauth_extra_params.get('TOKEN_URL', os.environ.get('PANEL_OAUTH_TOKEN_URL'))
@property
def _OAUTH_AUTHORIZE_URL(self):
return config.oauth_extra_params.get('AUTHORIZE_URL', os.environ.get('PANEL_OAUTH_AUTHORIZE_URL'))
@property
def _OAUTH_USER_URL(self):
return config.oauth_extra_params.get('USER_URL', os.environ.get('PANEL_OAUTH_USER_URL'))
@property
def _SCOPE(self):
if 'PANEL_OAUTH_SCOPE' not in os.environ:
return ['openid', 'email']
return [scope for scope in os.environ['PANEL_OAUTH_SCOPE'].split(',')]
@property
def _USER_KEY(self):
return config.oauth_extra_params.get('USER_KEY', os.environ.get('PANEL_USER_KEY', 'email'))
class GithubLoginHandler(OAuthLoginHandler, OAuth2Mixin):
"""GitHub OAuth2 Authentication
To authenticate with GitHub, first register your application at
https://github.com/settings/applications/new to get the client ID and
secret.
"""
_EXTRA_AUTHORIZE_PARAMS = {}
_OAUTH_ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
_OAUTH_AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'
_OAUTH_USER_URL = 'https://api.github.com/user'
_access_token_header = 'token {}'
_USER_KEY = 'login'
class BitbucketLoginHandler(OAuthLoginHandler, OAuth2Mixin):
_API_BASE_HEADERS = {
"Accept": "application/json",
}
_EXTRA_TOKEN_PARAMS = {
'grant_type': 'authorization_code'
}
_OAUTH_ACCESS_TOKEN_URL = "https://bitbucket.org/site/oauth2/access_token"
_OAUTH_AUTHORIZE_URL = "https://bitbucket.org/site/oauth2/authorize"
_OAUTH_USER_URL = "https://api.bitbucket.org/2.0/user?access_token="
_USER_KEY = 'username'
class Auth0Handler(OAuthLoginHandler, OAuth2Mixin):
_EXTRA_AUTHORIZE_PARAMS = {
'subdomain'
}
_OAUTH_ACCESS_TOKEN_URL_ = 'https://{0}.auth0.com/oauth/token'
_OAUTH_AUTHORIZE_URL_ = 'https://{0}.auth0.com/authorize'
_OAUTH_USER_URL_ = 'https://{0}.auth0.com/userinfo?access_token='
@property
def _OAUTH_ACCESS_TOKEN_URL(self):
url = config.oauth_extra_params.get('subdomain', 'example')
return self._OAUTH_ACCESS_TOKEN_URL_.format(url)
@property
def _OAUTH_AUTHORIZE_URL(self):
url = config.oauth_extra_params.get('subdomain', 'example')
return self._OAUTH_AUTHORIZE_URL_.format(url)
@property
def _OAUTH_USER_URL(self):
url = config.oauth_extra_params.get('subdomain', 'example')
return self._OAUTH_USER_URL_.format(url)
_USER_KEY = 'email'
_EXTRA_TOKEN_PARAMS = {
'grant_type': 'authorization_code'
}
class GitLabLoginHandler(OAuthLoginHandler, OAuth2Mixin):
_API_BASE_HEADERS = {
'Accept': 'application/json',
}
_EXTRA_TOKEN_PARAMS = {
'grant_type': 'authorization_code'
}
_OAUTH_ACCESS_TOKEN_URL_ = 'https://{0}/oauth/token'
_OAUTH_AUTHORIZE_URL_ = 'https://{0}/oauth/authorize'
_OAUTH_USER_URL_ = 'https://{0}/api/v4/user'
_USER_KEY = 'username'
@property
def _OAUTH_ACCESS_TOKEN_URL(self):
url = config.oauth_extra_params.get('url', 'gitlab.com')
return self._OAUTH_ACCESS_TOKEN_URL_.format(url)
@property
def _OAUTH_AUTHORIZE_URL(self):
url = config.oauth_extra_params.get('url', 'gitlab.com')
return self._OAUTH_AUTHORIZE_URL_.format(url)
@property
def _OAUTH_USER_URL(self):
url = config.oauth_extra_params.get('url', 'gitlab.com')
return self._OAUTH_USER_URL_.format(url)
async def _fetch_access_token(self, code, redirect_uri, client_id, client_secret):
"""
Fetches the access token.
Arguments
----------
code:
The response code from the server
redirect_uri:
The redirect URI
client_id:
The client ID
client_secret:
The client secret
state:
The unguessable random string to protect against cross-site
request forgery attacks
"""
if not client_secret:
raise ValueError('The client secret is undefined.')
log.debug("%s making access token request.", type(self).__name__)
http = self.get_auth_http_client()
params = {
'code': code,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret,
**self._EXTRA_TOKEN_PARAMS
}
url = url_concat(self._OAUTH_ACCESS_TOKEN_URL, params)
# Request the access token.
req = HTTPRequest(
url,
method="POST",
headers=self._API_BASE_HEADERS,
body=''
)
try:
response = await http.fetch(req)
except HTTPError as e:
return self._on_error(e.response)
body = decode_response_body(response)
if not body:
return
if 'access_token' not in body:
return self._on_error(response, body)
log.debug("%s granted access_token.", type(self).__name__)
headers = dict(self._API_BASE_HEADERS, **{
"Authorization": "Bearer {}".format(body['access_token']),
})
user_response = await http.fetch(
self._OAUTH_USER_URL,
method="GET",
headers=headers
)
user = decode_response_body(user_response)
if not user:
return
log.debug("%s received user information.", type(self).__name__)
return self._on_auth(user, body['access_token'])
class OAuthIDTokenLoginHandler(OAuthLoginHandler):
_API_BASE_HEADERS = {
'Content-Type':
'application/x-www-form-urlencoded; charset=UTF-8'
}
_EXTRA_AUTHORIZE_PARAMS = {
'grant_type': 'authorization_code'
}
async def _fetch_access_token(self, code, redirect_uri, client_id, client_secret):
"""
Fetches the access token.
Arguments
----------
code:
The response code from the server
redirect_uri:
The redirect URI
client_id:
The client ID
client_secret:
The client secret
state:
The unguessable random string to protect against cross-site
request forgery attacks
"""
if not client_secret:
raise ValueError('The client secret are undefined.')
log.debug("%s making access token request.", type(self).__name__)
http = self.get_auth_http_client()
params = {
'code': code,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret,
**self._EXTRA_AUTHORIZE_PARAMS
}
data = urlencode(
params, doseq=True, encoding='utf-8', safe='=')
# Request the access token.
req = HTTPRequest(
self._OAUTH_ACCESS_TOKEN_URL,
method="POST",
headers=self._API_BASE_HEADERS,
body=data
)
try:
response = await http.fetch(req)
except HTTPError as e:
return self._on_error(e.response)
body = decode_response_body(response)
if 'access_token' not in body:
return self._on_error(response, body)
log.debug("%s granted access_token.", type(self).__name__)
access_token = body['access_token']
id_token = body['id_token']
return self._on_auth(id_token, access_token)
def _on_auth(self, id_token, access_token):
decoded = decode_id_token(id_token)
user_key = config.oauth_jwt_user or self._USER_KEY
if user_key in decoded:
user = decoded[user_key]
else:
log.error("%s token payload did not contain expected %r.",
type(self).__name__, user_key)
raise HTTPError(400, "OAuth token payload missing user information")
self.set_secure_cookie('user', user, expires_days=config.oauth_expiry)
if state.encryption:
access_token = state.encryption.encrypt(access_token.encode('utf-8'))
id_token = state.encryption.encrypt(id_token.encode('utf-8'))
self.set_secure_cookie('access_token', access_token, expires_days=config.oauth_expiry)
self.set_secure_cookie('id_token', id_token, expires_days=config.oauth_expiry)
return user
class AzureAdLoginHandler(OAuthIDTokenLoginHandler, OAuth2Mixin):
_API_BASE_HEADERS = {
'Accept': 'application/json',
'User-Agent': 'Tornado OAuth'
}
_OAUTH_ACCESS_TOKEN_URL_ = 'https://login.microsoftonline.com/{tenant}/oauth2/token'
_OAUTH_AUTHORIZE_URL_ = 'https://login.microsoftonline.com/{tenant}/oauth2/authorize'
_OAUTH_USER_URL_ = ''
_USER_KEY = 'unique_name'
@property
def _OAUTH_ACCESS_TOKEN_URL(self):
tenant = os.environ.get('AAD_TENANT_ID', config.oauth_extra_params.get('tenant', 'common'))
return self._OAUTH_ACCESS_TOKEN_URL_.format(tenant=tenant)
@property
def _OAUTH_AUTHORIZE_URL(self):
tenant = os.environ.get('AAD_TENANT_ID', config.oauth_extra_params.get('tenant', 'common'))
return self._OAUTH_AUTHORIZE_URL_.format(tenant=tenant)
@property
def _OAUTH_USER_URL(self):
return self._OAUTH_USER_URL_.format(**config.oauth_extra_params)
class OktaLoginHandler(OAuthIDTokenLoginHandler, OAuth2Mixin):
"""Okta OAuth2 Authentication
To authenticate with Okta you first need to set up and configure
in the Okta developer console.
"""
_EXTRA_TOKEN_PARAMS = {
'grant_type': 'authorization_code',
'response_type': 'code,token,id_token'
}
_OAUTH_ACCESS_TOKEN_URL_ = 'https://{0}/oauth2/{1}/v1/token'
_OAUTH_ACCESS_TOKEN_URL__ = 'https://{0}/oauth2/v1/token'
_OAUTH_AUTHORIZE_URL_ = 'https://{0}/oauth2/{1}/v1/authorize'
_OAUTH_AUTHORIZE_URL__ = 'https://{0}/oauth2/v1/authorize'
_OAUTH_USER_URL_ = 'https://{0}/oauth2/{1}/v1/userinfo?access_token='
_OAUTH_USER_URL__ = 'https://{0}/oauth2/v1/userinfo?access_token='
_USER_KEY = 'email'
_SCOPE = ['openid', 'email', 'profile']
@property
def _OAUTH_ACCESS_TOKEN_URL(self):
url = config.oauth_extra_params.get('url', 'okta.com')
server = config.oauth_extra_params.get('server', 'default')
if server:
return self._OAUTH_ACCESS_TOKEN_URL_.format(url, server)
else:
return self._OAUTH_ACCESS_TOKEN_URL__.format(url)
@property
def _OAUTH_AUTHORIZE_URL(self):
url = config.oauth_extra_params.get('url', 'okta.com')
server = config.oauth_extra_params.get('server', 'default')
if server:
return self._OAUTH_AUTHORIZE_URL_.format(url, server)
else:
return self._OAUTH_AUTHORIZE_URL__.format(url)
@property
def _OAUTH_USER_URL(self):
url = config.oauth_extra_params.get('url', 'okta.com')
server = config.oauth_extra_params.get('server', 'default')
if server:
return self._OAUTH_USER_URL_.format(url, server)
else:
return self._OAUTH_USER_URL__.format(url, server)
class GoogleLoginHandler(OAuthIDTokenLoginHandler, OAuth2Mixin):
_API_BASE_HEADERS = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
}
_OAUTH_AUTHORIZE_URL = "https://accounts.google.com/o/oauth2/v2/auth"
_OAUTH_ACCESS_TOKEN_URL = "https://accounts.google.com/o/oauth2/token"
_SCOPE = ['profile', 'email']
_USER_KEY = 'email'
class LogoutHandler(tornado.web.RequestHandler):
def get(self):
self.clear_cookie("user")
self.clear_cookie("id_token")
self.clear_cookie("access_token")
self.clear_cookie(STATE_COOKIE_NAME)
self.redirect("/")
class OAuthProvider(AuthProvider):
@property
def get_user(self):
def get_user(request_handler):
return request_handler.get_secure_cookie("user", max_age_days=config.oauth_expiry)
return get_user
@property
def login_url(self):
return '/login'
@property
def login_handler(self):
return AUTH_PROVIDERS[config.oauth_provider]
@property
def logout_url(self):
return "/logout"
@property
def logout_handler(self):
return LogoutHandler
AUTH_PROVIDERS = {
'auth0': Auth0Handler,
'azure': AzureAdLoginHandler,
'bitbucket': BitbucketLoginHandler,
'generic': GenericLoginHandler,
'google': GoogleLoginHandler,
'github': GithubLoginHandler,
'gitlab': GitLabLoginHandler,
'okta': OktaLoginHandler
}
# Populate AUTH Providers from external extensions
for entry_point in pkg_resources.iter_entry_points('panel.auth'):
AUTH_PROVIDERS[entry_point.name] = entry_point.resolve()
config.param.objects(False)['_oauth_provider'].objects = list(AUTH_PROVIDERS.keys())