Check if cast message came from logged in instance (#12354)
This commit is contained in:
parent
1a69503eab
commit
f5385ba277
|
@ -181,7 +181,7 @@ class HcCast extends LitElement {
|
||||||
private async _handlePickView(ev: Event) {
|
private async _handlePickView(ev: Event) {
|
||||||
const path = (ev.currentTarget as any).getAttribute("data-path");
|
const path = (ev.currentTarget as any).getAttribute("data-path");
|
||||||
await ensureConnectedCastSession(this.castManager!, this.auth!);
|
await ensureConnectedCastSession(this.castManager!, this.auth!);
|
||||||
castSendShowLovelaceView(this.castManager, path);
|
castSendShowLovelaceView(this.castManager, path, this.auth.data.hassUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _handleLogout() {
|
private async _handleLogout() {
|
||||||
|
|
|
@ -33,7 +33,6 @@ import { castContext } from "../cast_context";
|
||||||
import "./hc-launch-screen";
|
import "./hc-launch-screen";
|
||||||
|
|
||||||
let resourcesLoaded = false;
|
let resourcesLoaded = false;
|
||||||
|
|
||||||
@customElement("hc-main")
|
@customElement("hc-main")
|
||||||
export class HcMain extends HassElement {
|
export class HcMain extends HassElement {
|
||||||
@state() private _showDemo = false;
|
@state() private _showDemo = false;
|
||||||
|
@ -46,6 +45,8 @@ export class HcMain extends HassElement {
|
||||||
|
|
||||||
@state() private _urlPath?: string | null;
|
@state() private _urlPath?: string | null;
|
||||||
|
|
||||||
|
private _hassUUID?: string;
|
||||||
|
|
||||||
private _unsubLovelace?: UnsubscribeFunc;
|
private _unsubLovelace?: UnsubscribeFunc;
|
||||||
|
|
||||||
public processIncomingMessage(msg: HassMessage) {
|
public processIncomingMessage(msg: HassMessage) {
|
||||||
|
@ -125,6 +126,7 @@ export class HcMain extends HassElement {
|
||||||
|
|
||||||
if (this.hass) {
|
if (this.hass) {
|
||||||
status.hassUrl = this.hass.auth.data.hassUrl;
|
status.hassUrl = this.hass.auth.data.hassUrl;
|
||||||
|
status.hassUUID = this._hassUUID;
|
||||||
status.lovelacePath = this._lovelacePath;
|
status.lovelacePath = this._lovelacePath;
|
||||||
status.urlPath = this._urlPath;
|
status.urlPath = this._urlPath;
|
||||||
}
|
}
|
||||||
|
@ -163,6 +165,18 @@ export class HcMain extends HassElement {
|
||||||
};
|
};
|
||||||
|
|
||||||
private async _handleGetStatusMessage(msg: GetStatusMessage) {
|
private async _handleGetStatusMessage(msg: GetStatusMessage) {
|
||||||
|
if (
|
||||||
|
(this.hass && msg.hassUUID && msg.hassUUID !== this._hassUUID) ||
|
||||||
|
(this.hass && msg.hassUrl && msg.hassUrl !== this.hass.auth.data.hassUrl)
|
||||||
|
) {
|
||||||
|
this._error = "Not connected to the same Home Assistant instance.";
|
||||||
|
this._sendError(
|
||||||
|
ReceiverErrorCode.WRONG_INSTANCE,
|
||||||
|
this._error,
|
||||||
|
msg.senderId!
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this._sendStatus(msg.senderId!);
|
this._sendStatus(msg.senderId!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,6 +193,7 @@ export class HcMain extends HassElement {
|
||||||
expires_in: 0,
|
expires_in: 0,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
this._hassUUID = msg.hassUUID;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const errorMessage = this._getErrorMessage(err);
|
const errorMessage = this._getErrorMessage(err);
|
||||||
this._error = errorMessage;
|
this._error = errorMessage;
|
||||||
|
@ -209,9 +224,29 @@ export class HcMain extends HassElement {
|
||||||
if (!this.hass) {
|
if (!this.hass) {
|
||||||
this._sendStatus(msg.senderId!);
|
this._sendStatus(msg.senderId!);
|
||||||
this._error = "Cannot show Lovelace because we're not connected.";
|
this._error = "Cannot show Lovelace because we're not connected.";
|
||||||
this._sendError(ReceiverErrorCode.NOT_CONNECTED, this._error);
|
this._sendError(
|
||||||
|
ReceiverErrorCode.NOT_CONNECTED,
|
||||||
|
this._error,
|
||||||
|
msg.senderId!
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
(msg.hassUUID && msg.hassUUID !== this._hassUUID) ||
|
||||||
|
(msg.hassUrl && msg.hassUrl !== this.hass.auth.data.hassUrl)
|
||||||
|
) {
|
||||||
|
this._sendStatus(msg.senderId!);
|
||||||
|
this._error =
|
||||||
|
"Cannot show Lovelace because we're not connected to the same Home Assistant instance.";
|
||||||
|
this._sendError(
|
||||||
|
ReceiverErrorCode.WRONG_INSTANCE,
|
||||||
|
this._error,
|
||||||
|
msg.senderId!
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
if (msg.urlPath === "lovelace") {
|
if (msg.urlPath === "lovelace") {
|
||||||
msg.urlPath = null;
|
msg.urlPath = null;
|
||||||
|
|
|
@ -8,6 +8,8 @@ import { BaseCastMessage } from "./types";
|
||||||
|
|
||||||
export interface GetStatusMessage extends BaseCastMessage {
|
export interface GetStatusMessage extends BaseCastMessage {
|
||||||
type: "get_status";
|
type: "get_status";
|
||||||
|
hassUrl?: string;
|
||||||
|
hassUUID?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConnectMessage extends BaseCastMessage {
|
export interface ConnectMessage extends BaseCastMessage {
|
||||||
|
@ -15,12 +17,15 @@ export interface ConnectMessage extends BaseCastMessage {
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
clientId: string | null;
|
clientId: string | null;
|
||||||
hassUrl: string;
|
hassUrl: string;
|
||||||
|
hassUUID?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ShowLovelaceViewMessage extends BaseCastMessage {
|
export interface ShowLovelaceViewMessage extends BaseCastMessage {
|
||||||
type: "show_lovelace_view";
|
type: "show_lovelace_view";
|
||||||
viewPath: string | number | null;
|
viewPath: string | number | null;
|
||||||
urlPath: string | null;
|
urlPath: string | null;
|
||||||
|
hassUrl: string;
|
||||||
|
hassUUID?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ShowDemoMessage extends BaseCastMessage {
|
export interface ShowDemoMessage extends BaseCastMessage {
|
||||||
|
@ -43,6 +48,7 @@ export const castSendAuth = (cast: CastManager, auth: Auth) =>
|
||||||
|
|
||||||
export const castSendShowLovelaceView = (
|
export const castSendShowLovelaceView = (
|
||||||
cast: CastManager,
|
cast: CastManager,
|
||||||
|
hassUrl: string,
|
||||||
viewPath: ShowLovelaceViewMessage["viewPath"],
|
viewPath: ShowLovelaceViewMessage["viewPath"],
|
||||||
urlPath?: string | null
|
urlPath?: string | null
|
||||||
) =>
|
) =>
|
||||||
|
@ -50,6 +56,7 @@ export const castSendShowLovelaceView = (
|
||||||
type: "show_lovelace_view",
|
type: "show_lovelace_view",
|
||||||
viewPath,
|
viewPath,
|
||||||
urlPath: urlPath || null,
|
urlPath: urlPath || null,
|
||||||
|
hassUrl: CAST_DEV ? CAST_DEV_HASS_URL : hassUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const castSendShowDemo = (cast: CastManager) =>
|
export const castSendShowDemo = (cast: CastManager) =>
|
||||||
|
|
|
@ -7,6 +7,7 @@ export interface ReceiverStatusMessage extends BaseCastMessage {
|
||||||
connected: boolean;
|
connected: boolean;
|
||||||
showDemo: boolean;
|
showDemo: boolean;
|
||||||
hassUrl?: string;
|
hassUrl?: string;
|
||||||
|
hassUUID?: string;
|
||||||
lovelacePath?: string | number | null;
|
lovelacePath?: string | number | null;
|
||||||
urlPath?: string | null;
|
urlPath?: string | null;
|
||||||
}
|
}
|
||||||
|
@ -23,6 +24,7 @@ export const enum ReceiverErrorCode {
|
||||||
CONNECTION_LOST = 3,
|
CONNECTION_LOST = 3,
|
||||||
HASS_URL_MISSING = 4,
|
HASS_URL_MISSING = 4,
|
||||||
NO_HTTPS = 5,
|
NO_HTTPS = 5,
|
||||||
|
WRONG_INSTANCE = 20,
|
||||||
NOT_CONNECTED = 21,
|
NOT_CONNECTED = 21,
|
||||||
FETCH_CONFIG_FAILED = 22,
|
FETCH_CONFIG_FAILED = 22,
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,6 +122,7 @@ class HuiCastRow extends LitElement implements LovelaceRow {
|
||||||
await ensureConnectedCastSession(this._castManager!, this.hass.auth);
|
await ensureConnectedCastSession(this._castManager!, this.hass.auth);
|
||||||
castSendShowLovelaceView(
|
castSendShowLovelaceView(
|
||||||
this._castManager!,
|
this._castManager!,
|
||||||
|
this.hass.auth.data.hassUrl,
|
||||||
this._config!.view,
|
this._config!.view,
|
||||||
this._config!.dashboard
|
this._config!.dashboard
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue