Skip to content

Commit dcc5915

Browse files
committed
fix multiple redirects, add e2e test
Signed-off-by: Jens Langhammer <[email protected]>
1 parent 484b4bf commit dcc5915

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

authentik/policies/templates/policies/buffer.html

+3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
{% block head %}
77
{{ block.super }}
88
<script>
9+
let redirecting = false;
910
const checkAuth = async () => {
11+
if (redirecting) return true;
1012
const url = "{{ check_auth_url }}";
1113
console.debug("authentik/policies/buffer: Checking authentication...");
1214
try {
@@ -17,6 +19,7 @@
1719
return false
1820
}
1921
console.debug("authentik/policies/buffer: Continuing");
22+
redirecting = true;
2023
if ("{{ auth_req_method }}" === "post") {
2124
document.querySelector("form").submit();
2225
} else {

authentik/policies/views.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,17 @@ class BufferedPolicyAccessView(PolicyAccessView):
173173
def handle_no_permission(self):
174174
plan: FlowPlan | None = self.request.session.get(SESSION_KEY_PLAN)
175175
if not plan:
176+
LOGGER.debug("Not buffering request, no flow plan active")
176177
return super().handle_no_permission()
177178
flow = Flow.objects.filter(pk=plan.flow_pk).first()
178179
if not flow or flow.designation != FlowDesignation.AUTHENTICATION:
180+
LOGGER.debug("Not buffering request, no flow or flow not for authentication")
179181
return super().handle_no_permission()
180182
if self.request.GET.get(QS_SKIP_BUFFER):
183+
LOGGER.debug("Not buffering request, explicit skip")
181184
return super().handle_no_permission()
182185
buffer_id = str(uuid4())
186+
LOGGER.debug("Buffering access request", bf_id=buffer_id)
183187
self.request.session[SESSION_KEY_BUFFER % buffer_id] = {
184188
"body": self.request.POST,
185189
"url": self.request.build_absolute_uri(self.request.get_full_path()),
@@ -192,5 +196,5 @@ def handle_no_permission(self):
192196
def dispatch(self, request, *args, **kwargs):
193197
response = super().dispatch(request, *args, **kwargs)
194198
if QS_BUFFER_ID in self.request.GET:
195-
self.request.session.pop(SESSION_KEY_BUFFER % self.request.GET[QS_BUFFER_ID])
199+
self.request.session.pop(SESSION_KEY_BUFFER % self.request.GET[QS_BUFFER_ID], None)
196200
return response

tests/e2e/test_provider_oauth2_grafana.py

+74
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,77 @@ def test_authorization_denied(self):
410410
self.driver.find_element(By.CSS_SELECTOR, "header > h1").text,
411411
"Permission denied",
412412
)
413+
414+
@retry()
415+
@apply_blueprint(
416+
"default/flow-default-authentication-flow.yaml",
417+
"default/flow-default-invalidation-flow.yaml",
418+
)
419+
@apply_blueprint("default/flow-default-provider-authorization-implicit-consent.yaml")
420+
@apply_blueprint("system/providers-oauth2.yaml")
421+
@reconcile_app("authentik_crypto")
422+
def test_authorization_consent_implied_parallel(self):
423+
"""test OpenID Provider flow (default authorization flow with implied consent)"""
424+
# Bootstrap all needed objects
425+
authorization_flow = Flow.objects.get(
426+
slug="default-provider-authorization-implicit-consent"
427+
)
428+
provider = OAuth2Provider.objects.create(
429+
name=generate_id(),
430+
client_type=ClientTypes.CONFIDENTIAL,
431+
client_id=self.client_id,
432+
client_secret=self.client_secret,
433+
signing_key=create_test_cert(),
434+
redirect_uris=[
435+
RedirectURI(
436+
RedirectURIMatchingMode.STRICT, "http://localhost:3000/login/generic_oauth"
437+
)
438+
],
439+
authorization_flow=authorization_flow,
440+
)
441+
provider.property_mappings.set(
442+
ScopeMapping.objects.filter(
443+
scope_name__in=[
444+
SCOPE_OPENID,
445+
SCOPE_OPENID_EMAIL,
446+
SCOPE_OPENID_PROFILE,
447+
SCOPE_OFFLINE_ACCESS,
448+
]
449+
)
450+
)
451+
Application.objects.create(
452+
name=generate_id(),
453+
slug=self.app_slug,
454+
provider=provider,
455+
)
456+
457+
self.driver.get(self.live_server_url)
458+
login_window = self.driver.current_window_handle
459+
460+
self.driver.switch_to.new_window("tab")
461+
grafana_window = self.driver.current_window_handle
462+
self.driver.get("http://localhost:3000")
463+
self.driver.find_element(By.CLASS_NAME, "btn-service--oauth").click()
464+
465+
self.driver.switch_to.window(login_window)
466+
self.login()
467+
468+
self.driver.switch_to.window(grafana_window)
469+
self.wait_for_url("http://localhost:3000/?orgId=1")
470+
self.driver.get("http://localhost:3000/profile")
471+
self.assertEqual(
472+
self.driver.find_element(By.CLASS_NAME, "page-header__title").text,
473+
self.user.name,
474+
)
475+
self.assertEqual(
476+
self.driver.find_element(By.CSS_SELECTOR, "input[name=name]").get_attribute("value"),
477+
self.user.name,
478+
)
479+
self.assertEqual(
480+
self.driver.find_element(By.CSS_SELECTOR, "input[name=email]").get_attribute("value"),
481+
self.user.email,
482+
)
483+
self.assertEqual(
484+
self.driver.find_element(By.CSS_SELECTOR, "input[name=login]").get_attribute("value"),
485+
self.user.email,
486+
)

0 commit comments

Comments
 (0)