forked from jupyter-server/jupyter_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aeb6f9d
commit 76ac37f
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import typing as ty | ||
|
||
import logging | ||
from jupyter_server.gateway.gateway_client import GatewayTokenRenewerBase | ||
import jupyter_server.base.handlers | ||
import jupyter_server.serverapp | ||
|
||
|
||
def get_header_value(request: ty.Any, header: str) -> str: | ||
if header not in request.headers: | ||
logging.error(f'Header "{header}" is missing') | ||
return "" | ||
logging.debug(f'Getting value from header "{header}"') | ||
value = request.headers[header] | ||
if len(value) == 0: | ||
logging.error(f'Header "{header}" is empty') | ||
return "" | ||
return value | ||
|
||
|
||
class SpotTokenRenewer(GatewayTokenRenewerBase): | ||
|
||
def get_token( | ||
self, | ||
auth_header_key: str, | ||
auth_scheme: ty.Union[str, None], | ||
auth_token: str, | ||
**kwargs: ty.Any, | ||
) -> str: | ||
request = jupyter_server.base.handlers.get_current_request() | ||
if request is None: | ||
logging.error("Could not get current request") | ||
return auth_token | ||
|
||
auth_header_value = get_header_value(request, auth_header_key) | ||
if auth_header_value: | ||
try: | ||
# We expect the header value to be of the form "Bearer: XXX" | ||
auth_token = auth_header_value.split(" ", maxsplit=1)[1] | ||
except Exception as e: | ||
logging.error(f"Could not read token from auth header: {str(e)}") | ||
|
||
return auth_token |