-
Notifications
You must be signed in to change notification settings - Fork 62
Add Authenticator for when serving OIDC as a proxy #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e80b535
0589387
939cdb3
fd7be50
3743679
018d72f
b50d216
4b88a7b
ed75f3e
471b797
c279181
a1669a0
7b20ba8
a0bea99
f511991
2cb5268
f6f9a10
3ca1344
765bdae
f3cc565
08b153c
58cf84f
e972d24
d5cd87b
8378221
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Any, Self | ||
|
||
import numpy | ||
|
||
from tiled.adapters.array import ArrayAdapter | ||
|
@@ -55,7 +57,7 @@ def __init__(self, base_url, metadata=None): | |
self.client = MockClient(base_url) | ||
self.metadata = metadata | ||
|
||
def with_session_state(self, state): | ||
def with_session_state(self, state: dict[str, Any]) -> Self: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From a design perspective, it's just expected to still be an Adapter (?) |
||
return AuthenticatedAdapter(self.client, state["token"], metadata=self.metadata) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,10 +5,11 @@ | |||||
import re | ||||||
import secrets | ||||||
from collections.abc import Iterable | ||||||
from typing import Any, Mapping, Optional, cast | ||||||
from typing import Any, Callable, Mapping, Optional, cast | ||||||
|
||||||
import httpx | ||||||
from fastapi import APIRouter, Request | ||||||
from fastapi.security import OAuth2AuthorizationCodeBearer | ||||||
from jose import JWTError, jwt | ||||||
from pydantic import Secret | ||||||
from starlette.responses import RedirectResponse | ||||||
|
@@ -181,6 +182,16 @@ def authorization_endpoint(self) -> httpx.URL: | |||||
cast(str, self._config_from_oidc_url.get("authorization_endpoint")) | ||||||
) | ||||||
|
||||||
async def decode_token(self, access_token: str) -> dict[str, Any]: | ||||||
keys = httpx.get(self.jwks_uri).raise_for_status().json().get("keys", []) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, use a TTL cache to ensure we do not hammer the AuthN provider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (for the result of |
||||||
return jwt.decode( | ||||||
token=access_token, | ||||||
key=keys, | ||||||
algorithms=self.id_token_signing_alg_values_supported, | ||||||
audience=self._audience, | ||||||
access_token=access_token, | ||||||
) | ||||||
|
||||||
async def authenticate(self, request: Request) -> Optional[UserSessionState]: | ||||||
code = request.query_params["code"] | ||||||
# A proxy in the middle may make the request into something like | ||||||
|
@@ -199,24 +210,51 @@ async def authenticate(self, request: Request) -> Optional[UserSessionState]: | |||||
logger.error("Authentication error: %r", response_body) | ||||||
return None | ||||||
response_body = response.json() | ||||||
id_token = response_body["id_token"] | ||||||
access_token = response_body["access_token"] | ||||||
keys = httpx.get(self.jwks_uri).raise_for_status().json().get("keys", []) | ||||||
try: | ||||||
verified_body = jwt.decode( | ||||||
token=id_token, | ||||||
key=keys, | ||||||
algorithms=self.id_token_signing_alg_values_supported, | ||||||
audience=self._audience, | ||||||
access_token=access_token, | ||||||
verified_body = await self.decode_token(access_token) | ||||||
return UserSessionState(verified_body["sub"], {}) | ||||||
|
||||||
except JWTError: | ||||||
logger.exception( | ||||||
"Authentication error. Unverified token: %r", | ||||||
jwt.get_unverified_claims(access_token), | ||||||
) | ||||||
return None | ||||||
|
||||||
|
||||||
class ProxiedOIDCAuthenticator(OIDCAuthenticator): | ||||||
def __init__( | ||||||
self, | ||||||
audience: str, | ||||||
client_id: str, | ||||||
client_secret: str, | ||||||
well_known_uri: str, | ||||||
confirmation_message: str = "", | ||||||
): | ||||||
super().__init__( | ||||||
audience, client_id, client_secret, well_known_uri, confirmation_message | ||||||
) | ||||||
self._oidc_bearer = OAuth2AuthorizationCodeBearer( | ||||||
authorizationUrl=self.authorization_endpoint, tokenUrl=self.token_endpoint | ||||||
) | ||||||
|
||||||
@property | ||||||
def oauth2_scheme(self) -> Callable[[Request], str]: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return self._oidc_bearer | ||||||
|
||||||
async def authenticate(self, request: Request) -> Optional[UserSessionState]: | ||||||
access_token = self._oidc_bearer(request) | ||||||
try: | ||||||
verified_body = await self.decode_token(access_token) | ||||||
return UserSessionState(verified_body["sub"], {}) | ||||||
|
||||||
except JWTError: | ||||||
logger.exception( | ||||||
"Authentication error. Unverified token: %r", | ||||||
jwt.get_unverified_claims(id_token), | ||||||
jwt.get_unverified_claims(access_token), | ||||||
) | ||||||
return None | ||||||
return UserSessionState(verified_body["sub"], {}) | ||||||
|
||||||
|
||||||
async def exchange_code( | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some type hinting for my benefit