axum_session_auth-0.6.0 has been yanked.
Library to Provide a User Authentication and privilege Token Checks. It requires the Axum_Session library.
This library will help by making it so User ID or authorizations are not stored on the Client side but rather on the Server side.
The Authorization is linked by the Clients Serverside Session ID which is stored on the Client side. Formally known as Axum Sessions Auth

- Wraps
axum_session for data management serverside.
- Right Management API
- Auto load of user Data upon Page loads.
- User Data cache to Avoid Repeated Database calls when not needed.
Help
If you need help with this library or have suggestions please go to our Discord Group
Install
Sessions Authentication uses tokio runtime and ['axum_session'];
[dependencies]
axum_session_auth = { version = "0.6.0", features = [ "postgres-rustls" ] }
Cargo Feature Flags
default: [postgres-rustls]
sqlite-rustls: Sqlx support for the self-contained SQLite database engine and rustls.
sqlite-native: Sqlx support for the self-contained SQLite database engine and native-tls.
postgres-rustls: Sqlx support for the Postgres database server and rustls.
postgres-native: Sqlx support for the Postgres database server and native-tls.
mysql-rustls: Sqlx support for the MySQL/MariaDB database server and rustls.
mysql-native: Sqlx support for the MySQL/MariaDB database server and native-tls.
redis-db: redis_pool 0.2.1 session support. Enables Redis Client Pool
redis-clusterdb: redis_pool 0.2.0 session support. Enabled Redis ClusterClient Pool.
surrealdb-rocksdb: 1.0.0 support for rocksdb.
surrealdb-tikv : 1.0.0 support for tikv.
surrealdb-fdb-?_? : 1.0.0 support for fdb versions 6_1, 6_2, 6_3, 7_0, 7_1. Replace ?_? with version.
surrealdb-mem : 1.0.0 support for mem.
mongo : mongodb 2.6.1 support for mongo.
Example
use sqlx::{PgPool, ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{SessionPgPool, Session, SessionConfig, SessionLayer, DatabasePool};
use axum_session_auth::{AuthSession, AuthSessionLayer, Authentication, AuthConfig};
use axum::{
Router,
routing::get,
};
#[tokio::main]
async fn main() {
# async {
let poll = connect_to_database().await.unwrap();
let session_config = SessionConfig::default()
.with_database("test")
.with_table_name("test_table");
let auth_config = AuthConfig::<i64>::default().with_anonymous_user_id(Some(1));
let session_store = SessionStore::<SessionPgPool>::new(Some(poll.clone().into()), session_config);
let app = Router::new()
.route("/greet/:name", get(greet))
.layer(SessionLayer::new(session_store))
.layer(AuthSessionLayer::<User, i64, SessionPgPool, PgPool>::new(Some(poll)).with_config(auth_config));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
# };
}
async fn greet(method: Method, auth: AuthSession<User, i64, SessionPgPool, PgPool>) -> &'static str {
let mut count: usize = auth.session.get("count").unwrap_or(0);
let current_user = auth.current_user.clone().unwrap_or_default();
count += 1;
auth.session.set("count", count);
auth.cache_clear_user(1).await;
auth.cache_clear_all().await;
if !Auth::<User, i64, PgPool>::build([Method::Get], false)
.requires(Rights::none([
Rights::permission("Token::UseAdmin"),
Rights::permission("Token::ModifyPerms"),
]))
.validate(¤t_user, &method, None)
.await
{
return format!("No Permissions! for {}", current_user.username)[];
}
if !auth.is_authenticated() {
auth.login_user(2);
auth.remember_user(true);
"You have Logged in! Please Refreash the page to display the username and counter."
} else {
format!("{}-{}", current_user.username, count)[..]
};
}
#[derive(Clone, Debug)]
pub struct User {
pub id: i32,
pub anonymous: bool,
pub username: String,
}
#[async_trait]
impl HasPermission<PgPool> for User {
async fn has(&self, perm: &String, _pool: &Option<&PgPool>) -> bool {
match &perm[..] {
"Token::UseAdmin" => true,
"Token::ModifyUser" => true,
_ => false,
}
}
}
#[async_trait]
impl Authentication<User, i64, PgPool> for User {
async fn load_user(userid: i64, _pool: Option<&PgPool>) -> Result<User> {
Ok(User {
id: userid,
anonymous: true,
username: "Guest".to_string(),
})
}
fn is_authenticated(&self) -> bool {
!self.anonymous
}
fn is_active(&self) -> bool {
!self.anonymous
}
fn is_anonymous(&self) -> bool {
self.anonymous
}
}
async fn connect_to_database() -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
# unimplemented!()
}