web: detect deep links in flow interface and redirect locally

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer
2021-03-23 17:55:32 +01:00
parent cfe7bc8155
commit de6fa63d21
5 changed files with 36 additions and 8 deletions

View File

@ -40,6 +40,9 @@ import { ifDefined } from "lit-html/directives/if-defined";
import { until } from "lit-html/directives/until";
import { TITLE_SUFFIX } from "../elements/router/RouterOutlet";
import { AccessDeniedChallenge } from "./access_denied/FlowAccessDenied";
import { getQueryVariables } from "./utils";
export const NEXT_ARG = "next";
@customElement("ak-flow-executor")
export class FlowExecutor extends LitElement implements StageHost {
@ -123,6 +126,14 @@ export class FlowExecutor extends LitElement implements StageHost {
}
firstUpdated(): void {
// Check if there is a ?next arg and save it
// this is used for deep linking, if a user tries to access an application,
// but needs to authenticate first
const queryVars = getQueryVariables();
if (NEXT_ARG in queryVars) {
const next = queryVars[NEXT_ARG];
localStorage.setItem(NEXT_ARG, next);
}
new RootApi(DEFAULT_CONFIG).rootConfigList().then((config) => {
this.config = config;
});
@ -171,7 +182,12 @@ export class FlowExecutor extends LitElement implements StageHost {
switch (this.challenge.type) {
case ChallengeTypeEnum.Redirect:
console.debug(`authentik/flows: redirecting to ${(this.challenge as RedirectChallenge).to}`);
window.location.assign((this.challenge as RedirectChallenge).to);
if (localStorage.getItem(NEXT_ARG) === null) {
window.location.assign((this.challenge as RedirectChallenge).to);
} else {
localStorage.clear();
window.location.assign(localStorage.getItem(NEXT_ARG) || "");
}
return this.renderLoading();
case ChallengeTypeEnum.Shell:
return html`${unsafeHTML((this.challenge as ShellChallenge).body)}`;

10
web/src/flows/utils.ts Normal file
View File

@ -0,0 +1,10 @@
export function getQueryVariables(): Record<string, string> {
const query = window.location.search.substring(1);
const vars = query.split("&");
const entries: Record<string, string> = {};
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
entries[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return entries;
}