2024-01-03 15:34:03 +00:00
|
|
|
use crate::read_auth_token;
|
2023-10-17 15:25:48 +00:00
|
|
|
use actix_web::{
|
|
|
|
web::{Data, Json},
|
|
|
|
HttpRequest,
|
|
|
|
};
|
2024-01-03 15:34:03 +00:00
|
|
|
use lemmy_api_common::{claims::Claims, context::LemmyContext, SuccessResponse};
|
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};
|
2023-10-17 15:25:48 +00:00
|
|
|
|
|
|
|
/// Returns an error message if the auth token is invalid for any reason. Necessary because other
|
|
|
|
/// endpoints silently treat any call with invalid auth as unauthenticated.
|
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn validate_auth(
|
|
|
|
req: HttpRequest,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<SuccessResponse>, LemmyError> {
|
|
|
|
let jwt = read_auth_token(&req)?;
|
|
|
|
if let Some(jwt) = jwt {
|
2024-01-03 15:34:03 +00:00
|
|
|
Claims::validate(&jwt, &context)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::NotLoggedIn)?;
|
2023-10-17 15:25:48 +00:00
|
|
|
} else {
|
|
|
|
Err(LemmyErrorType::NotLoggedIn)?;
|
|
|
|
}
|
|
|
|
Ok(Json(SuccessResponse::default()))
|
|
|
|
}
|