diff --git a/src/auth0/authentication/pushed_authorization_requests.py b/src/auth0/authentication/pushed_authorization_requests.py index 12c4fc97..b76f596f 100644 --- a/src/auth0/authentication/pushed_authorization_requests.py +++ b/src/auth0/authentication/pushed_authorization_requests.py @@ -1,3 +1,4 @@ +import json from typing import Any from .base import AuthenticationBase @@ -22,6 +23,11 @@ def pushed_authorization_request( See: https://www.rfc-editor.org/rfc/rfc9126.html """ + if isinstance(kwargs.get("authorization_details"), list): + kwargs["authorization_details"] = json.dumps( + kwargs["authorization_details"] + ) + return self.authenticated_post( f"{self.protocol}://{self.domain}/oauth/par", data={ diff --git a/tests/authentication/test_pushed_authorization_requests.py b/tests/authentication/test_pushed_authorization_requests.py index e062dabf..9dbc6230 100644 --- a/tests/authentication/test_pushed_authorization_requests.py +++ b/tests/authentication/test_pushed_authorization_requests.py @@ -1,6 +1,7 @@ import unittest import json from unittest import mock +from urllib.parse import parse_qs from auth0.authentication.pushed_authorization_requests import PushedAuthorizationRequests @@ -63,7 +64,7 @@ def test_with_authorization_details(self, mock_post): "client_secret": "sh!", "response_type": "code", "redirect_uri": "https://example.com/callback", - "authorization_details": [{"type": "money_transfer", "instructedAmount": {"amount": 2500, "currency": "USD"}}], + "authorization_details": json.dumps([{"type": "money_transfer", "instructedAmount": {"amount": 2500, "currency": "USD"}}]), } actual_data = kwargs["data"] @@ -75,6 +76,26 @@ def test_with_authorization_details(self, mock_post): json.dumps(expected_data, sort_keys=True) ) + @mock.patch("requests.request") + def test_authorization_details_sent_as_json(self, mock_requests_request): + response = mock.MagicMock() + response.status_code = 201 + response.text = '{"request_uri": "urn:example", "expires_in": 30}' + mock_requests_request.return_value = response + + authorization_details = [{"type": "money_transfer", "instructedAmount": {"amount": 2500, "currency": "USD"}}] + a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!") + a.pushed_authorization_request( + response_type="code", + redirect_uri="https://example.com/callback", + authorization_details=authorization_details, + ) + + args, kwargs = mock_requests_request.call_args + sent = parse_qs(kwargs["data"]) + + self.assertEqual(json.loads(sent["authorization_details"][0]), authorization_details) + @mock.patch("auth0.authentication.rest.RestClient.post") def test_jar(self, mock_post): a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!")