Skip to content

Commit

Permalink
core: fix source_flow_manager throwing error when authenticated user …
Browse files Browse the repository at this point in the history
…attempts to re-authenticate with existing link

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
BeryJu committed Nov 19, 2024
1 parent 9e96f19 commit 8933eff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions authentik/core/sources/flow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def get_action(self, **kwargs) -> tuple[Action, UserSourceConnection | None]: #
)
new_connection.user = self.request.user
new_connection = self.update_user_connection(new_connection, **kwargs)
if existing := self.user_connection_type.objects.filter(
source=self.source, identifier=self.identifier
).first():
existing = self.update_user_connection(existing)
return Action.AUTH, existing

Check warning on line 136 in authentik/core/sources/flow_manager.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/sources/flow_manager.py#L135-L136

Added lines #L135 - L136 were not covered by tests
return Action.LINK, new_connection

action, connection = self.matcher.get_user_action(self.identifier, self.user_properties)
Expand Down
16 changes: 16 additions & 0 deletions authentik/core/tests/test_source_flow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ def test_authenticated_link(self):
reverse("authentik_core:if-user") + "#/settings;page-sources",
)

def test_authenticated_auth(self):

Check warning on line 84 in authentik/core/tests/test_source_flow_manager.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_source_flow_manager.py#L84

Added line #L84 was not covered by tests
"""Test authenticated user linking"""
user = User.objects.create(username="foo", email="foo@bar.baz")
UserOAuthSourceConnection.objects.create(

Check warning on line 87 in authentik/core/tests/test_source_flow_manager.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_source_flow_manager.py#L86-L87

Added lines #L86 - L87 were not covered by tests
user=user, source=self.source, identifier=self.identifier
)
request = get_request("/", user=user)
flow_manager = OAuthSourceFlowManager(

Check warning on line 91 in authentik/core/tests/test_source_flow_manager.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_source_flow_manager.py#L90-L91

Added lines #L90 - L91 were not covered by tests
self.source, request, self.identifier, {"info": {}}, {}
)
action, connection = flow_manager.get_action()
self.assertEqual(action, Action.AUTH)
self.assertIsNotNone(connection.pk)
response = flow_manager.get_flow()
self.assertEqual(response.status_code, 302)

Check warning on line 98 in authentik/core/tests/test_source_flow_manager.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_source_flow_manager.py#L94-L98

Added lines #L94 - L98 were not covered by tests

def test_unauthenticated_link(self):
"""Test un-authenticated user linking"""
flow_manager = OAuthSourceFlowManager(
Expand Down

0 comments on commit 8933eff

Please sign in to comment.