Add actix-identity crate

This commit is contained in:
2022-03-30 16:58:00 +02:00
parent 6fdac7fbb1
commit eab9bdf7f5
6 changed files with 181 additions and 4 deletions

View File

@ -1,4 +1,5 @@
use actix::Addr;
use actix_identity::Identity;
use actix_web::{HttpResponse, Responder, web};
use askama::Template;
@ -30,7 +31,8 @@ pub struct LoginRequest {
/// Authenticate user
pub async fn login_route(users: web::Data<Addr<UsersActor>>,
req: Option<web::Form<LoginRequest>>) -> impl Responder {
req: Option<web::Form<LoginRequest>>,
id: Identity) -> impl Responder {
let mut danger = String::new();
let mut login = String::new();
@ -42,6 +44,7 @@ pub async fn login_route(users: web::Data<Addr<UsersActor>>,
password: req.password.clone(),
}).await.unwrap();
// TODO : save auth in case of successful authentication
danger = format!("{:?}", response)
}

View File

@ -15,6 +15,14 @@ pub struct AppConfig {
/// Storage path
#[clap(short, long, env)]
pub storage_path: String,
/// App token token
#[clap(short, long, env, default_value = "")]
pub token_key: String,
/// Should the auth cookie be secure
#[clap(long, env)]
pub secure_auth_cookie: bool,
}
impl AppConfig {

View File

@ -10,6 +10,7 @@ use basic_oidc::data::entity_manager::EntityManager;
use basic_oidc::data::user::{hash_password, User};
use basic_oidc::actors::users_actor::UsersActor;
use actix::Actor;
use actix_identity::{IdentityService, CookieIdentityPolicy};
#[get("/health")]
async fn health() -> &'static str {
@ -20,7 +21,12 @@ async fn health() -> &'static str {
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let config: AppConfig = AppConfig::parse();
let mut config: AppConfig = AppConfig::parse();
// In debug mode only, use dummy token
if cfg!(debug_assertions) && config.token_key.is_empty() {
config.token_key = String::from_utf8_lossy(&[32; 32]).to_string();
}
if !config.storage_path().exists() {
log::error!(
@ -55,10 +61,16 @@ async fn main() -> std::io::Result<()> {
log::info!("Server will listen on {}", config.listen_address);
HttpServer::new(move || {
let policy = CookieIdentityPolicy::new(config.token_key.as_bytes())
.name("auth-cookie")
.secure(config.secure_auth_cookie);
App::new()
.app_data(web::Data::new(users_actor.clone()))
.wrap(Logger::default())
.wrap(IdentityService::new(policy))
// /health route
.service(health)