Skip to content

Commit

Permalink
sources/oauth: ensure all UI sources return a valid source (cherry-pick
Browse files Browse the repository at this point in the history
#9401) (#9406)

sources/oauth: ensure all UI sources return a valid source (#9401)

* web/admin: prevent selection of inbuilt source in identification stage



* fix apple source



* also fix plex challenge



---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens L <jens@goauthentik.io>
  • Loading branch information
gcp-cherry-pick-bot[bot] and BeryJu authored Apr 24, 2024
1 parent 11ff795 commit 541becf
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
37 changes: 37 additions & 0 deletions authentik/sources/oauth/tests/test_type_apple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Apple Type tests"""

from django.test import RequestFactory, TestCase
from guardian.shortcuts import get_anonymous_user

from authentik.lib.generators import generate_id
from authentik.lib.tests.utils import dummy_get_response
from authentik.root.middleware import SessionMiddleware
from authentik.sources.oauth.models import OAuthSource
from authentik.sources.oauth.types.registry import registry


class TestTypeApple(TestCase):
"""OAuth Source tests"""

def setUp(self):
self.source = OAuthSource.objects.create(
name="test",
slug="test",
provider_type="apple",
authorization_url="",
profile_url="",
consumer_key=generate_id(),
)
self.factory = RequestFactory()

def test_login_challenge(self):
"""Test login_challenge"""
request = self.factory.get("/")
request.user = get_anonymous_user()

middleware = SessionMiddleware(dummy_get_response)
middleware.process_request(request)
request.session.save()
oauth_type = registry.find_type("apple")
challenge = oauth_type().login_challenge(self.source, request)
self.assertTrue(challenge.is_valid(raise_exception=True))
2 changes: 1 addition & 1 deletion authentik/sources/oauth/types/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def login_challenge(self, source: OAuthSource, request: HttpRequest) -> Challeng
)
args = apple_client.get_redirect_args()
return AppleLoginChallenge(
instance={
data={
"client_id": apple_client.get_client_id(),
"scope": "name email",
"redirect_uri": args["redirect_uri"],
Expand Down
2 changes: 1 addition & 1 deletion authentik/sources/plex/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def ui_login_button(self, request: HttpRequest) -> UILoginButton:
icon = static("authentik/sources/plex.svg")
return UILoginButton(
challenge=PlexAuthenticationChallenge(
{
data={
"type": ChallengeTypes.NATIVE.value,
"component": "ak-source-plex",
"client_id": self.client_id,
Expand Down
5 changes: 5 additions & 0 deletions authentik/sources/plex/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def setUp(self):
slug="test",
)

def test_login_challenge(self):
"""Test login_challenge"""
ui_login_button = self.source.ui_login_button(None)
self.assertTrue(ui_login_button.challenge.is_valid(raise_exception=True))

def test_get_user_info(self):
"""Test get_user_info"""
token = generate_key()
Expand Down
2 changes: 2 additions & 0 deletions authentik/stages/identification/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def get_challenge(self) -> Challenge:
)
for source in sources:
ui_login_button = source.ui_login_button(self.request)
if source.component == "":
continue
if ui_login_button:
button = asdict(ui_login_button)
source_challenge = ui_login_button.challenge
Expand Down
39 changes: 17 additions & 22 deletions web/src/admin/stages/identification/IdentificationStageForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,28 +214,23 @@ export class IdentificationStageForm extends BaseStageForm<IdentificationStage>
name="sources"
>
<select class="pf-c-form-control" multiple>
${this.sources?.results.map((source) => {
let selected = Array.from(this.instance?.sources || []).some(
(su) => {
return su == source.pk;
},
);
// Creating a new instance, auto-select built-in source
// Only when no other sources exist
if (
!this.instance &&
source.component === "" &&
(this.sources?.results || []).length < 2
) {
selected = true;
}
return html`<option
value=${ifDefined(source.pk)}
?selected=${selected}
>
${source.name}
</option>`;
})}
${this.sources?.results
.filter((source) => {
return source.component !== "";
})
.map((source) => {
const selected = Array.from(this.instance?.sources || []).some(
(su) => {
return su == source.pk;
},
);
return html`<option
value=${ifDefined(source.pk)}
?selected=${selected}
>
${source.name}
</option>`;
})}
</select>
<p class="pf-c-form__helper-text">
${msg(
Expand Down

0 comments on commit 541becf

Please sign in to comment.