cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
Plugins and classes to authenticate users. More...
Classes | |
class | Cutelyst::Authentication |
Main class to manage user authentication. More... | |
class | Cutelyst::AuthenticationCredential |
Abstract class to validate authentication credentials like user name and password. More... | |
class | Cutelyst::AuthenticationRealm |
Combines user store and credential validation into a named realm. More... | |
class | Cutelyst::AuthenticationStore |
Abstract class to retrieve user data from a store. More... | |
class | Cutelyst::AuthenticationUser |
Container for user data retrieved from an AuthenticationStore. More... | |
class | Cutelyst::CredentialHttp |
Use HTTP basic authentication to authenticate a user. More... | |
class | Cutelyst::CredentialPassword |
Use password based authentication to authenticate a user. More... | |
class | Cutelyst::StoreHtpasswd |
Authentication data store using a flat file. More... | |
class | Cutelyst::StoreMinimal |
Minimal in memory authentication data store. More... | |
The way to manage user authentication in Cutelyst is to combine a AuthenticationStore that provides the user data from for example a database together with a AuthenticationCredential validator that checks the password or other information to legitimate a user in an AuthenticationRealm that is added to the main plugin class Authentication in your application’s init() method.
You can add multpiple AuthenticationRealms to be used for different parts of your application. We will use one realm for the website and one for API access in this example. We will assume that you store your user data in a MySQL/MariaDB database with the following layout for your users
table:
Row | Type |
---|---|
id | INTEGER PRIMARY KEY |
username | TEXT |
password | TEXT |
email_address | TEXT |
first_name | TEXT |
last_name | TEXT |
active | INTEGER |
In order to persist the authenticated user over multiple requests, you also need the Session plugin. Include it together with the Authentication plugin in your CMakeLists.txt file. For this example we also need the Cutelyst::Sql plugin.
In order to get the user data from the database we have to create our own subclass of AuthenticationStore and reimplement the findUser() method. We will call it AuthStoreSql in this example.
The header file authstoresql.h looks like the following:
Our implementation in authstoresql.cpp:
We now have to glue all the stuff together in our application’s init method. Example myapp.cpp:
Let’s assume you have an admin area below /admin
and API routes below /api
. Your admin area login form is at /admin/login
.
In your admin controller header admin.h:
Implementation in admin.cpp:
Our API root controller api.h:
Implementation in api.cpp: