Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INCLUSION CONNECT] adapter l'authentification à la nouvelle version d'IC #447

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SIB_API_KEY=__key_to_be_set__
# for Inclusion Connect
INCLUSION_CONNECT_BASE_URL=http://127.0.0.1:8080
INCLUSION_CONNECT_CLIENT_ID=local_inclusion_connect
INCLUSION_CONNECT_CLIENT_SECRET=__key_to_be_set__
INCLUSION_CONNECT_CLIENT_SECRET=password

# Path to the itou-backup project repository.
PATH_TO_BACKUPS=~/path/to/backups
Expand Down
20 changes: 7 additions & 13 deletions lacommunaute/inclusion_connect/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,15 @@ def test_normal_signin(self):

class InclusionConnectLogoutTest(InclusionConnectBaseTestCase):
@respx.mock
def test_simple_logout(self):
def test_logout_with_redirection(self):
mock_oauth_dance(self)
params = {
"id_token_hint": 123456,
"post_logout_redirect_uri": f'http://testserver{reverse("pages:home")}',
}
expected_redirection = f"{constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT}?{urlencode(params)}"
respx.get(constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT).respond(200)
logout_url = reverse("inclusion_connect:logout")
response = self.client.get(logout_url)
self.assertRedirects(response, reverse("pages:home"))
self.assertFalse(auth.get_user(self.client).is_authenticated)

@respx.mock
def test_logout_with_redirection(self):
mock_oauth_dance(self)
expected_redirection = reverse("pages:home")
respx.get(constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT).respond(200)

params = {"redirect_url": expected_redirection}
logout_url = f"{reverse('inclusion_connect:logout')}?{urlencode(params)}"
response = self.client.get(logout_url)
self.assertRedirects(response, expected_redirection)
self.assertRedirects(response, expected_redirection, fetch_redirect_response=False)
14 changes: 3 additions & 11 deletions lacommunaute/inclusion_connect/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,21 @@ def inclusion_connect_callback(request): # pylint: disable=too-many-return-stat

def inclusion_connect_logout(request):
token = request.GET.get("token")
state = request.GET.get("state")
post_logout_redirect_url = request.GET.get("redirect_url", reverse("pages:home"))
post_logout_redirect_uri = request.GET.get("redirect_url", reverse("pages:home"))

# Fallback on session data.
if not token:
ic_session = request.session.get(constants.INCLUSION_CONNECT_SESSION_KEY)
if not ic_session:
raise KeyError("Missing session key.")
token = ic_session["token"]
state = ic_session["state"]

params = {
"id_token_hint": token,
"state": state,
"post_logout_redirect_uri": request.build_absolute_uri(post_logout_redirect_uri),
}
complete_url = f"{constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT}?{urlencode(params)}"
# Logout user from IC with HTTPX to benefit from respx in tests
# and to handle post logout redirection more easily.
response = httpx.get(complete_url)
if response.status_code != 200:
logger.error("Error during IC logout. Status code: %s", response.status_code)

# Logout user from Django
logout(request)

return HttpResponseRedirect(post_logout_redirect_url)
return HttpResponseRedirect(complete_url)