From 9f1feb3b11e81662f0a2a23e43b93a9aac3c5602 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Thu, 20 May 2021 17:46:36 +0200 Subject: [PATCH] Change auth handlers to return an HttpResponse --- src/infra/tcp_server.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index 2bb8196..84c5ee3 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -46,13 +46,17 @@ async fn index(req: HttpRequest) -> actix_web::Result { Ok(NamedFile::open(path)?) } -fn error_to_http_response(error: DomainError) -> ApiResult { - ApiResult::Right( +fn error_to_http_response(error: DomainError) -> HttpResponse { match error { DomainError::AuthenticationError(_) => HttpResponse::Unauthorized(), DomainError::DatabaseError(_) => HttpResponse::InternalServerError(), } - .body(error.to_string()), + .body(error.to_string()) +} + +fn error_to_api_response(error: DomainError) -> ApiResult { + ApiResult::Right( + error_to_http_response(error) ) } @@ -70,7 +74,7 @@ where .list_users(req) .await .map(|res| ApiResult::Left(web::Json(res))) - .unwrap_or_else(error_to_http_response) + .unwrap_or_else(error_to_api_response) } fn create_jwt(key: &Hmac, user: String, groups: HashSet) -> SignedToken { @@ -90,7 +94,7 @@ fn create_jwt(key: &Hmac, user: String, groups: HashSet) -> Sign async fn get_refresh( data: web::Data>, request: HttpRequest, -) -> ApiResult +) -> HttpResponse where Backend: TcpBackendHandler + 'static, { @@ -98,11 +102,11 @@ where let jwt_key = &data.jwt_key; let (refresh_token, user) = match request.cookie("refresh_token") { None => { - return ApiResult::Right(HttpResponse::Unauthorized().body("Missing refresh token")) + return HttpResponse::Unauthorized().body("Missing refresh token") } Some(t) => match t.value().split_once("+") { None => { - return ApiResult::Right(HttpResponse::Unauthorized().body("Invalid refresh token")) + return HttpResponse::Unauthorized().body("Invalid refresh token") } Some((t, u)) => (t.to_string(), u.to_string()), }, @@ -123,7 +127,6 @@ where } .map(|groups| create_jwt(jwt_key, user.to_string(), groups)) .map(|token| { - ApiResult::Right( HttpResponse::Ok() .cookie( Cookie::build("token", token.as_str()) @@ -133,8 +136,7 @@ where .same_site(SameSite::Strict) .finish(), ) - .body(token.as_str().to_owned()), - ) + .body(token.as_str().to_owned()) }) .unwrap_or_else(error_to_http_response) } @@ -142,7 +144,7 @@ where async fn post_authorize( data: web::Data>, request: web::Json, -) -> ApiResult +) -> HttpResponse where Backend: TcpBackendHandler + 'static, { @@ -163,7 +165,6 @@ where .await .map(|(groups, (refresh_token, max_age))| { let token = create_jwt(&data.jwt_key, request.name.clone(), groups); - ApiResult::Right( HttpResponse::Ok() .cookie( Cookie::build("token", token.as_str()) @@ -181,8 +182,7 @@ where .same_site(SameSite::Strict) .finish(), ) - .body(token.as_str().to_owned()), - ) + .body(token.as_str().to_owned()) }) .unwrap_or_else(error_to_http_response) }