Skip to content

Commit 3ecd7c1

Browse files
committed
crate::owners: Use token scope restrictions
1 parent fbd5b9e commit 3ecd7c1

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/controllers/krate/owners.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::auth::AuthCheck;
44
use crate::controllers::prelude::*;
5+
use crate::models::token::EndpointScope;
56
use crate::models::{Crate, Owner, Rights, Team, User};
67
use crate::views::EncodableOwner;
78

@@ -80,7 +81,12 @@ fn parse_owners_request(req: &mut dyn RequestExt) -> AppResult<Vec<String>> {
8081
}
8182

8283
fn modify_owners(req: &mut dyn RequestExt, add: bool) -> EndpointResult {
83-
let auth = AuthCheck::default().check(req)?;
84+
let crate_name = &req.params()["crate_id"];
85+
86+
let auth = AuthCheck::default()
87+
.with_endpoint_scope(EndpointScope::ChangeOwners)
88+
.for_crate(crate_name)
89+
.check(req)?;
8490

8591
let logins = parse_owners_request(req)?;
8692
let app = req.app();

src/tests/owners.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,11 @@ fn owner_change_via_change_owner_token_with_wrong_crate_scope() {
367367
let body = json!({ "owners": [user2.gh_login] });
368368
let body = serde_json::to_vec(&body).unwrap();
369369
let response = token.put::<()>(&url, &body);
370-
assert_eq!(response.status(), StatusCode::OK);
370+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
371371
assert_eq!(
372372
response.into_json(),
373-
json!({ "ok": true, "msg": "user user-2 has been invited to be an owner of crate foo_crate" })
373+
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
374374
);
375-
// TODO this should return "403 Forbidden" once token scopes are implemented for this endpoint
376-
// assert_eq!(response.status(), StatusCode::FORBIDDEN);
377-
// assert_eq!(
378-
// response.into_json(),
379-
// json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
380-
// );
381375
}
382376

383377
#[test]
@@ -395,17 +389,11 @@ fn owner_change_via_publish_token() {
395389
let body = json!({ "owners": [user2.gh_login] });
396390
let body = serde_json::to_vec(&body).unwrap();
397391
let response = token.put::<()>(&url, &body);
398-
assert_eq!(response.status(), StatusCode::OK);
392+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
399393
assert_eq!(
400394
response.into_json(),
401-
json!({ "ok": true, "msg": "user user-2 has been invited to be an owner of crate foo_crate" })
395+
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
402396
);
403-
// TODO this should return "403 Forbidden" once token scopes are implemented for this endpoint
404-
// assert_eq!(response.status(), StatusCode::FORBIDDEN);
405-
// assert_eq!(
406-
// response.into_json(),
407-
// json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
408-
// );
409397
}
410398

411399
#[test]

0 commit comments

Comments
 (0)