Skip to content

Commit 26a971c

Browse files
committed
oauth: fix 404
1 parent d46b6c7 commit 26a971c

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

plugins/protocol_lws_auth_server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The plugin can be enabled on any vhost. Its behavior is customized using Per-Vho
4545

4646
You can enable this plugin on a vhost without writing any C code at all by supplying a standard JSON configuration to `lwsws` or any LWS server parsing `lejp-conf`.
4747

48-
This example mounts the front-end UI at `/auth` and configures the `lws-auth-server` protocol with its required PVOs:
48+
This example mounts the front-end UI at `/` (the assets dir with `index.html` as the default) and configures the `lws-auth-server` protocol with its required PVOs. Note the UI must live at the root of the auth vhost: `/api/authorize` redirects anonymous inbound OAuth2 logins to `/?client_id=...` (with the OAuth2 params preserved in the query for `auth.js` to replay after login), the same URL shape `lws-login`'s `login_url` convention uses.
4949

5050
```json
5151
{

plugins/protocol_lws_auth_server/protocol_lws_auth_server.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,7 @@ callback_auth_server(struct lws *wsi, enum lws_callback_reasons reason,
34073407
if (!strncmp((const char *)in, "/authorize", 10)) {
34083408
char client_id[128] = {0}, redirect_uri[256] = {0}, response_type[16] = {0}, state[128] = {0};
34093409
char code_challenge[128] = {0}, code_challenge_method[16] = {0};
3410+
char service_name[128] = {0};
34103411

34113412
if (lws_get_urlarg_by_name_safe(wsi, "client_id=", client_id, sizeof(client_id)) < 0 ||
34123413
lws_get_urlarg_by_name_safe(wsi, "redirect_uri=", redirect_uri, sizeof(redirect_uri)) < 0) {
@@ -3476,7 +3477,6 @@ callback_auth_server(struct lws *wsi, enum lws_callback_reasons reason,
34763477
* JWT, so this works for the refresh-session fallback path
34773478
* where there is no valid JWT to inspect.
34783479
*/
3479-
char service_name[128] = {0};
34803480
lws_get_urlarg_by_name_safe(wsi, "service_name=",
34813481
service_name, sizeof(service_name));
34823482
if (service_name[0]) {
@@ -3501,10 +3501,34 @@ callback_auth_server(struct lws *wsi, enum lws_callback_reasons reason,
35013501
}
35023502

35033503
if (!session_uid) {
3504-
char loc[1024];
3505-
/* lws_urlencode is typically available, but if not we assume frontend can parse mostly raw */
3506-
lws_snprintf(loc, sizeof(loc), "/auth?client_id=%s&redirect_uri=%s&response_type=code&state=%s&code_challenge=%s&code_challenge_method=%s",
3507-
client_id, redirect_uri, state, code_challenge, code_challenge_method);
3504+
/*
3505+
* Anonymous: bounce to the login UI with the OAuth2
3506+
* params preserved in the query, so auth.js can replay
3507+
* them to /api/authorize (or /api/login) after the user
3508+
* authenticates. The UI lives at the auth server root
3509+
* ("/", the index.html filesystem mount -- the same URL
3510+
* lws-login's login_url convention points at); the old
3511+
* "/auth?..." target assumed a mount that does not
3512+
* exist in the standard layout and 404'd the whole
3513+
* login flow. Values are percent-encoded: they came
3514+
* decoded out of the request urlargs and are replayed
3515+
* verbatim into the next Location:, so a '&' or '#'
3516+
* inside any of them must not be able to reshape the
3517+
* query.
3518+
*/
3519+
char enc_redirect[768], enc_state[384], enc_cc[384];
3520+
char enc_ccm[48], enc_sn[384];
3521+
char loc[2048];
3522+
3523+
lws_urlencode(enc_redirect, redirect_uri, (int)sizeof(enc_redirect));
3524+
lws_urlencode(enc_state, state, (int)sizeof(enc_state));
3525+
lws_urlencode(enc_cc, code_challenge, (int)sizeof(enc_cc));
3526+
lws_urlencode(enc_ccm, code_challenge_method, (int)sizeof(enc_ccm));
3527+
lws_urlencode(enc_sn, service_name, (int)sizeof(enc_sn));
3528+
3529+
lws_snprintf(loc, sizeof(loc), "/?client_id=%s&redirect_uri=%s&response_type=code&state=%s&code_challenge=%s&code_challenge_method=%s%s%s",
3530+
client_id, enc_redirect, enc_state, enc_cc, enc_ccm,
3531+
service_name[0] ? "&service_name=" : "", enc_sn);
35083532

35093533
uint8_t hdr_buf[8192 + LWS_PRE];
35103534
uint8_t *h_start = hdr_buf + LWS_PRE;

0 commit comments

Comments
 (0)