Merge pull request #2964 from cesanta/http-cookie
Some checks are pending
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (clang++, , mip_test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (clang++, , test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (clang++, BUILTIN, mip_test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (clang++, BUILTIN, test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (gcc, , mip_test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (gcc, , test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (gcc, BUILTIN, mip_test) (push) Waiting to run
Build and test - essentials / linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }} (gcc, BUILTIN, test) (push) Waiting to run
Build and test - essentials / s390 (push) Waiting to run
Build and test - essentials / armhf (push) Waiting to run
Build and test - essentials / unamalgamated-mg_prefix (push) Waiting to run
Build and test - essentials / macos SSL=${{ matrix.ssl }} () (push) Waiting to run
Build and test - essentials / macos SSL=${{ matrix.ssl }} (BUILTIN) (push) Waiting to run
Build and test - essentials / windows ${{ matrix.target }} (mingw++) (push) Waiting to run
Build and test - essentials / windows ${{ matrix.target }} (vc22) (push) Waiting to run
Build and test - essentials / windows ${{ matrix.target }} (vc98) (push) Waiting to run
Build and test - essentials / arm (push) Waiting to run
Build and test - essentials / riscv (push) Waiting to run
Build and test - essentials / examples (push) Waiting to run
Build and test - essentials / examples_win (push) Waiting to run
Build and test - essentials / examples_mac (push) Waiting to run
Build and test - essentials / refprojs (push) Waiting to run
Build and test - essentials / refprojs_win (push) Waiting to run
Build and test - essentials / refprojs_mac (push) Waiting to run
Build and test - essentials / tutorials (push) Waiting to run
Build and test - essentials / tutorials_win (push) Waiting to run
Build and test - essentials / tutorials_mac (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:infineon/infineon-xmc7200]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/frdm-mcxn947-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/frdm-mcxn947-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1020-evk-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1060-evk-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1170-evk-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f429zi-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f429zi-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f746zg-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f746zg-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h563zi-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h563zi-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h723zg-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h723zg-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h743zi-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-h743zi-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/stm32h573i-dk-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/stm32h573i-dk-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:ti/ek-tm4c1294xl-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:ti/ek-tm4c1294xl-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:esp32/uart-bridge]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:esp8266/http-client-server]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:infineon/infineon-xmc4700_4800-lwip-rtx-rtos]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-g031-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:esp32/device-dashboard]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:microchip/same54-xpro/device-dashboard]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1020-evk-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1060-evk-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:nxp/rt1170-evk-make-freertos-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:pico-sdk/pico-rndis-dashboard]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:pico-sdk/pico-w]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:pico-sdk/w5500-evb-pico-picosdk-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:pico-sdk/w5500-evb-pico2-picosdk-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:renesas/ek-ra6m4-make-baremetal-builtin]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f746zg-make-baremetal-builtin-cmsis_driver]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:stm32/nucleo-f746zg-make-freertos-tcp]) (push) Waiting to run
Build and test - essentials / ${{ matrix.example.path }} (map[path:pico-sdk/pico-rndis-device]) (push) Waiting to run
Build and test - essentials / cube_examples (push) Waiting to run
Build and test - essentials / test_f7 (push) Waiting to run
Build and test - essentials / test_f4 (push) Waiting to run
Build and test - essentials / test_h743 (push) Waiting to run
Build and test - essentials / test_h723 (push) Waiting to run
Build and test - essentials / test_h5 (push) Waiting to run
Build and test - essentials / test_rt1020 (push) Waiting to run
Build and test - essentials / test_rt1060 (push) Waiting to run
Build and test - essentials / test_rt1170 (push) Waiting to run
Build and test - essentials / test_ra6m4 (push) Waiting to run
Build and test - essentials / test_tm4c (push) Waiting to run
Build and test - essentials / test_same54 (push) Waiting to run
Build and test - essentials / test_pico_w5500 (push) Waiting to run

Use separate cookies for HTTP and HTTPS connections
This commit is contained in:
Sergio R. Caprile 2024-11-22 16:39:08 -03:00 committed by GitHub
commit b6dc830bcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 16 deletions

View File

@ -77,19 +77,22 @@ static struct user *authenticate(struct mg_http_message *hm) {
static void handle_login(struct mg_connection *c, struct user *u) { static void handle_login(struct mg_connection *c, struct user *u) {
char cookie[256]; char cookie[256];
const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie), mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: access_token=%s; Path=/; " "Set-Cookie: %s=%s; Path=/; "
"%sHttpOnly; SameSite=Lax; Max-Age=%d\r\n", "%sHttpOnly; SameSite=Lax; Max-Age=%d\r\n",
u->access_token, c->is_tls ? "Secure; " : "", 3600 * 24); cookie_name, u->access_token,
c->is_tls ? "Secure; " : "", 3600 * 24);
mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name)); mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name));
} }
static void handle_logout(struct mg_connection *c) { static void handle_logout(struct mg_connection *c) {
char cookie[256]; char cookie[256];
const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie), mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: access_token=; Path=/; " "Set-Cookie: %s=; Path=/; "
"Expires=Thu, 01 Jan 1970 00:00:00 UTC; " "Expires=Thu, 01 Jan 1970 00:00:00 UTC; "
"%sHttpOnly; Max-Age=0; \r\n", "%sHttpOnly; Max-Age=0; \r\n", cookie_name,
c->is_tls ? "Secure; " : ""); c->is_tls ? "Secure; " : "");
mg_http_reply(c, 200, cookie, "true\n"); mg_http_reply(c, 200, cookie, "true\n");
} }

View File

@ -156,19 +156,23 @@ static struct user *authenticate(struct mg_http_message *hm) {
static void handle_login(struct mg_connection *c, struct user *u) { static void handle_login(struct mg_connection *c, struct user *u) {
char cookie[256]; char cookie[256];
const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie), mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: access_token=%s;Path=/;" "Set-Cookie: %s=%s;Path=/;"
"HttpOnly;SameSite=Lax;Max-Age=%d\r\n", "%sHttpOnly;SameSite=Lax;Max-Age=%d\r\n", cookie_name,
u->access_token, 3600 * 24); u->access_token, c->is_tls ? "Secure; " : "", 3600 * 24);
mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name)); mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name));
} }
static void handle_logout(struct mg_connection *c) { static void handle_logout(struct mg_connection *c) {
mg_http_reply(c, 200, char cookie[256];
"Set-Cookie: access_token=; Path=/; " const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: %s=; Path=/; "
"Expires=Thu, 01 Jan 1970 00:00:00 UTC; " "Expires=Thu, 01 Jan 1970 00:00:00 UTC; "
"Secure; HttpOnly; Max-Age=0; \r\n", "%sHttpOnly; Max-Age=0; \r\n", cookie_name,
"true\n"); c->is_tls ? "Secure; " : "");
mg_http_reply(c, 200, cookie, "true\n");
} }
static void handle_debug(struct mg_connection *c, struct mg_http_message *hm) { static void handle_debug(struct mg_connection *c, struct mg_http_message *hm) {

View File

@ -78,19 +78,22 @@ static struct user *authenticate(struct mg_http_message *hm) {
static void handle_login(struct mg_connection *c, struct user *u) { static void handle_login(struct mg_connection *c, struct user *u) {
char cookie[256]; char cookie[256];
const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie), mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: access_token=%s; Path=/; " "Set-Cookie: %s=%s; Path=/; "
"%sHttpOnly; SameSite=Lax; Max-Age=%d\r\n", "%sHttpOnly; SameSite=Lax; Max-Age=%d\r\n",
u->access_token, c->is_tls ? "Secure; " : "", 3600 * 24); cookie_name, u->access_token,
c->is_tls ? "Secure; " : "", 3600 * 24);
mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name)); mg_http_reply(c, 200, cookie, "{%m:%m}", MG_ESC("user"), MG_ESC(u->name));
} }
static void handle_logout(struct mg_connection *c) { static void handle_logout(struct mg_connection *c) {
char cookie[256]; char cookie[256];
const char *cookie_name = c->is_tls ? "secure_access_token" : "access_token";
mg_snprintf(cookie, sizeof(cookie), mg_snprintf(cookie, sizeof(cookie),
"Set-Cookie: access_token=; Path=/; " "Set-Cookie: %s=; Path=/; "
"Expires=Thu, 01 Jan 1970 00:00:00 UTC; " "Expires=Thu, 01 Jan 1970 00:00:00 UTC; "
"%sHttpOnly; Max-Age=0; \r\n", "%sHttpOnly; Max-Age=0; \r\n", cookie_name,
c->is_tls ? "Secure; " : ""); c->is_tls ? "Secure; " : "");
mg_http_reply(c, 200, cookie, "true\n"); mg_http_reply(c, 200, cookie, "true\n");
} }