actix-admin/examples/azure_auth/main.rs

199 lines
6.3 KiB
Rust
Raw Normal View History

2022-08-06 20:09:30 +02:00
extern crate serde_derive;
use actix_admin::prelude::*;
use actix_session::{Session, SessionMiddleware, storage::CookieSessionStore};
use actix_web::{cookie::Key, web, App, HttpResponse, HttpServer, middleware};
use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo};
use oauth2::basic::BasicClient;
use oauth2::RedirectUrl;
use sea_orm::{ConnectOptions, DatabaseConnection};
use std::env;
use std::time::Duration;
use tera::{Context, Tera};
2022-08-29 13:06:44 +02:00
use actix_web::Error;
2022-08-06 20:09:30 +02:00
mod entity;
use entity::{Post, Comment};
#[derive(Clone)]
2022-08-06 20:09:30 +02:00
pub struct AppState {
pub oauth: BasicClient,
pub tmpl: Tera,
pub db: DatabaseConnection,
pub actix_admin: ActixAdmin,
}
impl ActixAdminAppDataTrait for AppState {
fn get_db(&self) -> &DatabaseConnection {
&self.db
}
fn get_actix_admin(&self) -> &ActixAdmin {
&self.actix_admin
}
}
2022-08-29 13:06:44 +02:00
trait AppDataTrait {
fn get_tmpl(&self) -> &Tera;
}
impl AppDataTrait for AppState {
fn get_tmpl(&self) -> &Tera {
&self.tmpl
}
}
2022-08-06 20:09:30 +02:00
impl AzureAuthAppDataTrait for AppState {
fn get_oauth(&self) -> &BasicClient {
&self.oauth
}
}
async fn custom_handler<
2022-08-29 13:06:44 +02:00
T: ActixAdminAppDataTrait + AppDataTrait,
E: ActixAdminViewModelTrait,
>(
2022-08-29 13:06:44 +02:00
session: Session,
data: web::Data<T>,
_text: String
2022-08-29 13:06:44 +02:00
) -> Result<HttpResponse, Error> {
let mut ctx = Context::new();
ctx.extend(get_admin_ctx(session, &data));
let body = data.get_tmpl()
.render("custom_handler.html", &ctx).unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
2022-08-29 13:06:44 +02:00
async fn custom_index<
T: ActixAdminAppDataTrait + AppDataTrait
>(
session: Session,
data: web::Data<T>,
_text: String
) -> Result<HttpResponse, Error> {
let mut ctx = Context::new();
ctx.extend(get_admin_ctx(session, &data));
let body = data.get_tmpl()
.render("custom_index.html", &ctx).unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
2022-08-06 20:09:30 +02:00
async fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
let login = session.get::<UserInfo>("user_info").unwrap();
let web_auth_link = if login.is_some() {
2022-08-06 21:49:52 +02:00
"azure-auth/logout"
2022-08-06 20:09:30 +02:00
} else {
2022-08-06 21:49:52 +02:00
"azure-auth/login"
2022-08-06 20:09:30 +02:00
};
let mut ctx = Context::new();
ctx.insert("web_auth_link", web_auth_link);
let rendered = data.tmpl.render("index.html", &ctx).unwrap();
HttpResponse::Ok().body(rendered)
}
fn create_actix_admin_builder() -> ActixAdminBuilder {
let post_view_model = ActixAdminViewModel::from(Post);
let comment_view_model = ActixAdminViewModel::from(Comment);
2022-08-06 21:49:52 +02:00
let configuration = ActixAdminConfiguration {
2022-11-08 17:10:27 +01:00
enable_auth: true,
user_is_logged_in: Some(|session: &Session| -> bool {
let user_info = session.get::<UserInfo>("user_info").unwrap();
user_info.is_some()
}),
2022-08-27 14:41:08 +02:00
login_link: Some("/azure-auth/login".to_string()),
2022-12-29 19:56:20 +01:00
logout_link: Some("/azure-auth/logout".to_string()),
file_upload_directory: "./file_uploads"
2022-08-06 21:49:52 +02:00
};
let mut admin_builder = ActixAdminBuilder::new(configuration);
// admin_builder.add_custom_handler_for_index::<AppState>(
// web::get().to(custom_index::<AppState>)
// );
admin_builder.add_entity::<AppState, Post>(&post_view_model);
admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu", web::get().to(custom_index::<AppState>), true);
admin_builder.add_custom_handler("Custom Route not in Menu", "/custom_route_not_in_menu", web::get().to(custom_index::<AppState>), false);
let some_category = "Some Category";
admin_builder.add_entity_to_category::<AppState, Comment>(&comment_view_model, some_category);
admin_builder.add_custom_handler_for_entity_in_category::<AppState, Comment>(
"My custom handler",
"/custom_handler",
web::get().to(custom_handler::<AppState, Comment>),
some_category,
true
);
2022-08-06 20:09:30 +02:00
admin_builder
}
#[actix_rt::main]
async fn main() {
2022-12-29 19:56:20 +01:00
dotenv::from_filename("./examples/azure_auth/.env.example").ok();
dotenv::from_filename("./examples/azure_auth/.env").ok();
2022-09-02 17:23:06 +02:00
2022-11-08 17:10:27 +01:00
let oauth2_client_id = env::var("OAUTH2_CLIENT_ID").expect("Missing the OAUTH2_CLIENT_ID environment variable.");
let oauth2_client_secret = env::var("OAUTH2_CLIENT_SECRET").expect("Missing the OAUTH2_CLIENT_SECRET environment variable.");
let oauth2_server= env::var("OAUTH2_SERVER").expect("Missing the OAUTH2_SERVER environment variable.");
2022-09-02 17:23:06 +02:00
2022-08-06 20:09:30 +02:00
let azure_auth = AzureAuth::new(&oauth2_server, &oauth2_client_id, &oauth2_client_secret);
// Set up the config for the OAuth2 process.
let client = azure_auth
.clone()
.get_oauth_client()
// This example will be running its own server at 127.0.0.1:5000.
.set_redirect_uri(
2022-08-06 21:49:52 +02:00
RedirectUrl::new("http://localhost:5000/azure-auth/auth".to_string())
2022-08-06 20:09:30 +02:00
.expect("Invalid redirect URL"),
);
2022-08-29 13:06:44 +02:00
let mut tera = Tera::parse(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
tera.extend(&TERA).unwrap();
let _tera_res = tera.build_inheritance_chains();
2022-08-06 20:09:30 +02:00
2022-09-02 17:23:06 +02:00
let db_url = "sqlite::memory:".to_string();
2022-08-06 20:09:30 +02:00
let mut opt = ConnectOptions::new(db_url);
opt.max_connections(100)
.min_connections(5)
.connect_timeout(Duration::from_secs(8))
.idle_timeout(Duration::from_secs(8))
.sqlx_logging(true);
let conn = sea_orm::Database::connect(opt).await.unwrap();
let _ = entity::create_post_table(&conn).await;
let cookie_secret_key = Key::generate();
2022-08-06 20:09:30 +02:00
HttpServer::new(move || {
2022-11-08 17:10:27 +01:00
let actix_admin_builder = create_actix_admin_builder();
let app_state = AppState {
oauth: client.clone(),
tmpl: tera.clone(),
db: conn.clone(),
actix_admin: actix_admin_builder.get_actix_admin(),
};
2022-08-06 20:09:30 +02:00
App::new()
.app_data(web::Data::new(app_state.clone()))
.wrap(SessionMiddleware::new(CookieSessionStore::default(), cookie_secret_key.clone()))
2022-08-06 20:09:30 +02:00
.route("/", web::get().to(index))
.service(azure_auth.clone().create_scope::<AppState>())
.service(
2022-11-08 17:10:27 +01:00
actix_admin_builder.get_scope::<AppState>()
2022-08-06 20:09:30 +02:00
)
.wrap(middleware::Logger::default())
})
.bind("127.0.0.1:5000")
.expect("Can not bind to port 5000")
.run()
.await
.unwrap();
}