Add actix-identity crate
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
14
src/main.rs
14
src/main.rs
@ -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)
|
||||
|
Reference in New Issue
Block a user