Actix Database Identity Provider
SQL database (diesel) integration for actix framework
Description
Implements a SQL backend for Actix-Web's identity middleware. Does not perform any authentication, only "remembers" the user when told to. The returned header containing the authorized token is configurable via the SqlIdentityBuilder::response_header()
method.
Normal server application flow:
- Application authenticates a user
- Application calls
remember()
with a user-identifiable string - SQL identity middleware generates a new token and embeds it in the response header
- Application returns response (with header)
From here, normal flow according to identity middleware guide can be followed
Normal client application flow:
- Client POSTs to a login endpoint, is authorized by server
- Client receives response, extracts the authorization token in the specified header
- On future requests (including logout), the client builds a bearer authentication header with the returned token
SQL Variants supported
- SQLite
- MySQL
- Postgres
Features
Default: SQLite, MySQL, Postgres
sqlite: Include SQLite support
mysql: Include MySQL support
postgres: Include PostgreSQL supprt
Database Requirements
This crate requires a table named identities with the following fields:
Field | Type | Constraints | Description |
---|---|---|---|
token | CHAR(32) | PRIMARY KEY, NOT NULL, UNIQUE | The auto-generated token field, will be used to lookup user |
userid | TEXT | NOT NULL | The user id to remember, probably a key in another table |
ip | TEXT | The IP the user most recently connected from | |
useragent | TEXT | The user-agent of the most recent connection | |
created | TIMESTAMP | NOT NULL | Timestamp (w/out timezone) this token was created |
modified | TIMESTAMP | NOT NULL | Timestamp (w/out timezone) this token was last used |
Example SQL files for SQLite, MySQL, and PostgreSQL are available int the sql/ folder on the repository
Server Example
extern crate actix_web;
extern crate actix_web_sql_identity;
use ;
use ;
use SqlIdentityBuilder;
const POOL_SIZE: usize = 3; // Number of connections per pool
Client Example
extern crate reqwest;
extern crate hyper;
use ;
use ;
// Build our custom header that will contain our returned token
header!
/// Builds a GET request to send to the server, with optional authentication
///
/// # Arguments
///
/// * `client` - Client to build request with
/// * `uri` - Endpoint to target (e.g., /index, /profile, etc)
/// * `token` - An optional authentication token
/// Builds a POST request to send to the server, with optional authentication
///
/// # Arguments
///
/// * `client` - Client to build request with
/// * `uri` - Endpoint to target (e.g., /login, /logout)
/// * `token` - An optional authentication token
/// Pretty print a response
///
/// # Arguments
///
/// * `resp` - Response to print
License
BSD 3-Clause
Author
Kevin Allison kvnallsn AT gmail.com