Skip to content

Commit

Permalink
Option to save oauth redirect parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
GDay committed Mar 29, 2024
1 parent a16c417 commit ae30685
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
32 changes: 32 additions & 0 deletions back/admin/integrations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions back/admin/integrations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
12 changes: 12 additions & 0 deletions docs/integrations/oauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.<your value> }}
```


## Example
```json
Expand Down

0 comments on commit ae30685

Please sign in to comment.