Skip to content

Commit fc24f38

Browse files
committed
Use RequestApp
1 parent f54ffae commit fc24f38

File tree

9 files changed

+24
-33
lines changed

9 files changed

+24
-33
lines changed

src/auth.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashSet;
22

33
use crate::controllers;
44
use crate::controllers::util::RequestPartsExt;
5+
use crate::middleware::app::RequestApp;
56
use crate::middleware::log_request::RequestLogExt;
67
use crate::middleware::session::RequestSession;
78
use crate::models::token::{CrateScope, EndpointScope};
@@ -75,10 +76,12 @@ impl AuthCheck {
7576
pub fn check<T: RequestPartsExt>(
7677
&self,
7778
request: &T,
78-
gh_admin_user_ids: &HashSet<i32>,
7979
conn: &mut PgConnection,
8080
) -> AppResult<Authentication> {
81-
self.check_authentication(authenticate(request, conn)?, gh_admin_user_ids)
81+
self.check_authentication(
82+
authenticate(request, conn)?,
83+
&request.app().config.gh_admin_user_ids,
84+
)
8285
}
8386

8487
fn check_authentication(

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.gh_admin_user_ids, conn)?;
22+
let auth = AuthCheck::only_cookie().check(&req, 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.gh_admin_user_ids, conn)?;
62+
let auth = AuthCheck::only_cookie().check(&req, 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.gh_admin_user_ids, conn)?;
270+
let auth = AuthCheck::default().check(&req, conn)?;
271271
let user_id = auth.user_id();
272272

273273
let config = &state.config;

src/controllers/krate/follow.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ pub async fn follow(
2020
) -> AppResult<Response> {
2121
conduit_compat(move || {
2222
let conn = &mut *app.db_write()?;
23-
let user_id = AuthCheck::default()
24-
.check(&req, &app.config.gh_admin_user_ids, conn)?
25-
.user_id();
23+
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
2624
let follow = follow_target(&crate_name, conn, user_id)?;
2725
diesel::insert_into(follows::table)
2826
.values(&follow)
@@ -42,9 +40,7 @@ pub async fn unfollow(
4240
) -> AppResult<Response> {
4341
conduit_compat(move || {
4442
let conn = &mut *app.db_write()?;
45-
let user_id = AuthCheck::default()
46-
.check(&req, &app.config.gh_admin_user_ids, conn)?
47-
.user_id();
43+
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
4844
let follow = follow_target(&crate_name, conn, user_id)?;
4945
diesel::delete(&follow).execute(conn)?;
5046

@@ -63,9 +59,7 @@ pub async fn following(
6359
use diesel::dsl::exists;
6460

6561
let conn = &mut *app.db_read_prefer_primary()?;
66-
let user_id = AuthCheck::only_cookie()
67-
.check(&req, &app.config.gh_admin_user_ids, conn)?
68-
.user_id();
62+
let user_id = AuthCheck::only_cookie().check(&req, conn)?.user_id();
6963
let follow = follow_target(&crate_name, conn, user_id)?;
7064
let following =
7165
diesel::select(exists(follows::table.find(follow.id()))).get_result::<bool>(conn)?;

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.gh_admin_user_ids, conn)?;
109+
.check(req, 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.gh_admin_user_ids, conn)?;
97+
.check(&req, 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
190190
// Calculating the total number of results with filters is not supported yet.
191191
supports_seek = false;
192192

193-
let user_id = AuthCheck::default()
194-
.check(&req, &app.config.gh_admin_user_ids, conn)?
195-
.user_id();
193+
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
196194

197195
query = query.filter(
198196
crates::id.eq_any(

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.gh_admin_user_ids, conn)?;
16+
let auth = AuthCheck::only_cookie().check(&req, 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.gh_admin_user_ids, conn)?;
56+
let auth = AuthCheck::default().check(&req, 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.gh_admin_user_ids, conn)?;
110+
let auth = AuthCheck::default().check(&req, 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.gh_admin_user_ids, conn)?;
125+
let auth = AuthCheck::default().check(&req, 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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCra
1616
pub async fn me(app: AppState, req: Parts) -> AppResult<Json<EncodableMe>> {
1717
conduit_compat(move || {
1818
let conn = &mut *app.db_read_prefer_primary()?;
19-
let user_id = AuthCheck::only_cookie()
20-
.check(&req, &app.config.gh_admin_user_ids, conn)?
21-
.user_id();
19+
let user_id = AuthCheck::only_cookie().check(&req, conn)?.user_id();
2220

2321
let (user, verified, email, verification_sent): (User, Option<bool>, Option<String>, bool) =
2422
users::table
@@ -60,7 +58,7 @@ pub async fn me(app: AppState, req: Parts) -> AppResult<Json<EncodableMe>> {
6058
pub async fn updates(app: AppState, req: Parts) -> AppResult<Json<Value>> {
6159
conduit_compat(move || {
6260
let conn = &mut app.db_read_prefer_primary()?;
63-
let auth = AuthCheck::only_cookie().check(&req, &app.config.gh_admin_user_ids, conn)?;
61+
let auth = AuthCheck::only_cookie().check(&req, conn)?;
6462
let user = auth.user();
6563

6664
let followed_crates = Follow::belonging_to(user).select(follows::crate_id);
@@ -111,7 +109,7 @@ pub async fn update_user(
111109
let state = app.clone();
112110
let conn = &mut state.db_write()?;
113111

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

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

205203
let conn = &mut state.db_write()?;
206204

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

210208
// need to check if current user matches user to be updated
@@ -248,9 +246,7 @@ pub async fn update_email_notifications(app: AppState, req: BytesRequest) -> App
248246
.collect();
249247

250248
let conn = &mut *app.db_write()?;
251-
let user_id = AuthCheck::default()
252-
.check(&req, &app.config.gh_admin_user_ids, conn)?
253-
.user_id();
249+
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
254250

255251
// Build inserts from existing crates belonging to the current user
256252
let to_insert = CrateOwner::by_owner_kind(OwnerKind::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.gh_admin_user_ids, conn)?;
59+
.check(req, 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)