Skip to content

Commit 43975c5

Browse files
committed
Default to anonymous identity provider with token authentication
1 parent 9f5039c commit 43975c5

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

kernel_gateway/auth/identity.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
to be used in combination with Authorizer for _authorization_.
77
"""
88
from traitlets import default
9+
from tornado import web
910

10-
from jupyter_server.auth.identity import IdentityProvider
11+
from jupyter_server.auth.identity import IdentityProvider, User
1112
from jupyter_server.base.handlers import JupyterHandler
1213

1314

1415
class GatewayIdentityProvider(IdentityProvider):
1516
"""
1617
Interface for providing identity management and authentication for a Gateway server.
1718
"""
19+
1820
@default("token")
1921
def _token_default(self):
20-
# if the superclass generated a token, but auth_token is configured on
21-
# the Gateway server, reset token_generated and use the configured value.
22-
token_default = super()._token_default()
23-
if self.token_generated and self.parent.auth_token:
24-
self.token_generated = False
25-
return self.parent.auth_token
26-
return token_default
22+
return self.parent.auth_token
23+
24+
@property
25+
def auth_enabled(self):
26+
if not self.token:
27+
return False
28+
return True
2729

2830
def should_check_origin(self, handler: JupyterHandler) -> bool:
2931
"""Should the Handler check for CORS origin validation?
@@ -36,3 +38,21 @@ def should_check_origin(self, handler: JupyterHandler) -> bool:
3638
"""
3739
# Always check the origin unless operator configured gateway to allow any
3840
return handler.settings["kg_allow_origin"] != "*"
41+
42+
def generate_anonymous_user(self, handler: web.RequestHandler) -> User:
43+
"""Generate a random anonymous user.
44+
45+
For use when a single shared token is used,
46+
but does not identify a user.
47+
"""
48+
name = display_name = f"Anonymous"
49+
initials = "An"
50+
color = None
51+
return User(name.lower(), name, display_name, initials, None, color)
52+
53+
def is_token_authenticated(self, handler: web.RequestHandler) -> bool:
54+
"""The default authentication flow of Gateway is token auth.
55+
56+
The only other option is no auth
57+
"""
58+
return True

0 commit comments

Comments
 (0)