Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home Assistant WS: Send auth token upon auth request #368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions main/endpoint/hass.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,42 @@ static void cb_ws_event(const void *arg_evh, const esp_event_base_t *base_ev, co
goto cleanup;
}

cJSON *type = cJSON_GetObjectItemCaseSensitive(cjson, "type");
if (!cJSON_IsString(type) || type->valuestring == NULL) {
goto cleanup;
}

if (strcmp(type->valuestring, "auth_required") == 0) {
char *hass_token = config_get_char("hass_token", DEFAULT_TOKEN);
int len_auth = strlen(hass_token) + 34;
char *auth = calloc(sizeof(char), len_auth);
snprintf(auth, len_auth, "{\"type\":\"auth\",\"access_token\":\"%s\"}", hass_token);
free(hass_token);

// we must not send the terminating null byte
int ret = esp_websocket_client_send_text(hdl_wc, auth, len_auth - 1, 2000 / portTICK_PERIOD_MS);
free(auth);
if (ret < 0) {
ESP_LOGE(TAG, "failed to authenticate WebSocket client");
}
goto cleanup;
}

cJSON *event = cJSON_GetObjectItemCaseSensitive(cjson, "event");
if (!cJSON_IsObject(event)) {
goto cleanup;
}

cJSON *type = cJSON_GetObjectItemCaseSensitive(event, "type");
if (!cJSON_IsString(type) || type->valuestring == NULL) {
cJSON *event_type = cJSON_GetObjectItemCaseSensitive(event, "type");
if (!cJSON_IsString(event_type) || event_type->valuestring == NULL) {
goto cleanup;
}

if (strcmp(type->valuestring, "run-end") == 0) {
if (strcmp(event_type->valuestring, "run-end") == 0) {
goto end;
}

if (strcmp(type->valuestring, "intent-end") != 0) {
if (strcmp(event_type->valuestring, "intent-end") != 0) {
goto cleanup;
}

Expand Down Expand Up @@ -215,12 +236,7 @@ static void hass_get_url(char **url, const char *path, const bool ws)

static void init_hass_ws_client(void)
{
char *auth = NULL;
char *hass_token = NULL;
char *url = NULL;
esp_err_t err;
int len_auth, ret;

hass_get_url(&url, HASS_URI_WEBSOCKET, true);

const esp_websocket_client_config_t cfg_wc = {
Expand All @@ -236,23 +252,9 @@ static void init_hass_ws_client(void)

esp_websocket_register_events(hdl_wc, WEBSOCKET_EVENT_ANY, (esp_event_handler_t)cb_ws_event, NULL);

err = esp_websocket_client_start(hdl_wc);
esp_err_t err = esp_websocket_client_start(hdl_wc);
if (err != ESP_OK) {
ESP_LOGE(TAG, "failed to start WebSocket client: %s", esp_err_to_name(err));
return;
}

hass_token = config_get_char("hass_token", DEFAULT_TOKEN);
len_auth = strlen(hass_token) + 34;
auth = calloc(sizeof(char), len_auth);
snprintf(auth, len_auth, "{\"type\":\"auth\",\"access_token\":\"%s\"}", hass_token);
free(hass_token);

// we must not send the terminating null byte
ret = esp_websocket_client_send_text(hdl_wc, auth, len_auth - 1, 2000 / portTICK_PERIOD_MS);
free(auth);
if (ret < 0) {
ESP_LOGE(TAG, "failed to authenticate WebSocket client");
}
}

Expand Down
Loading