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