Skip to content
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

feat(context_url): add context url to caller #187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Awaitable, Callable, TypeVar, Union
from urllib.parse import unquote

from forestadmin.agent_toolkit.resources.collections.base_collection_resource import BaseCollectionResource
from forestadmin.agent_toolkit.resources.collections.filter import parse_timezone
Expand Down Expand Up @@ -42,6 +43,10 @@ async def _authenticate(
except JWTError:
return Response(status=401)

context_url = None
if "Forest-Context-Url" in request.headers:
context_url = unquote(request.headers["Forest-Context-Url"])

request.user = User(
rendering_id=int(user["rendering_id"]),
user_id=int(user["id"]),
Expand All @@ -51,6 +56,7 @@ async def _authenticate(
last_name=user["last_name"],
team=user["team"],
timezone=parse_timezone(request),
context_url=context_url,
)
return await decorated_fn(self, request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class User:
last_name: str
team: str
timezone: ZoneInfo
context_url: Optional[str] = None
# permission_level
# role

Expand Down
33 changes: 33 additions & 0 deletions src/agent_toolkit/tests/resources/collections/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,39 @@ async def _decorated_fn(resource, request):

self.assertEqual(response, True)

def test_should_parse_forest_context_url_if_present(self):
user = {
"rendering_id": "1",
"id": "1",
"tags": {"test": "tag"},
"email": "[email protected]",
"first_name": "first_name",
"last_name": "last_name",
"team": "best_team",
}
encoded_user = jwt.encode(user, "auth_secret")
request = RequestCollection(
RequestMethod.GET,
self.book_collection,
body=None,
query={"timezone": "Europe/Paris"},
headers={
"Authorization": f"Bearer {encoded_user}",
"Forest-Context-Url": "http://localhost/?param%3D%2Ftest%2F",
},
)

async def _decorated_fn(resource, request):
self.assertEqual(request.user.context_url, "http://localhost/?param=/test/")

return True

decorated_fn = AsyncMock(wraps=_decorated_fn)
response = self.loop.run_until_complete(_authenticate(self.collection_resource, request, decorated_fn))
decorated_fn.assert_awaited_once_with(self.collection_resource, request)

self.assertEqual(response, True)


class TestAuthorizeDecorators(TestDecorators):
@classmethod
Expand Down
12 changes: 12 additions & 0 deletions src/django_agent/forestadmin/django_agent/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
from typing import Callable, Optional, Union

from corsheaders import defaults as default_cors_settings
from django.apps import AppConfig, apps
from django.conf import settings
from forestadmin.agent_toolkit.forest_logger import ForestLogger
Expand Down Expand Up @@ -77,6 +78,7 @@ def get_agent(cls) -> DjangoAgent:
def ready(self):
# we need to wait for other apps to be ready, for this forest app must be ready
# that's why we need another thread waiting for every app to be ready
self.setup_cors_settings()
t = threading.Thread(name="forest.wait_and_launch_agent", target=self._wait_for_all_apps_ready_and_launch_agent)
t.start()

Expand All @@ -99,3 +101,13 @@ def _wait_for_all_apps_ready_and_launch_agent(self):
)

DjangoAgentApp._DJANGO_AGENT = init_app_agent()

def setup_cors_settings(self):
# headers
if getattr(settings, "CORS_ALLOW_HEADERS", None):
allowed_headers = settings.CORS_ALLOW_HEADERS
else:
allowed_headers = default_cors_settings.default_headers

if "Forest-Context-Url" not in allowed_headers:
settings.CORS_ALLOW_HEADERS = (*allowed_headers, "Forest-Context-Url")
Loading