Add support for OAuth2 callback via My (#12718)

This commit is contained in:
Paulus Schoutsen 2022-05-18 11:18:43 -07:00 committed by GitHub
parent f807618f75
commit 7d1c77a38f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -123,6 +123,15 @@ export const getMyRedirects = (hasSupervisor: boolean): Redirects => ({
component: "lovelace",
redirect: "/config/lovelace/resources",
},
oauth2_authorize_callback: {
redirect: "/auth/external/callback",
navigate_outside_spa: true,
params: {
error: "string?",
code: "string?",
state: "string",
},
},
people: {
component: "person",
redirect: "/config/person",
@ -214,11 +223,13 @@ const getRedirect = (
hasSupervisor: boolean
): Redirect | undefined => getMyRedirects(hasSupervisor)?.[path];
export type ParamType = "url" | "string";
export type ParamType = "url" | "string" | "string?";
export type Redirects = { [key: string]: Redirect };
export interface Redirect {
redirect: string;
// Set to True to use browser redirect instead of frontend navigation
navigate_outside_spa?: boolean;
component?: string;
params?: {
[key: string]: ParamType;
@ -277,7 +288,11 @@ class HaPanelMy extends LitElement {
return;
}
navigate(url, { replace: true });
if (this._redirect.navigate_outside_spa) {
location.assign(url);
} else {
navigate(url, { replace: true });
}
}
protected render() {
@ -345,17 +360,20 @@ class HaPanelMy extends LitElement {
return "";
}
const resultParams = {};
Object.entries(this._redirect!.params || {}).forEach(([key, type]) => {
for (const [key, type] of Object.entries(this._redirect!.params || {})) {
if (!params[key] && type.endsWith("?")) {
continue;
}
if (!params[key] || !this._checkParamType(type, params[key])) {
throw Error();
}
resultParams[key] = params[key];
});
}
return `?${createSearchParam(resultParams)}`;
}
private _checkParamType(type: ParamType, value: string) {
if (type === "string") {
if (type === "string" || type === "string?") {
return true;
}
if (type === "url") {