Skip to content

Commit

Permalink
providers/oauth2: launch url: if URL parsing fails, return no launch …
Browse files Browse the repository at this point in the history
…URL (#5918)

* providers/oauth2: launch url: if URL parsing fails, return no launch URL

Signed-off-by: Marc 'risson' Schmitt <[email protected]>

* add test

Signed-off-by: Jens Langhammer <[email protected]>

* only get provider launch URL when no url is set

Signed-off-by: Jens Langhammer <[email protected]>

* only catch value error

Signed-off-by: Jens Langhammer <[email protected]>

* format

Signed-off-by: Jens Langhammer <[email protected]>

---------

Signed-off-by: Marc 'risson' Schmitt <[email protected]>
Signed-off-by: Jens Langhammer <[email protected]>
Co-authored-by: Jens Langhammer <[email protected]>
  • Loading branch information
rissson and BeryJu authored Jun 9, 2023
1 parent 5873855 commit 0041cf8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions authentik/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ def get_meta_icon(self) -> Optional[str]:
def get_launch_url(self, user: Optional["User"] = None) -> Optional[str]:
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
url = None
if provider := self.get_provider():
url = provider.launch_url
if self.meta_launch_url:
url = self.meta_launch_url
elif provider := self.get_provider():
url = provider.launch_url
if user and url:
if isinstance(user, SimpleLazyObject):
user._setup()
Expand Down
11 changes: 9 additions & 2 deletions authentik/providers/oauth2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils.translation import gettext_lazy as _
from jwt import encode
from rest_framework.serializers import Serializer
from structlog.stdlib import get_logger

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

LOGGER = get_logger()


def generate_client_secret() -> str:
"""Generate client secret with adequate length"""
Expand Down Expand Up @@ -251,8 +254,12 @@ def launch_url(self) -> Optional[str]:
if self.redirect_uris == "":
return None
main_url = self.redirect_uris.split("\n", maxsplit=1)[0]
launch_url = urlparse(main_url)._replace(path="")
return urlunparse(launch_url)
try:
launch_url = urlparse(main_url)._replace(path="")
return urlunparse(launch_url)
except ValueError as exc:
LOGGER.warning("Failed to format launch url", exc=exc)
return None

@property
def component(self) -> str:
Expand Down
13 changes: 13 additions & 0 deletions authentik/providers/oauth2/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test OAuth2 API"""
from json import loads
from sys import version_info
from unittest import skipUnless

from django.urls import reverse
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -42,3 +44,14 @@ def test_setup_urls(self):
self.assertEqual(response.status_code, 200)
body = loads(response.content.decode())
self.assertEqual(body["issuer"], "http://testserver/application/o/test/")

# https://github.com/goauthentik/authentik/pull/5918
@skipUnless(version_info >= (3, 11, 4), "This behaviour is only Python 3.11.4 and up")
def test_launch_url(self):
"""Test launch_url"""
self.provider.redirect_uris = (
"https://[\\d\\w]+.pr.test.goauthentik.io/source/oauth/callback/authentik/\n"
)
self.provider.save()
self.provider.refresh_from_db()
self.assertIsNone(self.provider.launch_url)

0 comments on commit 0041cf8

Please sign in to comment.