Skip to content

Commit 11af10f

Browse files
committed
h2: POST: deal with basic auth
1 parent 0f77f31 commit 11af10f

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

lib/roles/h2/ops-h2.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,20 @@ lws_h2_bind_for_post_before_action(struct lws *wsi)
866866

867867
if (lws_bind_protocol(wsi, pp, __func__))
868868
return 1;
869+
#if defined(LWS_WITH_HTTP_BASIC_AUTH)
870+
/* basic auth? */
871+
872+
switch (lws_check_basic_auth(wsi, hit->basic_auth_login_file,
873+
hit->auth_mask & AUTH_MODE_MASK)) {
874+
case LCBA_CONTINUE:
875+
break;
876+
case LCBA_FAILED_AUTH:
877+
return lws_unauthorised_basic_auth(wsi);
878+
case LCBA_END_TRANSACTION:
879+
lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
880+
return lws_http_transaction_completed(wsi);
881+
}
882+
#endif
869883
}
870884

871885
methidx = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);

lib/roles/http/server/server.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,30 @@ lws_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len)
12001200

12011201
#if defined(LWS_WITH_HTTP_BASIC_AUTH)
12021202

1203+
static int
1204+
lws_authorization_rewrite(struct lws *wsi, const char *name, size_t len)
1205+
{
1206+
char *p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION);
1207+
int fi = wsi->http.ah->frag_index[WSI_TOKEN_HTTP_AUTHORIZATION];
1208+
1209+
if (!p)
1210+
return 1;
1211+
1212+
wsi->http.ah->frags[fi].len = (uint16_t)len;
1213+
strncpy(p, name, (unsigned int)len);
1214+
p[len] = '\0';
1215+
1216+
return 0;
1217+
}
1218+
12031219
enum lws_check_basic_auth_results
12041220
lws_check_basic_auth(struct lws *wsi, const char *basic_auth_login_file,
12051221
unsigned int auth_mode)
12061222
{
12071223
#if defined(LWS_WITH_FILE_OPS)
12081224
char b64[160], plain[(sizeof(b64) * 3) / 4], *pcolon;
12091225
int m, ml, fi, bar;
1226+
size_t le;
12101227

12111228
if (!basic_auth_login_file && auth_mode == LWSAUTHM_DEFAULT)
12121229
return LCBA_CONTINUE;
@@ -1276,13 +1293,12 @@ lws_check_basic_auth(struct lws *wsi, const char *basic_auth_login_file,
12761293
* authorized username
12771294
*/
12781295

1279-
*pcolon = '\0';
1280-
wsi->http.ah->frags[fi].len = (uint16_t)lws_ptr_diff_size_t(pcolon, &plain[0]);
1281-
pcolon = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION);
1282-
strncpy(pcolon, plain, (unsigned int)(ml - 1));
1283-
pcolon[ml - 1] = '\0';
1284-
lwsl_info("%s: basic auth accepted for %s\n", __func__,
1285-
lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION));
1296+
le = lws_ptr_diff_size_t(pcolon, plain);
1297+
plain[le] = '\0';
1298+
lws_authorization_rewrite(wsi, plain, le);
1299+
1300+
lwsl_wsi_info(wsi, "%s: basic auth accepted for '%s'\n", __func__,
1301+
lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION));
12861302

12871303
return LCBA_CONTINUE;
12881304
#else

0 commit comments

Comments
 (0)