From ae306855085ec4c7983b189a5e0d489162beaf13 Mon Sep 17 00:00:00 2001 From: GDay <1939656+GDay@users.noreply.github.com> Date: Fri, 29 Mar 2024 03:57:38 +0100 Subject: [PATCH] Option to save oauth redirect parameters --- back/admin/integrations/tests.py | 32 ++++++++++++++++++++++++++++++++ back/admin/integrations/views.py | 3 +++ docs/integrations/oauth.md | 12 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/back/admin/integrations/tests.py b/back/admin/integrations/tests.py index 96628b3f3..5a16b7098 100644 --- a/back/admin/integrations/tests.py +++ b/back/admin/integrations/tests.py @@ -718,6 +718,38 @@ def test_integration_oauth_callback_view( assert integration.extra_args["oauth"] == {"access_token": "test"} +@pytest.mark.django_db +@patch( + "admin.integrations.models.Integration.run_request", + Mock(return_value=(True, Mock(json=lambda: {"access_token": "test"}))), +) +def test_integration_oauth_callback_view_with_save_get_params( + client, django_user_model, custom_integration_factory +): + client.force_login( + django_user_model.objects.create(role=get_user_model().Role.ADMIN) + ) + integration = custom_integration_factory( + manifest={ + "oauth": { + "access_token": {"url": "http://localhost:8000/test/"}, + "authenticate_url": "http://localhost:8000/test/", + "store_redirect_parameters": True, + } + } + ) + + url = reverse("integrations:oauth-callback", args=[integration.id]) + client.get(url + "?code=test&somethingelse=blank", follow=True) + + integration.refresh_from_db() + assert integration.enabled_oauth + assert integration.extra_args["oauth"] == { + "access_token": "test", + "redirect_params": {"code": "test", "somethingelse": "blank"}, + } + + @pytest.mark.django_db def test_integration_clean_error_data(custom_integration_factory): integration = custom_integration_factory( diff --git a/back/admin/integrations/views.py b/back/admin/integrations/views.py index 75b9c1db1..2db054960 100644 --- a/back/admin/integrations/views.py +++ b/back/admin/integrations/views.py @@ -192,6 +192,9 @@ def get_redirect_url(self, pk, *args, **kwargs): return reverse_lazy("settings:integrations") integration.extra_args["oauth"] = response.json() + if integration.manifest["oauth"].get("store_redirect_parameters", False): + integration.extra_args["oauth"]["redirect_params"] = self.request.GET + if "expires_in" in response.json(): integration.expiring = timezone.now() + timedelta( seconds=response.json()["expires_in"] diff --git a/docs/integrations/oauth.md b/docs/integrations/oauth.md index ce7c783ef..8e1ecc0c6 100644 --- a/docs/integrations/oauth.md +++ b/docs/integrations/oauth.md @@ -17,6 +17,12 @@ Used to refresh the token to get a new one, if this is not added, then it will a Default: `False`. Enable this is a valid callback won't return a `code` query in the url. In some cases, we don't get it and also not need it. +`store_redirect_parameters` + +Default: `False`. Enable this if you want to store whatever the server returns when it redirects you back to the ChiefOnboaring instance. + + + ## Redirect url Most oauth providers will require you to put the redirect url in one of the links. Every integrations has a unique redirect url and you can get the correct one by using: @@ -32,6 +38,12 @@ When done, you should have gotten credentials to authenticate other urls. If you {{ oauth.refresh_token }} ``` +If you enabled the `store_redirect_parameters` value, then you can get the parameters by doing: + +``` +{{ oauth.redirect_params. }} +``` + ## Example ```json