This commit is contained in:
2025-11-06 12:26:29 +01:00
parent 4027ac66cb
commit a6f79dbc75
12 changed files with 344 additions and 199 deletions
+22 -19
View File
@@ -28,6 +28,7 @@ use crate::{
AuthenticatedUser,
},
error::{AppError, AppResult},
http::responders::{ok_json, JsonResponse},
models::{
MagicToken, MagicTokenKind, NewUser, NewUserSession, TenantStatus, User, UserMembership,
UserSession,
@@ -224,7 +225,7 @@ pub async fn login(
pub async fn api_token_exchange(
State(state): State<AppState>,
Json(payload): Json<ApiTokenExchangeRequest>,
) -> AppResult<Json<LoginResponse>> {
) -> AppResult<JsonResponse<LoginResponse>> {
let secret = payload.api_token.trim();
if secret.is_empty() {
return Err(AppError::bad_request("api_token must not be empty"));
@@ -289,7 +290,7 @@ pub async fn api_token_exchange(
},
};
Ok(Json(response))
ok_json(response)
}
#[utoipa::path(
@@ -306,7 +307,7 @@ pub async fn api_token_exchange(
pub async fn signup_start(
State(state): State<AppState>,
Json(payload): Json<SignupStartRequest>,
) -> AppResult<Json<SignupStartResponse>> {
) -> AppResult<JsonResponse<SignupStartResponse>> {
let username = payload.username.trim();
if username.is_empty() {
return Err(AppError::bad_request("username must not be empty"));
@@ -334,10 +335,10 @@ pub async fn signup_start(
.generate_signup_token(user_id, challenge.challenge_id, username.to_owned())
.map_err(AppError::from)?;
Ok(Json(SignupStartResponse {
ok_json(SignupStartResponse {
signup_token,
challenge,
}))
})
}
#[utoipa::path(
@@ -590,7 +591,7 @@ pub async fn me(user: AuthenticatedUser) -> Json<AuthenticatedUser> {
pub async fn list_tenants(
State(state): State<AppState>,
auth: Option<TypedHeader<Authorization<Bearer>>>,
) -> AppResult<Json<TenantListResponse>> {
) -> AppResult<JsonResponse<TenantListResponse>> {
let bearer = auth.ok_or_else(AppError::unauthorized)?;
let token = bearer.token();
@@ -630,7 +631,7 @@ pub async fn list_tenants(
});
}
Ok(Json(TenantListResponse { tenants }))
ok_json(TenantListResponse { tenants })
}
#[utoipa::path(
@@ -642,7 +643,7 @@ pub async fn list_tenants(
pub async fn passkey_register_start(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> AppResult<Json<RegistrationChallengeResponse>> {
) -> AppResult<JsonResponse<RegistrationChallengeResponse>> {
let service = state
.passkeys
.as_ref()
@@ -651,7 +652,7 @@ pub async fn passkey_register_start(
let mut conn = state.db_unscoped()?;
let current_user: User = dsl::users.find(user.user_id).first(&mut conn)?;
let challenge = service.start_registration(&mut conn, &current_user)?;
Ok(Json(challenge))
ok_json(challenge)
}
#[utoipa::path(
@@ -665,7 +666,7 @@ pub async fn passkey_register_finish(
State(state): State<AppState>,
user: AuthenticatedUser,
Json(payload): Json<PasskeyRegistrationFinishPayload>,
) -> AppResult<Json<PasskeySummary>> {
) -> AppResult<JsonResponse<PasskeySummary>> {
let service = state
.passkeys
.as_ref()
@@ -688,7 +689,7 @@ pub async fn passkey_register_finish(
nickname,
)?;
Ok(Json(PasskeySummary::from(passkey)))
ok_json(PasskeySummary::from(passkey))
}
#[utoipa::path(
@@ -701,7 +702,7 @@ pub async fn passkey_register_finish(
pub async fn passkey_login_start(
State(state): State<AppState>,
Json(payload): Json<PasskeyLoginStartPayload>,
) -> AppResult<Json<AuthenticationChallengeResponse>> {
) -> AppResult<JsonResponse<AuthenticationChallengeResponse>> {
let service = state
.passkeys
.as_ref()
@@ -718,7 +719,7 @@ pub async fn passkey_login_start(
.first(&mut conn)?;
let challenge = service.start_authentication(&mut conn, &user)?;
Ok(Json(challenge))
ok_json(challenge)
}
#[utoipa::path(
@@ -799,11 +800,12 @@ fn complete_login(
});
}
Ok(Json(TenantSelectionResponse {
let response = ok_json(TenantSelectionResponse {
access_token: selection_token,
tenants,
})
.into_response())
})?;
Ok(response.into_response())
}
fn magic_token_login(
@@ -925,7 +927,7 @@ fn issue_session(
.values(&new_session)
.execute(conn)?;
let mut response = Json(LoginResponse {
let json = ok_json(LoginResponse {
access_token,
token_type: "Bearer".to_string(),
expires_in: state.config.jwt_expiry_minutes * 60,
@@ -933,8 +935,9 @@ fn issue_session(
id: tenant_id,
name: tenant_name,
},
})
.into_response();
})?;
let mut response = json.into_response();
response.headers_mut().insert(
SET_COOKIE,