/
auth_request_handler.py
87 lines (66 loc) · 3.13 KB
/
auth_request_handler.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
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Provide a mixin class to add authorization hooks to a request handler.
'''
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations
import logging # isort:skip
log = logging.getLogger(__name__)
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# External imports
from tornado.web import RequestHandler
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
__all__ = (
'AuthRequestHandler',
)
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
class AuthRequestHandler(RequestHandler):
''' This mixin adds the expected Tornado authorization hooks:
* get_login_url
* get_current_user
* prepare
All of these delegate to the a :class:`~bokeh.serve.auth_provider.AuthProvider`
confiured on the Bokeh tornado application.
'''
def get_login_url(self):
''' Delegates to``get_login_url`` method of the auth provider, or the
``login_url`` attribute.
'''
if self.application.auth_provider.get_login_url is not None:
return self.application.auth_provider.get_login_url(self)
if self.application.auth_provider.login_url is not None:
return self.application.auth_provider.login_url
raise RuntimeError('login_url or get_login_url() must be supplied when authentication hooks are enabled')
def get_current_user(self):
''' Delegate to the synchronous ``get_user`` method of the auth
provider
'''
if self.application.auth_provider.get_user is not None:
return self.application.auth_provider.get_user(self)
return "default_user"
async def prepare(self):
''' Async counterpart to ``get_current_user``
'''
if self.application.auth_provider.get_user_async is not None:
self.current_user = await self.application.auth_provider.get_user_async(self)
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------