Skip to content

✨(back) integrate resource server API #195

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

lunika
Copy link
Member

@lunika lunika commented May 20, 2025

Purpose

Expose endpoints for external api. This endpoints will allow to work on the item and itemAccess resources.

The external apis are available with these urls:

/external_api/v1.0/	rest_framework.routers.APIRootView	api-root
/external_api/v1.0/<drf_format_suffix:format>	rest_framework.routers.APIRootView	api-root
/external_api/v1.0/items/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-list
/external_api/v1.0/items/<pk>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-detail
/external_api/v1.0/items/<pk>/children/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-children
/external_api/v1.0/items/<pk>/children\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-children
/external_api/v1.0/items/<pk>/favorite/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-favorite
/external_api/v1.0/items/<pk>/favorite\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-favorite
/external_api/v1.0/items/<pk>/hard-delete/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-hard-delete
/external_api/v1.0/items/<pk>/hard-delete\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-hard-delete
/external_api/v1.0/items/<pk>/link-configuration/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-link-configuration
/external_api/v1.0/items/<pk>/link-configuration\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-link-configuration
/external_api/v1.0/items/<pk>/move/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-move
/external_api/v1.0/items/<pk>/move\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-move
/external_api/v1.0/items/<pk>/restore/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-restore
/external_api/v1.0/items/<pk>/restore\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-restore
/external_api/v1.0/items/<pk>/tree/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-tree
/external_api/v1.0/items/<pk>/tree\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-tree
/external_api/v1.0/items/<pk>/upload-ended/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-upload-ended
/external_api/v1.0/items/<pk>/upload-ended\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-upload-ended
/external_api/v1.0/items/<pk>\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-detail
/external_api/v1.0/items/<resource_id>/	rest_framework.routers.APIRootView	api-root
/external_api/v1.0/items/<resource_id>/<drf_format_suffix:format>	rest_framework.routers.APIRootView	api-root
/external_api/v1.0/items/<resource_id>/accesses/	core.external_api.viewsets.ResourceServerItemAccessViewSet	resource_server_item_accesses-list
/external_api/v1.0/items/<resource_id>/accesses/<pk>/	core.external_api.viewsets.ResourceServerItemAccessViewSet	resource_server_item_accesses-detail
/external_api/v1.0/items/<resource_id>/accesses/<pk>\.<format>/	core.external_api.viewsets.ResourceServerItemAccessViewSet	resource_server_item_accesses-detail
/external_api/v1.0/items/<resource_id>/accesses\.<format>/	core.external_api.viewsets.ResourceServerItemAccessViewSet	resource_server_item_accesses-list
/external_api/v1.0/items/favorite_list/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-favorite-list
/external_api/v1.0/items/favorite_list\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-favorite-list
/external_api/v1.0/items/media-auth/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-media-auth
/external_api/v1.0/items/media-auth\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-media-auth
/external_api/v1.0/items/trashbin/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-trashbin
/external_api/v1.0/items/trashbin\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-trashbin
/external_api/v1.0/items\.<format>/	core.external_api.viewsets.ResourceServerItemViewSet	resource_server_items-list

To authenticate to this API, the request must have a Bearer token provided by your OIDC provider when the user is connected. The authentication will introspect the given token by fetching an introspection endpoint provided by your OIDC provider.

To configure your instance, you can follow this documentation: https://github.com/suitenumerique/django-lasuite/blob/main/documentation/how-to-use-oidc-resource-server-backend.md

@lunika lunika marked this pull request as draft May 20, 2025 18:46
@lunika lunika force-pushed the feat/resource-server branch 3 times, most recently from 2a19602 to 238a83c Compare May 23, 2025 13:53
@lunika lunika self-assigned this May 23, 2025
@lunika lunika marked this pull request as ready for review May 23, 2025 13:54
@lunika lunika changed the title 🚧(back) integrate resource server API ✨(back) integrate resource server API May 26, 2025
@lunika lunika force-pushed the feat/resource-server branch from 0758b5f to 75cf0c7 Compare May 26, 2025 06:53
@sylvinus sylvinus force-pushed the feat/resource-server branch from 33ca843 to 32dd759 Compare June 3, 2025 09:52
@@ -37,5 +40,9 @@ def has_permission(self, request, view):

# When used as a resource server, the request has a token audience
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs an update and so the docstring (class & method)

Comment on lines 37 to 50
try:
payload = jwt.decode(
token, settings.JWT_SECRET_KEY, algorithms=[settings.JWT_ALGORITHM]
)
except jwt.InvalidTokenError as e:
logger.error("Invalid JWT token: %s", e)
return None

if not payload.get("sub") or not payload.get("email"):
logger.warning("Invalid JWT token. Missing 'sub' or 'email' in payload")
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be carreful here, because the OIDC authentication can also use the "Bearer" format, and provide a JWT (the OIDC access token can be a JWT), so you will log error and warning even for valid OIDC authentication.

logger = logging.getLogger(__name__)


class JWTAuthentication(authentication.BaseAuthentication):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may add a authenticate_header method to return a 401 instead of a 403 when no authentication is provided.

lunika and others added 14 commits June 19, 2025 09:48
We expose now an external API. This external API can be used using a
resource server authentication. The urls are prefixed with
/external_api/v1.0 and for now the viewsets available are the Item and
ItemAccess.
The mozilla OIDCAuthentication backend was configured in the DRF
settings but not used. We have to remove it.
The external_api prefix must be managed by an ingress to use the correct
backend, the impress-backend one.
We need to publish a new version to add external-api route in the
ingress
We want to allow temporarily the connection of the external API using a
JWT token with a simmetric key. This simmetric key must be used with the
system allowed to request drive.
@sylvaingi sylvaingi force-pushed the feat/resource-server branch 2 times, most recently from a820d78 to 24a7a15 Compare June 20, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants