Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Publish changelog for ingestion_server-2024.05.20.19.47.22 (#4364) (#4334)
Princewill Onyenanu896ca2441d28
diff --git a/api/api/migrations/0062_throttledapplication_revoked.py b/api/api/migrations/0062_throttledapplication_revoked.pynew file mode 100644index 000000000..0c80e7979--- /dev/null+++ b/api/api/migrations/0062_throttledapplication_revoked.py@@ -0,0 +1,18 @@+# Generated by Django 4.2.11 on 2024-05-16 10:00++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('api', '0061_convert_varchar_to_text'),+ ]++ operations = [+ migrations.AddField(+ model_name='throttledapplication',+ name='revoked',+ field=models.BooleanField(default=False),+ ),+ ]diff --git a/api/api/migrations/0063_merge_20240521_0843.py b/api/api/migrations/0063_merge_20240521_0843.pynew file mode 100644index 000000000..9eb7c3aa5--- /dev/null+++ b/api/api/migrations/0063_merge_20240521_0843.py@@ -0,0 +1,14 @@+# Generated by Django 4.2.11 on 2024-05-21 08:43++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ('api', '0062_decision_through_tables'),+ ('api', '0062_throttledapplication_revoked'),+ ]++ operations = [+ ]diff --git a/api/api/models/oauth.py b/api/api/models/oauth.pyindex 50e6fb920..4154f18f5 100644--- a/api/api/models/oauth.py+++ b/api/api/models/oauth.py@@ -38,6 +38,7 @@ class ThrottledApplication(AbstractApplication):max_length=20, choices=RATE_LIMIT_MODELS, default="standard")verified = models.BooleanField(default=False)+ revoked = models.BooleanField(default=False)class OAuth2Verification(models.Model):diff --git a/api/conf/oauth2_extensions.py b/api/conf/oauth2_extensions.pyindex 2ece1b497..5bc12a784 100644--- a/api/conf/oauth2_extensions.py+++ b/api/conf/oauth2_extensions.py@@ -1,4 +1,7 @@-from rest_framework.exceptions import AuthenticationFailed+from rest_framework.exceptions import (+ AuthenticationFailed,+ PermissionDenied,+)from drf_spectacular.authentication import TokenSchemefrom oauth2_provider.contrib.rest_framework import (@@ -11,7 +14,7 @@ class OAuth2Authentication(BaseOAuth2Authentication):keyword = "Bearer"def authenticate(self, request):- result = super().authenticate(request)+ user_auth_tuple = super().authenticate(request)if getattr(request, "oauth2_error", None):# oauth2_error is only defined on requests that had errors# it will be undefined or empty for anonymous requests and@@ -19,7 +22,16 @@ class OAuth2Authentication(BaseOAuth2Authentication):# `request` is mutated by `super().authenticate`raise AuthenticationFailed()- return result+ # if this is an authed request, check and+ # deny access if client's access has been+ # revoked.+ if user_auth_tuple is not None:+ user, auth = user_auth_tuple+ if application := getattr(auth, "application", None):+ if application.revoked:+ raise PermissionDenied()++ return user_auth_tupleclass OAuth2OpenApiAuthenticationExtension(TokenScheme):diff --git a/api/latest_migrations/api b/api/latest_migrations/apiindex 426a19e09..b1c2f6346 100644--- a/api/latest_migrations/api+++ b/api/latest_migrations/api@@ -2,4 +2,4 @@# If you have a merge conflict in this file, it means you need to run:# manage.py makemigrations --merge# in order to resolve the conflict between migrations.-0062_decision_through_tables+0063_merge_20240521_0843diff --git a/api/test/integration/test_auth.py b/api/test/integration/test_auth.pyindex f462368ac..e5dbe2f0b 100644--- a/api/test/integration/test_auth.py+++ b/api/test/integration/test_auth.py@@ -267,6 +267,7 @@ def test_page_size_limit_authed(api_client, test_auth_token_exchange):res = api_client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}")+assert res.status_code == 200query_params = {"page_size": 500}@@ -282,3 +283,22 @@ def test_invalid_credentials_401(api_client):"/v1/images/", HTTP_AUTHORIZATION="Bearer thisIsNot_ARealToken")assert res.status_code == 401+++def test_revoked_application_access(api_client, test_auth_token_exchange):+ token = test_auth_token_exchange["access_token"]+ application = AccessToken.objects.get(token=token).application+ application.save()++ res = api_client.get("/v1/images/", HTTP_AUTHORIZATION=f"Bearer {token}")++ assert res.status_code == 200++ # Now revoke access+ application.revoked = True+ application.save()++ res = api_client.get("/v1/images/", HTTP_AUTHORIZATION=f"Bearer {token}")++ assert res.status_code == 403