Skip to content

Commit

Permalink
Merge branch 'feature/add_ws_auth' into 'master'
Browse files Browse the repository at this point in the history
tcp_transport: Add authorization header for websocket client

Closes FCS-1126

See merge request espressif/esp-idf!22202
  • Loading branch information
ESP-YJM committed Feb 6, 2023
2 parents 7d25e38 + fecf476 commit f6b0d04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions components/tcp_transport/include/esp_transport_ws.h
Expand Up @@ -34,6 +34,7 @@ typedef struct {
const char *sub_protocol; /*!< WS subprotocol */
const char *user_agent; /*!< WS user agent */
const char *headers; /*!< WS additional headers */
const char *auth; /*!< HTTP authorization header */
bool propagate_control_frames; /*!< If true, control frames are passed to the reader
* If false, only user frames are propagated, control frames are handled
* automatically during read operations
Expand Down Expand Up @@ -93,6 +94,18 @@ esp_err_t esp_transport_ws_set_user_agent(esp_transport_handle_t t, const char *
*/
esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *headers);

/**
* @brief Set websocket authorization headers
*
* @param t websocket transport handle
* @param sub_protocol The HTTP authorization header string, set NULL to clear the old value
*
* @return
* - ESP_OK on success
* - One of the error codes
*/
esp_err_t esp_transport_ws_set_auth(esp_transport_handle_t t, const char *auth);

/**
* @brief Set websocket transport parameters
*
Expand Down
36 changes: 36 additions & 0 deletions components/tcp_transport/transport_ws.c
Expand Up @@ -55,6 +55,7 @@ typedef struct {
char *sub_protocol;
char *user_agent;
char *headers;
char *auth;
bool propagate_control_frames;
ws_transport_frame_state_t frame_state;
esp_transport_handle_t parent;
Expand Down Expand Up @@ -194,6 +195,16 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
return -1;
}
}
if (ws->auth) {
ESP_LOGD(TAG, "Authorization: %s", ws->auth);
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "Authorization: %s\r\n", ws->auth);
len += r;
if (r <= 0 || len >= WS_BUFFER_SIZE) {
ESP_LOGE(TAG, "Error in request generation"
"(snprintf of authorization returned %d, desired request len: %d, buffer size: %d", r, len, WS_BUFFER_SIZE);
return -1;
}
}
if (ws->headers) {
ESP_LOGD(TAG, "headers: %s", ws->headers);
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "%s", ws->headers);
Expand Down Expand Up @@ -567,6 +578,7 @@ static esp_err_t ws_destroy(esp_transport_handle_t t)
free(ws->sub_protocol);
free(ws->user_agent);
free(ws->headers);
free(ws->auth);
free(ws);
return 0;
}
Expand Down Expand Up @@ -711,6 +723,26 @@ esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *hea
return ESP_OK;
}

esp_err_t esp_transport_ws_set_auth(esp_transport_handle_t t, const char *auth)
{
if (t == NULL) {
return ESP_ERR_INVALID_ARG;
}
transport_ws_t *ws = esp_transport_get_context_data(t);
if (ws->auth) {
free(ws->auth);
}
if (auth == NULL) {
ws->auth = NULL;
return ESP_OK;
}
ws->auth = strdup(auth);
if (ws->auth == NULL) {
return ESP_ERR_NO_MEM;
}
return ESP_OK;
}

esp_err_t esp_transport_ws_set_config(esp_transport_handle_t t, const esp_transport_ws_config_t *config)
{
if (t == NULL) {
Expand All @@ -734,6 +766,10 @@ esp_err_t esp_transport_ws_set_config(esp_transport_handle_t t, const esp_transp
err = esp_transport_ws_set_headers(t, config->headers);
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
}
if (config->auth) {
err = esp_transport_ws_set_auth(t, config->auth);
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
}
ws->propagate_control_frames = config->propagate_control_frames;

return err;
Expand Down

3 comments on commit f6b0d04

@Guitton-S
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am interested by this feature do you know when it will be included in an official release?
Thank you

@david-cermak
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be available in the upcoming v5.1. We're probably going to backport this to v5.0, so it would appear in v5.0.2 eventually.

@Guitton-S
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give me an expected date even if it's not sure at all for the moment?

Thank you,

Please sign in to comment.