Skip to content

Commit 69e5346

Browse files
committed
✨(backend) Fix access token storage issues
When indexer service is not configured, the search view should work event with a disabled OIDC_STORE_ACCESS_TOKEN. Disable token storage for the unit tests. Add bin/fernetkey that generates a key for the OIDC_STORE_REFRESH_TOKEN_KEY setting. Signed-off-by: Fabre Florian <[email protected]>
1 parent e72f76b commit 69e5346

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

bin/fernetkey

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck source=bin/_config.sh
4+
source "$(dirname "${BASH_SOURCE[0]}")/_config.sh"
5+
6+
_dc_run app-dev python -c 'from cryptography.fernet import Fernet;import sys; sys.stdout.write("\n" + Fernet.generate_key().decode() + "\n");'

env.d/development/common

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ SEARCH_INDEXER_URL="http://find:8000/api/v1.0/documents/index/"
8181
SEARCH_INDEXER_QUERY_URL="http://find:8000/api/v1.0/documents/search/"
8282

8383
# Store OIDC tokens in the session
84-
OIDC_STORE_ACCESS_TOKEN = True
85-
OIDC_STORE_REFRESH_TOKEN = True # Store the encrypted refresh token in the session.
84+
# OIDC_STORE_ACCESS_TOKEN = True
85+
# OIDC_STORE_REFRESH_TOKEN = True # Store the encrypted refresh token in the session.
86+
# Must be a valid Fernet key (32 url-safe base64-encoded bytes)
87+
# To create one, use the bin/fernetkey command.
88+
# OIDC_STORE_REFRESH_TOKEN_KEY="your-32-byte-encryption-key=="

src/backend/core/api/viewsets.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,8 @@ def breadcrumb(self, request, *args, **kwargs):
10121012
return drf.response.Response(serializer.data, status=drf.status.HTTP_200_OK)
10131013

10141014
# pylint: disable-next=too-many-arguments,too-many-positional-arguments
1015-
def _fulltext_search(self, queryset, indexer, request, text):
1015+
@method_decorator(refresh_oidc_access_token)
1016+
def _indexed_search(self, request, queryset, indexer, text):
10161017
"""
10171018
Returns a queryset from the results the fulltext search of Find
10181019
"""
@@ -1021,16 +1022,19 @@ def _fulltext_search(self, queryset, indexer, request, text):
10211022

10221023
# Retrieve the documents ids from Find. No pagination here the queryset is
10231024
# already filtered
1024-
results = indexer.search(
1025-
text=text, token=token, visited=get_visited_items_ids_of(queryset, user)
1026-
)
1025+
result_ids = [
1026+
r["_id"]
1027+
for r in indexer.search(
1028+
text=text, token=token, visited=get_visited_items_ids_of(queryset, user)
1029+
)
1030+
]
10271031

1028-
queryset = queryset.filter(pk__in=results)
1032+
queryset = queryset.filter(pk__in=result_ids)
10291033
queryset = self.annotate_user_roles(queryset)
10301034
queryset = self.annotate_is_favorite(queryset)
10311035

10321036
files_by_uuid = {str(d.pk): d for d in queryset}
1033-
ordered_files = [files_by_uuid[id] for id in results if id in files_by_uuid]
1037+
ordered_files = [files_by_uuid[id] for id in result_ids if id in files_by_uuid]
10341038

10351039
page = self.paginate_queryset(ordered_files)
10361040

@@ -1050,7 +1054,6 @@ def _fulltext_search(self, queryset, indexer, request, text):
10501054
url_path="search",
10511055
pagination_class=drf.pagination.PageNumberPagination,
10521056
)
1053-
@method_decorator(refresh_oidc_access_token)
10541057
def search(self, request, *args, **kwargs):
10551058
"""
10561059
Returns a DRF response containing the filtered, annotated and ordered items.
@@ -1104,10 +1107,10 @@ def search(self, request, *args, **kwargs):
11041107
if indexer:
11051108
# When the indexer is configured pop "title" from queryset search and use
11061109
# fulltext results instead.
1107-
return self._fulltext_search(
1110+
return self._indexed_search(
1111+
request,
11081112
queryset,
11091113
indexer,
1110-
request,
11111114
text=filterset.form.cleaned_data.pop("title"),
11121115
)
11131116

src/backend/core/services/search_indexers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def search(self, text, token, visited=(), nb_results=None):
270270
token=token,
271271
)
272272

273-
return [d["_id"] for d in response]
273+
return response
274274

275275
@abstractmethod
276276
def search_query(self, data, token) -> dict:

src/backend/core/tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pytest
99
import responses
10+
from cryptography.fernet import Fernet
1011

1112
from core import factories
1213
from core.tests.utils.urls import reload_urls
@@ -143,6 +144,10 @@ def indexer_settings_fixture(settings):
143144
settings.SEARCH_INDEXER_ALLOWED_MIMETYPES = ("text/",)
144145
settings.SEARCH_INDEXER_COUNTDOWN = 1
145146

147+
settings.OIDC_STORE_ACCESS_TOKEN = True
148+
settings.OIDC_STORE_REFRESH_TOKEN = True
149+
settings.OIDC_STORE_REFRESH_TOKEN_KEY = Fernet.generate_key().decode()
150+
146151
yield settings
147152

148153
# clear cache to prevent issues with other tests

src/backend/drive/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,8 @@ class Test(Base):
995995
CELERY_TASK_ALWAYS_EAGER = values.BooleanValue(True)
996996

997997
SEARCH_INDEXER_CLASS = None
998+
OIDC_STORE_ACCESS_TOKEN = False
999+
OIDC_STORE_REFRESH_TOKEN = False
9981000

9991001
def __init__(self):
10001002
# pylint: disable=invalid-name

0 commit comments

Comments
 (0)