Skip to content

Commit f54ffae

Browse files
committed
Only pass in HashSet.
1 parent 605b3a4 commit f54ffae

File tree

9 files changed

+30
-32
lines changed

9 files changed

+30
-32
lines changed

src/auth.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::config::Server;
1+
use std::collections::HashSet;
2+
23
use crate::controllers;
34
use crate::controllers::util::RequestPartsExt;
45
use crate::middleware::log_request::RequestLogExt;
@@ -74,16 +75,16 @@ impl AuthCheck {
7475
pub fn check<T: RequestPartsExt>(
7576
&self,
7677
request: &T,
77-
config: &Server,
78+
gh_admin_user_ids: &HashSet<i32>,
7879
conn: &mut PgConnection,
7980
) -> AppResult<Authentication> {
80-
self.check_authentication(authenticate(request, conn)?, config)
81+
self.check_authentication(authenticate(request, conn)?, gh_admin_user_ids)
8182
}
8283

8384
fn check_authentication(
8485
&self,
8586
auth: Authentication,
86-
config: &Server,
87+
gh_admin_user_ids: &HashSet<i32>,
8788
) -> AppResult<Authentication> {
8889
if let Some(token) = auth.api_token() {
8990
if !self.allow_token {
@@ -103,7 +104,7 @@ impl AuthCheck {
103104
}
104105
}
105106

106-
if self.require_admin && !config.gh_admin_user_ids.contains(&auth.user().gh_id) {
107+
if self.require_admin && !gh_admin_user_ids.contains(&auth.user().gh_id) {
107108
let error_message = "User is unauthorized";
108109
return Err(internal(error_message).chain(forbidden()));
109110
}
@@ -374,15 +375,12 @@ mod tests {
374375
#[test]
375376
fn require_admin() {
376377
let auth_check = AuthCheck::default().require_admin();
377-
let config = Server {
378-
gh_admin_user_ids: [42, 43].into_iter().collect(),
379-
..Default::default()
380-
};
378+
let gh_admin_user_ids = [42, 43].into_iter().collect();
381379

382-
assert_ok!(auth_check.check_authentication(mock_cookie(42), &config));
383-
assert_err!(auth_check.check_authentication(mock_cookie(44), &config));
384-
assert_ok!(auth_check.check_authentication(mock_token(43), &config));
385-
assert_err!(auth_check.check_authentication(mock_token(45), &config));
380+
assert_ok!(auth_check.check_authentication(mock_cookie(42), &gh_admin_user_ids));
381+
assert_err!(auth_check.check_authentication(mock_cookie(44), &gh_admin_user_ids));
382+
assert_ok!(auth_check.check_authentication(mock_token(43), &gh_admin_user_ids));
383+
assert_err!(auth_check.check_authentication(mock_token(45), &gh_admin_user_ids));
386384
}
387385

388386
fn mock_user(gh_id: i32) -> User {

src/controllers/crate_owner_invitation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::collections::{HashMap, HashSet};
1919
pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
2020
conduit_compat(move || {
2121
let conn = &mut app.db_read()?;
22-
let auth = AuthCheck::only_cookie().check(&req, &app.config, conn)?;
22+
let auth = AuthCheck::only_cookie().check(&req, &app.config.gh_admin_user_ids, conn)?;
2323
let user_id = auth.user_id();
2424

2525
let PrivateListResponse {
@@ -59,7 +59,7 @@ pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
5959
pub async fn private_list(app: AppState, req: Parts) -> AppResult<Json<PrivateListResponse>> {
6060
conduit_compat(move || {
6161
let conn = &mut app.db_read()?;
62-
let auth = AuthCheck::only_cookie().check(&req, &app.config, conn)?;
62+
let auth = AuthCheck::only_cookie().check(&req, &app.config.gh_admin_user_ids, conn)?;
6363

6464
let filter = if let Some(crate_name) = req.query().get("crate_name") {
6565
ListFilter::CrateName(crate_name.clone())
@@ -267,7 +267,7 @@ pub async fn handle_invite(state: AppState, req: BytesRequest) -> AppResult<Json
267267

268268
let conn = &mut state.db_write()?;
269269

270-
let auth = AuthCheck::default().check(&req, &state.config, conn)?;
270+
let auth = AuthCheck::default().check(&req, &state.config.gh_admin_user_ids, conn)?;
271271
let user_id = auth.user_id();
272272

273273
let config = &state.config;

src/controllers/krate/follow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn follow(
2121
conduit_compat(move || {
2222
let conn = &mut *app.db_write()?;
2323
let user_id = AuthCheck::default()
24-
.check(&req, &app.config, conn)?
24+
.check(&req, &app.config.gh_admin_user_ids, conn)?
2525
.user_id();
2626
let follow = follow_target(&crate_name, conn, user_id)?;
2727
diesel::insert_into(follows::table)
@@ -43,7 +43,7 @@ pub async fn unfollow(
4343
conduit_compat(move || {
4444
let conn = &mut *app.db_write()?;
4545
let user_id = AuthCheck::default()
46-
.check(&req, &app.config, conn)?
46+
.check(&req, &app.config.gh_admin_user_ids, conn)?
4747
.user_id();
4848
let follow = follow_target(&crate_name, conn, user_id)?;
4949
diesel::delete(&follow).execute(conn)?;
@@ -64,7 +64,7 @@ pub async fn following(
6464

6565
let conn = &mut *app.db_read_prefer_primary()?;
6666
let user_id = AuthCheck::only_cookie()
67-
.check(&req, &app.config, conn)?
67+
.check(&req, &app.config.gh_admin_user_ids, conn)?
6868
.user_id();
6969
let follow = follow_target(&crate_name, conn, user_id)?;
7070
let following =

src/controllers/krate/owners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn modify_owners(
106106
let auth = AuthCheck::default()
107107
.with_endpoint_scope(EndpointScope::ChangeOwners)
108108
.for_crate(crate_name)
109-
.check(req, &app.config, conn)?;
109+
.check(req, &app.config.gh_admin_user_ids, conn)?;
110110

111111
let user = auth.user();
112112

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
9494
let auth = AuthCheck::default()
9595
.with_endpoint_scope(endpoint_scope)
9696
.for_crate(&new_crate.name)
97-
.check(&req, &app.config, conn)?;
97+
.check(&req, &app.config.gh_admin_user_ids, conn)?;
9898

9999
let api_token_id = auth.api_token_id();
100100
let user = auth.user();

src/controllers/krate/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
191191
supports_seek = false;
192192

193193
let user_id = AuthCheck::default()
194-
.check(&req, &app.config, conn)?
194+
.check(&req, &app.config.gh_admin_user_ids, conn)?
195195
.user_id();
196196

197197
query = query.filter(

src/controllers/token.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde_json as json;
1313
pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
1414
conduit_compat(move || {
1515
let conn = &mut *app.db_read_prefer_primary()?;
16-
let auth = AuthCheck::only_cookie().check(&req, &app.config, conn)?;
16+
let auth = AuthCheck::only_cookie().check(&req, &app.config.gh_admin_user_ids, conn)?;
1717
let user = auth.user();
1818

1919
let tokens: Vec<ApiToken> = ApiToken::belonging_to(user)
@@ -53,7 +53,7 @@ pub async fn new(app: AppState, req: BytesRequest) -> AppResult<Json<Value>> {
5353

5454
let conn = &mut *app.db_write()?;
5555

56-
let auth = AuthCheck::default().check(&req, &app.config, conn)?;
56+
let auth = AuthCheck::default().check(&req, &app.config.gh_admin_user_ids, conn)?;
5757
if auth.api_token_id().is_some() {
5858
return Err(bad_request(
5959
"cannot use an API token to create a new API token",
@@ -107,7 +107,7 @@ pub async fn new(app: AppState, req: BytesRequest) -> AppResult<Json<Value>> {
107107
pub async fn revoke(app: AppState, Path(id): Path<i32>, req: Parts) -> AppResult<Json<Value>> {
108108
conduit_compat(move || {
109109
let conn = &mut *app.db_write()?;
110-
let auth = AuthCheck::default().check(&req, &app.config, conn)?;
110+
let auth = AuthCheck::default().check(&req, &app.config.gh_admin_user_ids, conn)?;
111111
let user = auth.user();
112112
diesel::update(ApiToken::belonging_to(user).find(id))
113113
.set(api_tokens::revoked.eq(true))
@@ -122,7 +122,7 @@ pub async fn revoke(app: AppState, Path(id): Path<i32>, req: Parts) -> AppResult
122122
pub async fn revoke_current(app: AppState, req: Parts) -> AppResult<Response> {
123123
conduit_compat(move || {
124124
let conn = &mut *app.db_write()?;
125-
let auth = AuthCheck::default().check(&req, &app.config, conn)?;
125+
let auth = AuthCheck::default().check(&req, &app.config.gh_admin_user_ids, conn)?;
126126
let api_token_id = auth
127127
.api_token_id()
128128
.ok_or_else(|| bad_request("token not provided"))?;

src/controllers/user/me.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn me(app: AppState, req: Parts) -> AppResult<Json<EncodableMe>> {
1717
conduit_compat(move || {
1818
let conn = &mut *app.db_read_prefer_primary()?;
1919
let user_id = AuthCheck::only_cookie()
20-
.check(&req, &app.config, conn)?
20+
.check(&req, &app.config.gh_admin_user_ids, conn)?
2121
.user_id();
2222

2323
let (user, verified, email, verification_sent): (User, Option<bool>, Option<String>, bool) =
@@ -60,7 +60,7 @@ pub async fn me(app: AppState, req: Parts) -> AppResult<Json<EncodableMe>> {
6060
pub async fn updates(app: AppState, req: Parts) -> AppResult<Json<Value>> {
6161
conduit_compat(move || {
6262
let conn = &mut app.db_read_prefer_primary()?;
63-
let auth = AuthCheck::only_cookie().check(&req, &app.config, conn)?;
63+
let auth = AuthCheck::only_cookie().check(&req, &app.config.gh_admin_user_ids, conn)?;
6464
let user = auth.user();
6565

6666
let followed_crates = Follow::belonging_to(user).select(follows::crate_id);
@@ -111,7 +111,7 @@ pub async fn update_user(
111111
let state = app.clone();
112112
let conn = &mut state.db_write()?;
113113

114-
let auth = AuthCheck::default().check(&req, &app.config, conn)?;
114+
let auth = AuthCheck::default().check(&req, &app.config.gh_admin_user_ids, conn)?;
115115
let user = auth.user();
116116

117117
// need to check if current user matches user to be updated
@@ -204,7 +204,7 @@ pub async fn regenerate_token_and_send(
204204

205205
let conn = &mut state.db_write()?;
206206

207-
let auth = AuthCheck::default().check(&req, &state.config, conn)?;
207+
let auth = AuthCheck::default().check(&req, &state.config.gh_admin_user_ids, conn)?;
208208
let user = auth.user();
209209

210210
// need to check if current user matches user to be updated
@@ -249,7 +249,7 @@ pub async fn update_email_notifications(app: AppState, req: BytesRequest) -> App
249249

250250
let conn = &mut *app.db_write()?;
251251
let user_id = AuthCheck::default()
252-
.check(&req, &app.config, conn)?
252+
.check(&req, &app.config.gh_admin_user_ids, conn)?
253253
.user_id();
254254

255255
// Build inserts from existing crates belonging to the current user

src/controllers/version/yank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn modify_yank(
5656
let auth = AuthCheck::default()
5757
.with_endpoint_scope(EndpointScope::Yank)
5858
.for_crate(crate_name)
59-
.check(req, &state.config, conn)?;
59+
.check(req, &state.config.gh_admin_user_ids, conn)?;
6060

6161
let (version, krate) = version_and_crate(conn, crate_name, version)?;
6262
let api_token_id = auth.api_token_id();

0 commit comments

Comments
 (0)