Fix clippy findings
This commit is contained in:
parent
99b9d85e47
commit
91559224e5
4 changed files with 22 additions and 13 deletions
|
|
@ -21,6 +21,8 @@ use axum::{body::Body, response::Response, Router};
|
||||||
#[cfg(feature = "oidc")]
|
#[cfg(feature = "oidc")]
|
||||||
use axum_oidc::{EmptyAdditionalClaims, OidcClaims};
|
use axum_oidc::{EmptyAdditionalClaims, OidcClaims};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
#[cfg(feature = "mock_auth")]
|
||||||
|
use service::permission::MockContext;
|
||||||
use service::user_service::UserService;
|
use service::user_service::UserService;
|
||||||
use service::PermissionService;
|
use service::PermissionService;
|
||||||
use service::ServiceError;
|
use service::ServiceError;
|
||||||
|
|
@ -31,9 +33,8 @@ use tower::ServiceBuilder;
|
||||||
use tower_sessions::{cookie::SameSite, Expiry, MemoryStore, SessionManagerLayer};
|
use tower_sessions::{cookie::SameSite, Expiry, MemoryStore, SessionManagerLayer};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
// TODO: In prod, it must be a different type than in dev mode.
|
|
||||||
#[cfg(feature = "mock_auth")]
|
#[cfg(feature = "mock_auth")]
|
||||||
type Context = ();
|
type Context = MockContext;
|
||||||
#[cfg(feature = "oidc")]
|
#[cfg(feature = "oidc")]
|
||||||
type Context = Option<Arc<str>>;
|
type Context = Option<Arc<str>>;
|
||||||
|
|
||||||
|
|
@ -216,9 +217,9 @@ pub fn oidc_config() -> OidcConfig {
|
||||||
let client_id = std::env::var("CLIENT_ID").expect("CLIENT_ID env variable");
|
let client_id = std::env::var("CLIENT_ID").expect("CLIENT_ID env variable");
|
||||||
let client_secret = std::env::var("CLIENT_SECRET").ok();
|
let client_secret = std::env::var("CLIENT_SECRET").ok();
|
||||||
OidcConfig {
|
OidcConfig {
|
||||||
app_url: app_url.into(),
|
app_url,
|
||||||
issuer: issuer.into(),
|
issuer,
|
||||||
client_id: client_id.into(),
|
client_id,
|
||||||
client_secret: client_secret.unwrap_or_default().into(),
|
client_secret: client_secret.unwrap_or_default().into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -245,7 +246,7 @@ pub async fn auth_info<RestState: RestStateDef>(
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let user = rest_state
|
let user = rest_state
|
||||||
.user_service()
|
.user_service()
|
||||||
.current_user(context.clone().into())
|
.current_user(context.clone())
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| "NoUser".into());
|
.unwrap_or_else(|_| "NoUser".into());
|
||||||
let privileges: Arc<[Arc<str>]> = rest_state
|
let privileges: Arc<[Arc<str>]> = rest_state
|
||||||
|
|
@ -253,12 +254,12 @@ pub async fn auth_info<RestState: RestStateDef>(
|
||||||
.get_privileges_for_current_user(context.into())
|
.get_privileges_for_current_user(context.into())
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| Arc::new([]))
|
.unwrap_or_else(|_| Arc::new([]))
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|privilege| privilege.name.clone())
|
.map(|privilege| privilege.name.clone())
|
||||||
.collect();
|
.collect();
|
||||||
let auth_info = AuthInfoTO { user, privileges };
|
let auth_info = AuthInfoTO { user, privileges };
|
||||||
|
|
||||||
let response = serde_json::to_string(&AuthInfoTO::from(auth_info)).unwrap();
|
let response = serde_json::to_string(&auth_info).unwrap();
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(200)
|
.status(200)
|
||||||
.body(Body::new(response))
|
.body(Body::new(response))
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,11 @@ use mockall::automock;
|
||||||
|
|
||||||
use crate::ServiceError;
|
use crate::ServiceError;
|
||||||
|
|
||||||
|
/// For mocking the context locally since there is actually
|
||||||
|
/// no context.
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct MockContext;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: Arc<str>,
|
pub name: Arc<str>,
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,13 @@ mod test;
|
||||||
pub mod uuid_service;
|
pub mod uuid_service;
|
||||||
|
|
||||||
pub use permission::PermissionServiceImpl;
|
pub use permission::PermissionServiceImpl;
|
||||||
|
use service::permission::MockContext;
|
||||||
|
|
||||||
pub struct UserServiceDev;
|
pub struct UserServiceDev;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl service::user_service::UserService for UserServiceDev {
|
impl service::user_service::UserService for UserServiceDev {
|
||||||
type Context = ();
|
type Context = MockContext;
|
||||||
|
|
||||||
async fn current_user(
|
async fn current_user(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -36,8 +37,6 @@ impl service::user_service::UserService for UserServiceImpl {
|
||||||
&self,
|
&self,
|
||||||
context: Self::Context,
|
context: Self::Context,
|
||||||
) -> Result<Arc<str>, service::ServiceError> {
|
) -> Result<Arc<str>, service::ServiceError> {
|
||||||
context
|
context.ok_or_else(|| service::ServiceError::Unauthorized)
|
||||||
.ok_or_else(|| service::ServiceError::Unauthorized)
|
|
||||||
.map(|user| user.clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,11 @@ async fn test_user_service_dev() {
|
||||||
let user_service = UserServiceDev;
|
let user_service = UserServiceDev;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"DEVUSER",
|
"DEVUSER",
|
||||||
user_service.current_user(()).await.unwrap().as_ref()
|
user_service
|
||||||
|
.current_user(MockContext)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.as_ref()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue