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

View File

@ -3,6 +3,7 @@ use crate::session::AppClaims;
use actix_jwt_session::{
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::{HttpRequest, HttpResponse};
use entities::prelude::Users;
@ -25,12 +26,16 @@ pub struct AuthResponseBody {
}
pub fn configure(http_client: reqwest::Client, config: &mut ServiceConfig) {
config
.service(email_check::email_check)
.service(sign_in::sign_in)
.service(sign_up::sign_up)
.service(sign_out::sign_out);
social_auth::configure(http_client, config);
config.service(
scope("")
.service(email_check::email_check)
.service(sign_in::sign_in)
.service(sign_up::sign_up)
.service(sign_out::sign_out)
.configure(|c| {
social_auth::configure(http_client, c);
}),
);
}
#[derive(Debug, Clone, Copy, derive_more::Display)]

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@ pub fn configure(http_client: reqwest::Client, config: &mut ServiceConfig) {
config.service(
scope("/api")
.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 actix_jwt_session::Authenticated;
use actix_web::get;
@ -12,7 +13,11 @@ pub fn configure(config: &mut ServiceConfig) {
}
#[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 {
Ok(user) => HttpResponse::Ok().json(user),
Err(_error) => HttpResponse::BadRequest().finish(),

View File

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