-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial attempt to get JWT authentication * Getting to read the token * Getting closer * Just linting * JWT authentication improvements * An attempt to wrap the API with JWT Lots of hardcoded stuff and assumptions, but works as expected. It can be run as a service with a config like this ``` c.JupyterHub.load_roles = [ { "name": "jwt", "scopes": [ "read:users:activity", # read user last_activity "servers", # start and stop servers "admin:users", # needed if culling idle users as well ], }, { "name": "user", "scopes": ["access:services", "self"], }, ] c.JupyterHub.services = [ { "name": "jwt", "url": "http://localhost:1984/", # any secret >8 characters, you'll use api_token to # authenticate api requests to the hub from your service "api_token": "super-secret", } ] ``` and then start the service: ``` fastapi dev --port 1984 api_wrapper.py ``` * Improve linting * Removed unneeded function * Generalise for all HTTP methods * Raise 403 when appropriate * Better error handling * Adjust the duration of tokens Also remove some unneeded code * Remove dangling code * Add fastapi as requirement
- Loading branch information
Showing
4 changed files
with
169 additions
and
6 deletions.
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,56 @@ | ||
import httpx | ||
from fastapi import FastAPI, HTTPException, Request | ||
|
||
app = FastAPI() | ||
|
||
AUTH_HEADER = "authorization" | ||
TOKEN_TYPE = "bearer" | ||
URL = "http://localhost:8000/hub/jwt_login" | ||
API_URL = "http://localhost:8000/hub/api" | ||
PREFIX = "services/jwt" | ||
|
||
|
||
# wrapping all the HTTP actions in a single function | ||
@app.get("/{svc_path:path}") | ||
@app.put("/{svc_path:path}") | ||
@app.post("/{svc_path:path}") | ||
@app.delete("/{svc_path:path}") | ||
@app.options("/{svc_path:path}") | ||
@app.head("/{svc_path:path}") | ||
@app.patch("/{svc_path:path}") | ||
@app.trace("/{svc_path:path}") | ||
async def api_wrapper(request: Request, svc_path: str): | ||
token_header = {} | ||
if AUTH_HEADER in request.headers: | ||
f = request.headers[AUTH_HEADER].split() | ||
if len(f) == 2 and f[0].lower() == TOKEN_TYPE: | ||
try: | ||
async with httpx.AsyncClient() as client: | ||
r = await client.get( | ||
URL, headers={AUTH_HEADER: request.headers[AUTH_HEADER]} | ||
) | ||
r.raise_for_status() | ||
user_token = r.json() | ||
token_header[AUTH_HEADER] = f"token {user_token['token']}" | ||
except httpx.HTTPStatusError as exc: | ||
if exc.response.status_code != 403: | ||
raise HTTPException( | ||
status_code=exc.response.status_code, detail=exc.response.text | ||
) | ||
content = await request.body() | ||
api_path = svc_path.removeprefix(PREFIX) | ||
async with httpx.AsyncClient() as client: | ||
# which headers do we need to preserve? | ||
headers = dict(request.headers) | ||
if AUTH_HEADER in headers: | ||
del headers[AUTH_HEADER] | ||
headers.update(token_header) | ||
method = getattr(client, request.method.lower()) | ||
if content: | ||
r = await method(API_URL + api_path, content=content, headers=headers) | ||
else: | ||
r = await method(API_URL + api_path, headers=headers) | ||
try: | ||
return r.json() | ||
except ValueError: | ||
return r.content |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ jupyterhub>=4.0.2 | |
oauthenticator>=16.1.0 | ||
jupyterhub-kubespawner>=6.1.0 | ||
xmltodict | ||
fastapi |