Skip to content
Snippets Groups Projects

MFE-6 Use Redis to cache SAP responses

Merged Trond Aasan requested to merge MFE-6-caching into master
2 unresolved threads
Files
10
@@ -2,7 +2,7 @@ import json
import logging
from datetime import datetime, timedelta
from http import HTTPStatus
from typing import Callable, Optional, List, Iterable
from typing import Callable, Optional, List, Iterable, Tuple
from urllib.parse import urlencode
import requests
@@ -13,6 +13,7 @@ from jose.jwt import get_unverified_claims
from oauthlib.oauth2 import WebApplicationClient
from pydantic import BaseModel, Field
from starlette.authentication import AuthenticationBackend, BaseUser, AuthCredentials
from starlette.requests import HTTPConnection
from starlette.responses import RedirectResponse
from mfe_ms.config import MFEConfig
@@ -87,8 +88,10 @@ def routes(config: MFEConfig, app: FastAPI, home: Callable):
def display_name(self) -> str:
return self.name
async def authenticate(self, request: Request):
token = request.session.get("token")
async def authenticate(
self, conn: HTTPConnection
) -> Optional[Tuple[AuthCredentials, BaseUser]]:
token = conn.session.get("token")
if not token:
return
try:
@@ -104,12 +107,14 @@ def routes(config: MFEConfig, app: FastAPI, home: Callable):
except Exception as e:
logger.debug(e)
del request.session["token"]
del conn.session["token"]
auth_backend = CurrentUserAuthBackend()
def get_relevant_groups(access_token: str) -> List[str]:
allowed = config.auth.allowed_groups
if not allowed:
return []
res = requests.get(
config.auth.dataporten.groups_endpoint,
headers={"Authorization": f"Bearer {access_token}"},
@@ -124,7 +129,7 @@ def routes(config: MFEConfig, app: FastAPI, home: Callable):
return True
return bool(groups)
def is_allowed_by_id(ids: Iterable[id]) -> bool:
def is_allowed_by_id(ids: Iterable[str]) -> bool:
allowed = config.auth.allowed_users
if allowed is None:
logger.info("`allowed_users` is None, accepting all")
Loading