Skip to content

Commit 8e85f51

Browse files
committed
refactor: [torrust#615] new authorization error for guest users
1 parent d8b3ee2 commit 8e85f51

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed

src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ pub enum ServiceError {
111111
#[display(fmt = "Unauthorized action.")]
112112
UnauthorizedAction,
113113

114+
#[display(
115+
fmt = "Unauthorized actions for guest users. Try logging in to check if you have permission to perform the action"
116+
)]
117+
UnauthorizedActionForGuests,
118+
114119
#[display(fmt = "This torrent already exists in our database.")]
115120
InfoHashAlreadyExists,
116121

@@ -301,6 +306,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
301306
ServiceError::InvalidCategory => StatusCode::BAD_REQUEST,
302307
ServiceError::InvalidTag => StatusCode::BAD_REQUEST,
303308
ServiceError::UnauthorizedAction => StatusCode::FORBIDDEN,
309+
ServiceError::UnauthorizedActionForGuests => StatusCode::UNAUTHORIZED,
304310
ServiceError::InfoHashAlreadyExists => StatusCode::BAD_REQUEST,
305311
ServiceError::CanonicalInfoHashAlreadyExists => StatusCode::CONFLICT,
306312
ServiceError::OriginalInfoHashAlreadyExists => StatusCode::CONFLICT,

src/services/authorization.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ impl Service {
8080
let enforcer = self.casbin_enforcer.enforcer.read().await;
8181

8282
let authorize = enforcer
83-
.enforce((role, action))
83+
.enforce((&role, action))
8484
.map_err(|_| ServiceError::UnauthorizedAction)?;
8585

8686
if authorize {
8787
Ok(())
88+
} else if role == UserRole::Guest {
89+
Err(ServiceError::UnauthorizedActionForGuests)
8890
} else {
8991
Err(ServiceError::UnauthorizedAction)
9092
}

tests/e2e/web/api/v1/contexts/category/contract.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn it_should_not_allow_adding_a_new_category_to_unauthenticated_users() {
6161
})
6262
.await;
6363

64-
assert_eq!(response.status, 403);
64+
assert_eq!(response.status, 401);
6565
}
6666

6767
#[tokio::test]
@@ -194,5 +194,5 @@ async fn it_should_not_allow_guests_to_delete_categories() {
194194
})
195195
.await;
196196

197-
assert_eq!(response.status, 403);
197+
assert_eq!(response.status, 401);
198198
}

tests/e2e/web/api/v1/contexts/tag/contract.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async fn it_should_not_allow_adding_a_new_tag_to_unauthenticated_users() {
6363
})
6464
.await;
6565

66-
assert_eq!(response.status, 403);
66+
assert_eq!(response.status, 401);
6767
}
6868

6969
#[tokio::test]
@@ -174,5 +174,5 @@ async fn it_should_not_allow_guests_to_delete_tags() {
174174

175175
let response = client.delete_tag(DeleteTagForm { tag_id }).await;
176176

177-
assert_eq!(response.status, 403);
177+
assert_eq!(response.status, 401);
178178
}

tests/e2e/web/api/v1/contexts/torrent/contract.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ mod for_guests {
442442

443443
let response = client.upload_torrent(form.into()).await;
444444

445-
assert_eq!(response.status, 403);
445+
assert_eq!(response.status, 401);
446446
}
447447

448448
#[tokio::test]

tests/e2e/web/api/v1/contexts/user/contract.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,6 @@ mod banned_user_list {
231231

232232
let response = client.ban_user(Username::new(registered_user.username.clone())).await;
233233

234-
assert_eq!(response.status, 403);
234+
assert_eq!(response.status, 401);
235235
}
236236
}

0 commit comments

Comments
 (0)