From bfbab6357a208be1390799b41036cbf3baf2a4ac Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:58:38 +0200 Subject: [PATCH] sources/oauth: fix link not being saved (cherry-pick #10374) (#10376) sources/oauth: fix link not being saved (#10374) Signed-off-by: Jens Langhammer Co-authored-by: Jens L --- authentik/core/sources/flow_manager.py | 2 +- tests/e2e/test_source_oauth_oauth2.py | 39 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/authentik/core/sources/flow_manager.py b/authentik/core/sources/flow_manager.py index ce55611d1872..d30802257a9c 100644 --- a/authentik/core/sources/flow_manager.py +++ b/authentik/core/sources/flow_manager.py @@ -309,7 +309,7 @@ def handle_existing_link( # When request isn't authenticated we jump straight to auth if not self.request.user.is_authenticated: return self.handle_auth(connection) - # Connection has already been saved + connection.save() Event.new( EventAction.SOURCE_LINKED, message="Linked Source", diff --git a/tests/e2e/test_source_oauth_oauth2.py b/tests/e2e/test_source_oauth_oauth2.py index 5986de7c4799..e6eea24c6be0 100644 --- a/tests/e2e/test_source_oauth_oauth2.py +++ b/tests/e2e/test_source_oauth_oauth2.py @@ -1,5 +1,6 @@ """test OAuth Source""" +from json import loads from pathlib import Path from time import sleep from typing import Any @@ -194,3 +195,41 @@ def test_oauth_enroll_auth(self): self.driver.get(self.if_user_url("/settings")) self.assert_user(User(username="foo", name="admin", email="admin@example.com")) + + @retry() + @apply_blueprint( + "default/flow-default-authentication-flow.yaml", + "default/flow-default-invalidation-flow.yaml", + ) + @apply_blueprint( + "default/flow-default-source-authentication.yaml", + "default/flow-default-source-enrollment.yaml", + "default/flow-default-source-pre-authentication.yaml", + ) + def test_oauth_link(self): + """test OAuth Source link OIDC""" + self.create_objects() + self.driver.get(self.live_server_url) + self.login() + + self.driver.get( + self.url("authentik_sources_oauth:oauth-client-login", source_slug=self.slug) + ) + + # Now we should be at the IDP, wait for the login field + self.wait.until(ec.presence_of_element_located((By.ID, "login"))) + self.driver.find_element(By.ID, "login").send_keys("admin@example.com") + self.driver.find_element(By.ID, "password").send_keys("password") + self.driver.find_element(By.ID, "password").send_keys(Keys.ENTER) + + # Wait until we're logged in + self.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]"))) + self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click() + + self.driver.get(self.url("authentik_api:usersourceconnection-list") + "?format=json") + body_json = loads(self.driver.find_element(By.CSS_SELECTOR, "pre").text) + results = body_json["results"] + self.assertEqual(len(results), 1) + connection = results[0] + self.assertEqual(connection["source"]["slug"], self.slug) + self.assertEqual(connection["user"], self.user.pk)