Skip to content

Commit cdc791d

Browse files
committed
use oauth lib for oidc
let the lib do the work instead of building requests and urls uses the well-known endpoint fixes the urls only working on fief
1 parent c06daf5 commit cdc791d

2 files changed

Lines changed: 68 additions & 400 deletions

File tree

carbonserver/carbonserver/api/services/auth_providers/oidc_auth_provider.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
from typing import Any, Dict, Optional, Tuple
99

1010
from authlib.integrations.starlette_client import OAuth
11+
from authlib.jose import JsonWebKey
12+
from authlib.jose import jwt as jose_jwt
13+
from fief_client import FiefAsync
1114

1215
from carbonserver.config import settings
1316

1417
DEFAULT_SIGNATURE_CACHE_TTL = 3600 # seconds
1518
OAUTH_SCOPES = ["openid", "email", "profile"]
1619

20+
fief = FiefAsync(
21+
settings.fief_url, settings.fief_client_id, settings.fief_client_secret
22+
)
1723

1824
oauth = OAuth()
1925
oauth.register(
@@ -44,3 +50,20 @@ async def get_authorize_url(self, request, login_url):
4450

4551
def get_client_credentials(self) -> Tuple[str, str]:
4652
return (self.client.client_id, self.client.client_secret)
53+
54+
async def _decode_token(self, token: str) -> Dict[str, Any]:
55+
try:
56+
access_token_info = await fief.validate_access_token(token)
57+
return access_token_info
58+
except Exception:
59+
...
60+
61+
jwks_data = await self.client.fetch_jwk_set()
62+
keyset = JsonWebKey.import_key_set(jwks_data)
63+
claims = jose_jwt.decode(token, keyset)
64+
claims.validate()
65+
return dict(claims)
66+
67+
async def validate_access_token(self, token: str) -> bool:
68+
await self._decode_token(token)
69+
return True

0 commit comments

Comments
 (0)