![]() |
libwebsockets
Lightweight C library for HTML5 websockets
|
Enable at CMake with -DLWS_WITH_GENERIC_SESSIONS=1
This also needs sqlite3 (libsqlite3-dev or similar package)
The generic-sessions protocol plugin provides cookie-based login authentication for lws web and ws connections.
The plugin handles everything about generic account registration, email verification, lost password, account deletion, and other generic account management.
Other code, in another eg, ws protocol handler, only needs very high-level state information from generic-sessions, ie, which user the client is authenticated as. Everything underneath is managed in generic-sessions.
Only three steps are needed to integrate lwsgs in your HTML.
1) lwsgs HTML UI is bundled with the javascript it uses in lwsgs.js
, so import that script file in your head section
2) define an empty div of id "lwsgs" somewhere
3) Call lwsgs_initial() in your page
That's it. An example is below
When the protocol is initialized, it gets per-vhost information from the config, such as where the sqlite3 databases are to be stored. The admin username and sha-1 of the admin password are also taken from here.
In the mounts using protocol-generic-sessions, a cookie is maintained against any requests; if no cookie was active on the initial request a new session is created with no attached user.
So there should always be an active session after any transactions with the server.
In the example html going to the mount /lwsgs loads a login / register page as the default.
The <form> in the login page contains 'next url' hidden inputs that let the html 'program' where the form handler will go after a successful admin login, a successful user login and a failed login.
After a successful login, the sqlite record at the server for the current session is updated to have the logged-in username associated with it.
"auth-mask" defines the authorization sector bits that must be enabled on the session to gain access.
"auth-mask" 0 is the default.
Note that the name of the real application protocol that uses generic-sessions is used, not generic-sessions itself.
The vhost configures the storage dir, admin credentials and session cookie lifetimes:
The email- related settings control generation of automatic emails for registration and forgotten password.
email-from
: The email address automatic emails are sent fromemail-smtp-ip
: Normally 127.0.0.1, if you have a suitable server on port 25 on your lan you can use this instead here.email-expire
: Seconds that links sent in email will work before being deletedemail-helo
: HELO to use when communicating with your SMTP serveremail-contact-person
: mentioned in the automatic emails as a human who can answer questionsemail-confirm-url-base
: the URL to start links with in the emails, so the recipient can get back to the web serverThe real protocol that makes use of generic-sessions must also be listed and any configuration it needs given
Notice the real application uses his own sqlite db, no details about how generic-sessions works or how it stores data are available to it.
You can also define a per-vhost confounder shown in the example above, used when aggregating the password with the salt when it is hashed. Any attacker will also need to get the confounder along with the database, which you can make harder by making the config dir only eneterable / readable by root.
You will have to prepare the db directory so it's suitable for the lwsws user to use, that usually means apache, eg
lwsgs will can send emails by talking to an SMTP server on localhost:25. That will usually be sendmail or postfix, you should confirm that works first by itself using the mail
application to send on it.
lwsgs has been tested on stock Fedora sendmail and postfix.
lwsgs is designed to provide sessions and accounts in a standalone and generic way.
But it's not useful by itself, there will always be the actual application who wants to make use of generic-sessions features.
We provide the "messageboard" plugin as an example of how to integrate with your actual application protocol.
The basic approach is the 'real' protocol handler (usually a plugin itself) subclasses the generic-sessions plugin and calls through to it by default.
The "real" protocol handler entirely deals with ws-related stuff itself, since generic-sessions does not use ws. But for
the "real" protocol handler checks if it recognizes the activity (eg, his own POST form URL) and if not, passes stuff through to the generic-sessions protocol callback to handle it. To simplify matters the real protocol can just pass through any unhandled messages to generic-sessions.
The "real" protocol can get a pointer to generic-sessions protocol on the same vhost using
The "real" protocol must also arrange generic-sessions per_session_data in his own per-session allocation. To allow keeping generic-sessions opaque, the real protocol must allocate that space at runtime, using the pss size the generic-sessions protocol struct exposes
The allocation reserved for generic-sessions is then used as user_space when the real protocol calls through to the generic-sessions callback
In that way the "real" protocol can subclass generic-sessions functionality.
To ease management of these secondary allocations, there are callbacks that occur when a wsi binds to a protocol and when the binding is dropped. These should be used to malloc and free and kind of per-connection secondary allocations.
#section gsapsib Getting session-specific information from another protocol
At least at the time when someone tries to upgrade an http(s) connection to ws(s) with your real protocol, it is necessary to confirm the cookie the http(s) connection has with generic-sessions and find out his username and other info.
Generic sessions lets another protocol check it again by calling his callback, and lws itself provides a generic session info struct to pass the related data
After the call to generic-sessions, the results can be
the real protocol can use this to reject attempts to open ws connections from http connections that are not authenticated; afterwards there's no need to check the ws connection auth status again.