Skip to content

Add set_basic_auth and set_bearer_token_auth to WebSocketClient - #2652

Merged
yhirose merged 1 commit into
yhirose:masterfrom
Hyukya:feature/websocket-client-auth
Oct 28, 2026
Merged

yhirose merged 1 commit into
yhirose:masterfrom
Hyukya:feature/websocket-client-auth

Conversation

@Hyukya

@Hyukya Hyukya commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Add set_basic_auth() and set_bearer_token_auth() to WebSocketClient, the same origin credential setters Client has.

The credentials are sent as the Authorization header of the upgrade request on every connect(). Through a proxy, they are sent only on the request inside the tunnel, never on the CONNECT request.

Problem

ClientImpl and SSLClient let callers set origin credentials with setters, but WebSocketClient could only take an Authorization header through its constructor, and there is no other API to change its headers.

This meant:

  • Reconnecting with a new token required creating a new client.
  • Callers had to build the Basic credentials (base64(user:pass)) themselves.
  • The same credentials had to be set differently on the HTTP and WebSocket clients.

Changes

  • Add WebSocketClient::set_basic_auth() and set_bearer_token_auth().
  • Add the Authorization header to the upgrade request in prepare_default_headers() through detail::add_authorization_header().
    • As in ClientImpl, Basic is sent when a username or password is set, otherwise Bearer.
    • An Authorization header already in the constructor headers is kept as is.
    • The current values are read on every connect(), so a change takes effect on the next reconnect.
  • The CONNECT request built in connect_with_proxy() is unchanged and still carries only the set_proxy_*_auth() credentials.
  • Document the two setters in the API list of README-websocket.md and add an Authentication section.
Setting Authorization on the upgrade request inside the tunnel CONNECT request
set_bearer_token_auth("t") Bearer t none
set_basic_auth("u", "p") Basic dTpw none
Both Basic and Bearer Basic none
Constructor Authorization + setter the constructor value none

Tests

Add coverage for:

  • WebSocketClient over a direct connection:
    • Basic and Bearer credentials are sent on the upgrade request.
    • Basic credentials are sent even with an empty password.
    • Basic is sent when both are set.
    • A constructor Authorization header wins over the setters.
    • Changed credentials are sent on reconnect.
  • WebSocketClient through a proxy:
    • For both Basic and Bearer, the origin credentials reach only the request inside the tunnel, and the proxy credentials only the CONNECT request.

The existing proxy credential tests are unchanged; the new ones use a separate helper.

ClientImpl and SSLClient let callers set origin credentials with
set_basic_auth()/set_bearer_token_auth(), but WebSocketClient could only
take a fixed Authorization header through its constructor. Add both
setters: each connect() adds the Authorization header to the upgrade
request from the current credentials (Basic over Bearer), unless the
constructor headers already carry one, and never to the proxy's CONNECT
request.
@yhirose
yhirose merged commit a0ab09f into yhirose:master Oct 28, 2026
27 of 28 checks passed
@yhirose

yhirose commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks again!

@Hyukya

Hyukya commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

@AhnLab-OSS @AhnLab-OSSG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants