Skip to content

Commit 0041cf8

Browse files
risssonBeryJu
andauthored
providers/oauth2: launch url: if URL parsing fails, return no launch URL (#5918)
* providers/oauth2: launch url: if URL parsing fails, return no launch URL Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space> * add test Signed-off-by: Jens Langhammer <jens@goauthentik.io> * only get provider launch URL when no url is set Signed-off-by: Jens Langhammer <jens@goauthentik.io> * only catch value error Signed-off-by: Jens Langhammer <jens@goauthentik.io> * format Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space> Signed-off-by: Jens Langhammer <jens@goauthentik.io> Co-authored-by: Jens Langhammer <jens@goauthentik.io>
1 parent 5873855 commit 0041cf8

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

authentik/core/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ def get_meta_icon(self) -> Optional[str]:
376376
def get_launch_url(self, user: Optional["User"] = None) -> Optional[str]:
377377
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
378378
url = None
379-
if provider := self.get_provider():
380-
url = provider.launch_url
381379
if self.meta_launch_url:
382380
url = self.meta_launch_url
381+
elif provider := self.get_provider():
382+
url = provider.launch_url
383383
if user and url:
384384
if isinstance(user, SimpleLazyObject):
385385
user._setup()

authentik/providers/oauth2/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from django.utils.translation import gettext_lazy as _
1818
from jwt import encode
1919
from rest_framework.serializers import Serializer
20+
from structlog.stdlib import get_logger
2021

2122
from authentik.core.models import ExpiringModel, PropertyMapping, Provider, User
2223
from authentik.crypto.models import CertificateKeyPair
@@ -26,6 +27,8 @@
2627
from authentik.providers.oauth2.id_token import IDToken, SubModes
2728
from authentik.sources.oauth.models import OAuthSource
2829

30+
LOGGER = get_logger()
31+
2932

3033
def generate_client_secret() -> str:
3134
"""Generate client secret with adequate length"""
@@ -251,8 +254,12 @@ def launch_url(self) -> Optional[str]:
251254
if self.redirect_uris == "":
252255
return None
253256
main_url = self.redirect_uris.split("\n", maxsplit=1)[0]
254-
launch_url = urlparse(main_url)._replace(path="")
255-
return urlunparse(launch_url)
257+
try:
258+
launch_url = urlparse(main_url)._replace(path="")
259+
return urlunparse(launch_url)
260+
except ValueError as exc:
261+
LOGGER.warning("Failed to format launch url", exc=exc)
262+
return None
256263

257264
@property
258265
def component(self) -> str:

authentik/providers/oauth2/tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test OAuth2 API"""
22
from json import loads
3+
from sys import version_info
4+
from unittest import skipUnless
35

46
from django.urls import reverse
57
from rest_framework.test import APITestCase
@@ -42,3 +44,14 @@ def test_setup_urls(self):
4244
self.assertEqual(response.status_code, 200)
4345
body = loads(response.content.decode())
4446
self.assertEqual(body["issuer"], "http://testserver/application/o/test/")
47+
48+
# https://github.com/goauthentik/authentik/pull/5918
49+
@skipUnless(version_info >= (3, 11, 4), "This behaviour is only Python 3.11.4 and up")
50+
def test_launch_url(self):
51+
"""Test launch_url"""
52+
self.provider.redirect_uris = (
53+
"https://[\\d\\w]+.pr.test.goauthentik.io/source/oauth/callback/authentik/\n"
54+
)
55+
self.provider.save()
56+
self.provider.refresh_from_db()
57+
self.assertIsNone(self.provider.launch_url)

0 commit comments

Comments
 (0)