[Libwebsockets] HTTP basic authorization

Andy Green andy at warmcat.com
Fri Oct 30 23:37:32 CET 2015



On 31 October 2015 00:46:26 GMT+08:00, "Elmar Berghöfer" <elmar.berghoefer at dfki.de> wrote:
>Hello,
>
>I am trying to use libwebsocket to write a client that catches some 
>information from a server. Now in principal with a test server 
>everything worked fine, but now additionally a basic user authorization
>
>is required (on HTTP level). Additionally ssl is used, but that part 
>works so far.
>
>No I couldn't figure out how to add the authentification header since I
>
>couldn't found some option to give it to the lib in form of "username" 

No the library doesn't know about basic auth.  You would handle it by adding headers in your user code, similar to how the test server does cookies.

>and "password". In principal it would be enough if I could add an 
>arbitrary string to the header, I tried during the callback that let
>you 
>add something to the header during handshake but that doesn't seem to
>be 

Should be.

>enough (I am not familiar with websocket programming, but seems like
>the 
>header has to be included in each packet?).

No... after the handshake, the connection literally 'upgrades' away from http to the completely different binary ws protocol.  So there's no native way to send http headers on ws content.

You only need to send this one time in the http handshake, the server will either like it and do the ws upgrade or deny it and send a http unauthorized and close the connection.  If it did the upgrade, that state at the server and the individuality of tcp connections is enough to know it was authenticated.

>So is there a way to add the "Authorization: Basic <passwd in B64>" 
>somehow to the header hat is used by the lib, or alternatively specify
>a 
>username and password?

Yes just look at cookie-related code in the test server and roll your own.

Lws does provide some public helpers useful for this

LWS_VISIBLE LWS_EXTERN int
lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);

LWS_VISIBLE LWS_EXTERN int
lws_b64_decode_string(const char *in, char *out, int out_size);

-Andy

>Thanks
>
>Elmar Berghöfer




More information about the Libwebsockets mailing list