-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: remove oidc group api fetching
- Loading branch information
Showing
19 changed files
with
179 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Migration from v1 to v2 | ||
**Warning** | ||
|
||
The `group` attribute will be removed from the Template model. | ||
A suggested migration would be to move the value to `meta` before migrating. | ||
|
||
---- | ||
|
||
The previous pre-defined permission and visibility system was removed in favour of [dgap](https://github.com/adfinis/django-generic-api-permissions). | ||
|
||
The integration of `OIDC_GROUPS_API` and `OIDC_GROUPS_API_JSONPATH` was removed with it. | ||
Because every consuming app can now define its own way to handle the permissions. | ||
|
||
Example Permissions: | ||
```py | ||
import requests | ||
from rest_framework import exceptions | ||
from generic_permissions.permissions import object_permission_for | ||
|
||
from document_merge_service.models import Template | ||
|
||
|
||
class CustomPermission: | ||
""" | ||
Current settings and how to refactor them | ||
OIDC_GROUPS_API = "https://example.com/users/{sub}/group" | ||
OIDC_GROUPS_API_JSONPATH = "$$.included[?(@.type=='services')].id" | ||
""" | ||
@object_permission_for(Template) | ||
def has_object_permission_template(self, request, instance): | ||
uri = "https://example.com/users/{sub}/group" | ||
# replace headers, extract header | ||
token = request.headers["AUTHORIZATION"] | ||
|
||
# replace existing placeholders | ||
groups_api = uri.replace("{sub}", request.user.username) | ||
|
||
response = requests.get( | ||
groups_api, verify=True, headers={"authorization": token} | ||
) | ||
try: | ||
response.raise_for_status() | ||
except requests.HTTPError as e: | ||
raise exceptions.AuthenticationFailed( | ||
f"Retrieving groups from {uri} " | ||
f"failed with error '{str(e)}'." | ||
) | ||
|
||
result = response.json() | ||
|
||
# previously OIDC_GROUPS_API_JSONPATH was used here to extract the group from the response | ||
for data in result["included"]: | ||
if data.type == "services" | ||
groups = data.id | ||
|
||
return instance.meta["group"] in groups | ||
``` | ||
|
||
After creating the permission define it in `settings.py` for dgap. | ||
```py | ||
GENERIC_PERMISSIONS_PERMISSION_CLASSES = ['app.permissions.CustomPermission'] | ||
``` | ||
|
||
Example Visibility: | ||
```py | ||
from django.db.models import Q | ||
from generic_permissions.visibilities import filter_queryset_for | ||
|
||
from document_merge_service.models import Template | ||
|
||
|
||
class CustomVisibility: | ||
"""Example Visibility class to replicate previous behaviour.""" | ||
|
||
@filter_queryset_for(Template) | ||
def filter_templates(self, queryset, request): | ||
queryset = queryset.filter( | ||
Q(meta__group__in=self.request.user.groups or []) | Q(meta__group__isnull=True) | ||
) | ||
|
||
return queryset | ||
``` | ||
|
||
After creating the visibility define it in `settings.py` for dgap. | ||
```py | ||
GENERIC_PERMISSIONS_VISIBILITY_CLASSES = ['app.visibilites.CustomVisibility'] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
document_merge_service/api/migrations/0006_remove_template_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 3.2.16 on 2022-12-23 12:54 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0005_xlsx_template_engine"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="template", | ||
name="group", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,9 @@ | ||
import inspect | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from .collections import list_duplicates | ||
from .models import Template | ||
|
||
class AllowAny(BasePermission): | ||
pass | ||
|
||
|
||
class IsAuthenticated(BasePermission): | ||
""" | ||
Allow access only to authenticated users. | ||
You can either use this in combination with your own permission | ||
classes, or inherit from it if you want *some* models to be accessible | ||
publicly. | ||
""" | ||
from rest_framework import permissions | ||
|
||
@permission_for(Template) | ||
def base_permission(self, request): | ||
return request.user.is_authenticated | ||
|
||
@object_permission_for(Template) | ||
def base_object_permission(self, request, instance): | ||
return self.base_permission(request) | ||
|
||
|
||
class AsConfigured(IsAuthenticated): | ||
@permission_for(Template) | ||
def base_permission(self, request): | ||
if settings.REQUIRE_AUTHENTICATION: | ||
return super().base_permission(request) | ||
return True | ||
|
||
@object_permission_for(Template) | ||
def base_object_permission(self, request, instance): | ||
class AsConfigured(permissions.IsAuthenticated): | ||
def has_permission(self, request, view): | ||
if settings.REQUIRE_AUTHENTICATION: | ||
return super().base_object_permission(request, instance) | ||
return super().has_permission(request, view) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.