fix: put auth errors in good place
This commit is contained in:
+1
-13
@@ -1,3 +1,4 @@
|
||||
use crate::api::middlewares::errors::AuthMiddlewareError;
|
||||
use crate::databases::errors::DbError;
|
||||
use crate::providers::keycloak::errors::AuthError;
|
||||
use crate::providers::ollama::errors::LlmError;
|
||||
@@ -7,7 +8,6 @@ use axum::Json;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
@@ -25,18 +25,6 @@ pub struct ApiError {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AuthMiddlewareError {
|
||||
#[error("invalid authorization format")]
|
||||
InvalidAuthorizationFormat,
|
||||
|
||||
#[error("authentication required")]
|
||||
AuthenticationRequired,
|
||||
|
||||
#[error("insufficient permissions")]
|
||||
Forbidden,
|
||||
}
|
||||
|
||||
impl From<AuthMiddlewareError> for ApiError {
|
||||
fn from(err: AuthMiddlewareError) -> Self {
|
||||
match err {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::api::errors::{ApiError, AuthMiddlewareError};
|
||||
use crate::api::errors::ApiError;
|
||||
use crate::api::middlewares::errors::AuthMiddlewareError;
|
||||
use crate::api::state::SharedState;
|
||||
|
||||
use axum::{
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AuthMiddlewareError {
|
||||
#[error("invalid authorization format")]
|
||||
InvalidAuthorizationFormat,
|
||||
|
||||
#[error("authentication required")]
|
||||
AuthenticationRequired,
|
||||
|
||||
#[error("insufficient permissions")]
|
||||
Forbidden,
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod errors;
|
||||
|
||||
+23
-18
@@ -16,22 +16,23 @@ async fn openapi_json() -> Json<utoipa::openapi::OpenApi> {
|
||||
Json(ApiDoc::openapi())
|
||||
}
|
||||
|
||||
fn public_router() -> Router<SharedState> {
|
||||
Router::new().route("/docs.json", get(openapi_json))
|
||||
}
|
||||
|
||||
pub fn protected_router() -> Router<SharedState> {
|
||||
fn llm_router() -> Router<SharedState> {
|
||||
Router::new()
|
||||
.route("/models", get(llm::list_models))
|
||||
.route("/completions", post(llm::completions))
|
||||
.route("/chat/completions", post(llm::chat_completions))
|
||||
.route("/models/{model}/load", post(llm::load_model))
|
||||
// .route("/models/{model}/unload", post(models::unload_model))
|
||||
.route(
|
||||
"/keys/generate",
|
||||
post(apikey::create_api_key) // Usage
|
||||
.route_layer(role_guard!(Some("admin"), None)),
|
||||
)
|
||||
}
|
||||
|
||||
fn keys_router() -> Router<SharedState> {
|
||||
Router::new().route(
|
||||
"/generate",
|
||||
post(apikey::create_api_key).route_layer(role_guard!(Some("admin"), None)),
|
||||
)
|
||||
}
|
||||
|
||||
fn log_router() -> Router<SharedState> {
|
||||
Router::new()
|
||||
.route("/conversations", get(llm::get_conversations))
|
||||
.route(
|
||||
"/conversations/{conversation_id}/messages",
|
||||
@@ -40,13 +41,17 @@ pub fn protected_router() -> Router<SharedState> {
|
||||
}
|
||||
|
||||
pub fn router(state: SharedState) -> Router<SharedState> {
|
||||
let protected = Router::new()
|
||||
.nest("/llm", llm_router())
|
||||
.nest("/keys", keys_router())
|
||||
.nest("/log", log_router())
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
auth_middleware,
|
||||
));
|
||||
|
||||
Router::new()
|
||||
.merge(public_router())
|
||||
.merge(
|
||||
protected_router().route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
auth_middleware,
|
||||
)),
|
||||
)
|
||||
.route("/docs.json", get(openapi_json))
|
||||
.merge(protected)
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user