Skip to content

Commit

Permalink
fix for sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Nov 7, 2023
1 parent 3556fa0 commit 191abd2
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions jupyter_server/auth/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
from .security import passwd_check, set_password
from .utils import get_anonymous_username

# circular imports for type checking
if t.TYPE_CHECKING:
from jupyter_server.base.handlers import AuthenticatedHandler, JupyterHandler
from jupyter_server.serverapp import ServerApp

_non_alphanum = re.compile(r"[^A-Za-z0-9]")


Expand Down Expand Up @@ -213,7 +208,7 @@ def _token_default(self):

need_token: bool | Bool[bool, t.Union[bool, int]] = Bool(True)

def get_user(self, handler: JupyterHandler) -> User | None | t.Awaitable[User | None]:
def get_user(self, handler: web.RequestHandler) -> User | None | t.Awaitable[User | None]:
"""Get the authenticated user for a request
Must return a :class:`jupyter_server.auth.User`,
Expand All @@ -228,7 +223,7 @@ def get_user(self, handler: JupyterHandler) -> User | None | t.Awaitable[User |
# not sure how to have optional-async type signature
# on base class with `async def` without splitting it into two methods

async def _get_user(self, handler: JupyterHandler) -> User | None:
async def _get_user(self, handler: web.RequestHandler) -> User | None:
"""Get the user."""
if getattr(handler, "_jupyter_current_user", None):
# already authenticated
Expand Down Expand Up @@ -321,7 +316,7 @@ def user_from_cookie(self, cookie_value: str) -> User | None:
user["color"],
)

def get_cookie_name(self, handler: AuthenticatedHandler) -> str:
def get_cookie_name(self, handler: web.RequestHandler) -> str:
"""Return the login cookie name
Uses IdentityProvider.cookie_name, if defined.
Expand All @@ -333,7 +328,7 @@ def get_cookie_name(self, handler: AuthenticatedHandler) -> str:
else:
return _non_alphanum.sub("-", f"username-{handler.request.host}")

def set_login_cookie(self, handler: AuthenticatedHandler, user: User) -> None:
def set_login_cookie(self, handler: web.RequestHandler, user: User) -> None:
"""Call this on handlers to set the login cookie for success"""
cookie_options = {}
cookie_options.update(self.cookie_options)
Expand All @@ -350,7 +345,7 @@ def set_login_cookie(self, handler: AuthenticatedHandler, user: User) -> None:
handler.set_secure_cookie(cookie_name, self.user_to_cookie(user), **cookie_options)

def _force_clear_cookie(
self, handler: AuthenticatedHandler, name: str, path: str = "/", domain: str | None = None
self, handler: web.RequestHandler, name: str, path: str = "/", domain: str | None = None
) -> None:
"""Deletes the cookie with the given name.
Expand All @@ -376,7 +371,7 @@ def _force_clear_cookie(
morsel["domain"] = domain
handler.add_header("Set-Cookie", morsel.OutputString())

def clear_login_cookie(self, handler: AuthenticatedHandler) -> None:
def clear_login_cookie(self, handler: web.RequestHandler) -> None:
"""Clear the login cookie, effectively logging out the session."""
cookie_options = {}
cookie_options.update(self.cookie_options)
Expand All @@ -390,7 +385,9 @@ def clear_login_cookie(self, handler: AuthenticatedHandler) -> None:
# two cookies with the same name. See the method above.
self._force_clear_cookie(handler, cookie_name)

def get_user_cookie(self, handler: JupyterHandler) -> User | None | t.Awaitable[User | None]:
def get_user_cookie(
self, handler: web.RequestHandler
) -> User | None | t.Awaitable[User | None]:
"""Get user from a cookie
Calls user_from_cookie to deserialize cookie value
Expand All @@ -413,7 +410,7 @@ def get_user_cookie(self, handler: JupyterHandler) -> User | None | t.Awaitable[

auth_header_pat = re.compile(r"(token|bearer)\s+(.+)", re.IGNORECASE)

def get_token(self, handler: JupyterHandler) -> str | None:
def get_token(self, handler: web.RequestHandler) -> str | None:
"""Get the user token from a request
Default:
Expand All @@ -429,7 +426,7 @@ def get_token(self, handler: JupyterHandler) -> str | None:
user_token = m.group(2)
return user_token

async def get_user_token(self, handler: JupyterHandler) -> User | None:
async def get_user_token(self, handler: web.RequestHandler) -> User | None:
"""Identify the user based on a token in the URL or Authorization header
Returns:
Expand Down Expand Up @@ -464,7 +461,7 @@ async def get_user_token(self, handler: JupyterHandler) -> User | None:
else:
return None

def generate_anonymous_user(self, handler: JupyterHandler) -> User:
def generate_anonymous_user(self, handler: web.RequestHandler) -> User:
"""Generate a random anonymous user.
For use when a single shared token is used,
Expand All @@ -478,7 +475,7 @@ def generate_anonymous_user(self, handler: JupyterHandler) -> User:
handler.log.debug(f"Generating new user for token-authenticated request: {user_id}")
return User(user_id, name, display_name, initials, None, color)

def should_check_origin(self, handler: AuthenticatedHandler) -> bool:
def should_check_origin(self, handler: web.RequestHandler) -> bool:
"""Should the Handler check for CORS origin validation?
Origin check should be skipped for token-authenticated requests.
Expand All @@ -489,7 +486,7 @@ def should_check_origin(self, handler: AuthenticatedHandler) -> bool:
"""
return not self.is_token_authenticated(handler)

def is_token_authenticated(self, handler: AuthenticatedHandler) -> bool:
def is_token_authenticated(self, handler: web.RequestHandler) -> bool:
"""Returns True if handler has been token authenticated. Otherwise, False.
Login with a token is used to signal certain things, such as:
Expand All @@ -504,7 +501,7 @@ def is_token_authenticated(self, handler: AuthenticatedHandler) -> bool:

def validate_security(
self,
app: ServerApp,
app: t.Any,
ssl_options: dict[str, t.Any] | None = None,
) -> None:
"""Check the application's security.
Expand All @@ -526,7 +523,7 @@ def validate_security(
" Anyone who can connect to this server will be able to run code."
)

def process_login_form(self, handler: JupyterHandler) -> User | None:
def process_login_form(self, handler: web.RequestHandler) -> User | None:
"""Process login form data
Return authenticated User if successful, None if not.
Expand Down Expand Up @@ -633,7 +630,7 @@ def passwd_check(self, password):
"""Check password against our stored hashed password"""
return passwd_check(self.hashed_password, password)

def process_login_form(self, handler: JupyterHandler) -> User | None:
def process_login_form(self, handler: web.RequestHandler) -> User | None:
"""Process login form data
Return authenticated User if successful, None if not.
Expand All @@ -659,7 +656,7 @@ def process_login_form(self, handler: JupyterHandler) -> User | None:

def validate_security(
self,
app: ServerApp,
app: t.Any,
ssl_options: dict[str, t.Any] | None = None,
) -> None:
"""Handle security validation."""
Expand Down Expand Up @@ -700,7 +697,7 @@ def _default_login_handler_class(self):
def auth_enabled(self):
return self.login_available

def get_user(self, handler: JupyterHandler) -> User | None:
def get_user(self, handler: web.RequestHandler) -> User | None:
"""Get the user."""
user = self.login_handler_class.get_user(handler) # type:ignore[attr-defined]
if user is None:
Expand All @@ -715,17 +712,17 @@ def login_available(self) -> bool:
)
)

def should_check_origin(self, handler: AuthenticatedHandler) -> bool:
def should_check_origin(self, handler: web.RequestHandler) -> bool:
"""Whether we should check origin."""
return bool(self.login_handler_class.should_check_origin(handler)) # type:ignore[attr-defined]

def is_token_authenticated(self, handler: AuthenticatedHandler) -> bool:
def is_token_authenticated(self, handler: web.RequestHandler) -> bool:
"""Whether we are token authenticated."""
return bool(self.login_handler_class.is_token_authenticated(handler)) # type:ignore[attr-defined]

def validate_security(
self,
app: ServerApp,
app: t.Any,
ssl_options: dict[str, t.Any] | None = None,
) -> None:
"""Validate security."""
Expand Down

0 comments on commit 191abd2

Please sign in to comment.