Fix paths, experiment with instance lookup

This commit is contained in:
eraden 2024-01-28 20:03:35 +01:00
parent 25390c0c56
commit 9617bf08b0
8 changed files with 34 additions and 11 deletions

View File

@ -1,7 +1,8 @@
use actix_web::{web::Data, FromRequest}; use actix_web::{guard::Guard, web::Data, FromRequest};
use derive_more::*; use derive_more::*;
use futures_util::{future::LocalBoxFuture, FutureExt}; use futures_util::{future::LocalBoxFuture, FutureExt};
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use tracing::warn;
#[derive(Debug, Display)] #[derive(Debug, Display)]
#[display(fmt = "{{\"error\":\"Instance is not configured\"}}")] #[display(fmt = "{{\"error\":\"Instance is not configured\"}}")]
@ -27,10 +28,12 @@ impl FromRequest for RequireInstance {
let db = req.app_data::<Data<DatabaseConnection>>().cloned(); let db = req.app_data::<Data<DatabaseConnection>>().cloned();
async move { async move {
let Some(db) = db else { let Some(db) = db else {
warn!("Failed to fetch database connection fot required instance configured");
return Err(NoInstance); return Err(NoInstance);
}; };
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
let Ok(Some(instance)) = entities::prelude::Instances::find().one(&**db).await else { let Ok(Some(instance)) = entities::prelude::Instances::find().one(&**db).await else {
warn!("No instance found");
return Err(NoInstance); return Err(NoInstance);
}; };
Ok(Self(instance)) Ok(Self(instance))
@ -53,13 +56,16 @@ impl FromRequest for RequireInstanceConfigured {
let db = req.app_data::<Data<DatabaseConnection>>().cloned(); let db = req.app_data::<Data<DatabaseConnection>>().cloned();
async move { async move {
let Some(db) = db else { let Some(db) = db else {
warn!("Failed to fetch database connection fot required instance configured");
return Err(NoInstance); return Err(NoInstance);
}; };
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
let Ok(Some(instance)) = entities::prelude::Instances::find().one(&**db).await else { let Ok(Some(instance)) = entities::prelude::Instances::find().one(&**db).await else {
warn!("No instance found");
return Err(NoInstance); return Err(NoInstance);
}; };
if !instance.is_setup_done { if !instance.is_setup_done {
warn!("Instance is not configured");
return Err(NoInstance); return Err(NoInstance);
} }
return Ok(Self(instance)); return Ok(Self(instance));

View File

@ -3,6 +3,7 @@ use crate::session::AppClaims;
use actix_jwt_session::{ use actix_jwt_session::{
Duration, Hashing, JwtTtl, RefreshTtl, SessionStorage, JWT_HEADER_NAME, REFRESH_HEADER_NAME, Duration, Hashing, JwtTtl, RefreshTtl, SessionStorage, JWT_HEADER_NAME, REFRESH_HEADER_NAME,
}; };
use actix_web::web::scope;
use actix_web::web::{Data, ServiceConfig}; use actix_web::web::{Data, ServiceConfig};
use actix_web::{HttpRequest, HttpResponse}; use actix_web::{HttpRequest, HttpResponse};
use entities::prelude::Users; use entities::prelude::Users;
@ -25,12 +26,16 @@ pub struct AuthResponseBody {
} }
pub fn configure(http_client: reqwest::Client, config: &mut ServiceConfig) { pub fn configure(http_client: reqwest::Client, config: &mut ServiceConfig) {
config config.service(
.service(email_check::email_check) scope("")
.service(sign_in::sign_in) .service(email_check::email_check)
.service(sign_up::sign_up) .service(sign_in::sign_in)
.service(sign_out::sign_out); .service(sign_up::sign_up)
social_auth::configure(http_client, config); .service(sign_out::sign_out)
.configure(|c| {
social_auth::configure(http_client, c);
}),
);
} }
#[derive(Debug, Clone, Copy, derive_more::Display)] #[derive(Debug, Clone, Copy, derive_more::Display)]

View File

@ -7,6 +7,7 @@ use sea_orm::prelude::*;
use sea_orm::Set; use sea_orm::Set;
use serde_json::json; use serde_json::json;
use crate::extractors::RequireInstanceConfigured;
use crate::models::{Error, JsonError}; use crate::models::{Error, JsonError};
use crate::session::AppClaims; use crate::session::AppClaims;
use crate::utils::extract_req_ip; use crate::utils::extract_req_ip;
@ -14,6 +15,7 @@ use crate::utils::extract_req_ip;
#[post("/sign-out")] #[post("/sign-out")]
pub async fn sign_out( pub async fn sign_out(
req: HttpRequest, req: HttpRequest,
_: RequireInstanceConfigured,
db: Data<DatabaseConnection>, db: Data<DatabaseConnection>,
claims: Authenticated<RefreshToken>, claims: Authenticated<RefreshToken>,
session: Data<SessionStorage>, session: Data<SessionStorage>,

View File

@ -35,6 +35,7 @@ use sea_orm::{
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use crate::{ use crate::{
extractors::RequireInstanceConfigured,
http::OAuthError, http::OAuthError,
models::{Error, JsonError}, models::{Error, JsonError},
}; };
@ -57,7 +58,7 @@ macro_rules! init_flow_service {
($provider: expr, $flow: expr) => { ($provider: expr, $flow: expr) => {
web::resource(std::concat!("/auth/", $provider)).get({ web::resource(std::concat!("/auth/", $provider)).get({
let flow = $flow.clone(); let flow = $flow.clone();
move || { move |_: RequireInstanceConfigured| {
let flow = flow.clone(); let flow = flow.clone();
async move { async move {
let flow = flow.clone(); let flow = flow.clone();
@ -77,6 +78,7 @@ macro_rules! flow_callback {
($provider: expr, $flow: expr) => {{ ($provider: expr, $flow: expr) => {{
web::resource(std::concat!("/auth/", $provider, "/callback")).get( web::resource(std::concat!("/auth/", $provider, "/callback")).get(
move |req: HttpRequest, move |req: HttpRequest,
_: RequireInstanceConfigured,
db: Data<DatabaseConnection>, db: Data<DatabaseConnection>,
event_bus: Data<crate::EventBusClient>, event_bus: Data<crate::EventBusClient>,
session: Data<actix_jwt_session::SessionStorage>| { session: Data<actix_jwt_session::SessionStorage>| {

View File

@ -7,6 +7,7 @@ use actix_web::{
use crate::config::ApplicationConfig; use crate::config::ApplicationConfig;
pub fn configure(config: &mut ServiceConfig) { pub fn configure(config: &mut ServiceConfig) {
// config.service(actix_web::web::resource("").guard(actix_web::guard::fn_guard(f)))
config.service(configs); config.service(configs);
} }

View File

@ -9,7 +9,7 @@ pub fn configure(http_client: reqwest::Client, config: &mut ServiceConfig) {
config.service( config.service(
scope("/api") scope("/api")
.configure(config::configure) .configure(config::configure)
.configure(users::configure), .configure(users::configure)
.configure(|c| authentication::configure(http_client, c)),
); );
authentication::configure(http_client, config);
} }

View File

@ -1,3 +1,4 @@
use crate::extractors::RequireInstanceConfigured;
use crate::session::AppClaims; use crate::session::AppClaims;
use actix_jwt_session::Authenticated; use actix_jwt_session::Authenticated;
use actix_web::get; use actix_web::get;
@ -12,7 +13,11 @@ pub fn configure(config: &mut ServiceConfig) {
} }
#[get("/me")] #[get("/me")]
async fn retrieve(session: Authenticated<AppClaims>, db: Data<DatabaseConnection>) -> HttpResponse { async fn retrieve(
_: RequireInstanceConfigured,
session: Authenticated<AppClaims>,
db: Data<DatabaseConnection>,
) -> HttpResponse {
match Users::find_by_id(session.account_id()).one(&**db).await { match Users::find_by_id(session.account_id()).one(&**db).await {
Ok(user) => HttpResponse::Ok().json(user), Ok(user) => HttpResponse::Ok().json(user),
Err(_error) => HttpResponse::BadRequest().finish(), Err(_error) => HttpResponse::BadRequest().finish(),

View File

@ -1,6 +1,7 @@
use std::env; use std::env;
use actix_jwt_session::*; use actix_jwt_session::*;
use actix_web::middleware::NormalizePath;
use actix_web::{web::Data, App, HttpServer}; use actix_web::{web::Data, App, HttpServer};
pub use jet_contract::event_bus::Client as EventBusClient; pub use jet_contract::event_bus::Client as EventBusClient;
pub use jet_contract::{deadpool_redis, redis, RedisClient}; pub use jet_contract::{deadpool_redis, redis, RedisClient};
@ -90,6 +91,7 @@ async fn main() {
.app_data(Data::new(db.clone())) .app_data(Data::new(db.clone()))
.app_data(application_config.clone()) .app_data(application_config.clone())
.app_data(event_bus.clone()) .app_data(event_bus.clone())
.wrap(NormalizePath::trim())
.wrap(factory.clone()) .wrap(factory.clone())
.wrap(actix_web::middleware::Logger::default()) .wrap(actix_web::middleware::Logger::default())
.configure(|config| http::configure(http_client.clone(), config)) .configure(|config| http::configure(http_client.clone(), config))