Skip to content

Commit 220b2a0

Browse files
committed
feat(labrinth/delphi): use PAT token authorization with project read scopes
1 parent 7555a39 commit 220b2a0

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

apps/labrinth/src/routes/internal/delphi.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::{collections::HashMap, fmt::Write, sync::LazyLock};
22

3-
use actix_web::{HttpResponse, get, post, put, web};
3+
use actix_web::{HttpRequest, HttpResponse, get, post, put, web};
44
use chrono::{DateTime, Utc};
55
use serde::Deserialize;
66
use sqlx::PgPool;
77
use tracing::info;
88

99
use crate::{
10+
auth::check_is_moderator_from_headers,
1011
database::{
1112
models::{
1213
DBFileId, DelphiReportId, DelphiReportIssueId,
@@ -20,6 +21,8 @@ use crate::{
2021
},
2122
redis::RedisPool,
2223
},
24+
models::pats::Scopes,
25+
queue::session::AuthQueue,
2326
routes::ApiError,
2427
util::guards::admin_key_guard,
2528
};
@@ -184,16 +187,42 @@ pub async fn run(
184187
Ok(HttpResponse::NoContent().finish())
185188
}
186189

187-
#[post("run", guard = "admin_key_guard")]
190+
#[post("run")]
188191
async fn _run(
192+
req: HttpRequest,
189193
pool: web::Data<PgPool>,
194+
redis: web::Data<RedisPool>,
195+
session_queue: web::Data<AuthQueue>,
190196
run_parameters: web::Query<DelphiRunParameters>,
191197
) -> Result<HttpResponse, ApiError> {
198+
check_is_moderator_from_headers(
199+
&req,
200+
&**pool,
201+
&redis,
202+
&session_queue,
203+
Scopes::PROJECT_READ,
204+
)
205+
.await?;
206+
192207
run(&**pool, run_parameters.into_inner()).await
193208
}
194209

195-
#[get("version", guard = "admin_key_guard")]
196-
async fn version(pool: web::Data<PgPool>) -> Result<HttpResponse, ApiError> {
210+
#[get("version")]
211+
async fn version(
212+
req: HttpRequest,
213+
pool: web::Data<PgPool>,
214+
redis: web::Data<RedisPool>,
215+
session_queue: web::Data<AuthQueue>,
216+
) -> Result<HttpResponse, ApiError> {
217+
check_is_moderator_from_headers(
218+
&req,
219+
&**pool,
220+
&redis,
221+
&session_queue,
222+
Scopes::PROJECT_READ,
223+
)
224+
.await?;
225+
197226
Ok(HttpResponse::Ok().json(
198227
sqlx::query_scalar!("SELECT MAX(delphi_version) FROM delphi_reports")
199228
.fetch_one(&**pool)
@@ -211,11 +240,23 @@ struct DelphiIssuesSearchOptions {
211240
offset: Option<u64>,
212241
}
213242

214-
#[get("issues", guard = "admin_key_guard")]
243+
#[get("issues")]
215244
async fn issues(
245+
req: HttpRequest,
216246
pool: web::Data<PgPool>,
247+
redis: web::Data<RedisPool>,
248+
session_queue: web::Data<AuthQueue>,
217249
search_options: web::Query<DelphiIssuesSearchOptions>,
218250
) -> Result<HttpResponse, ApiError> {
251+
check_is_moderator_from_headers(
252+
&req,
253+
&**pool,
254+
&redis,
255+
&session_queue,
256+
Scopes::PROJECT_READ,
257+
)
258+
.await?;
259+
219260
Ok(HttpResponse::Ok().json(
220261
DBDelphiReportIssue::find_all_by(
221262
search_options.ty,
@@ -235,12 +276,24 @@ async fn issues(
235276
))
236277
}
237278

238-
#[put("issue/{issue_id}", guard = "admin_key_guard")]
279+
#[put("issue/{issue_id}")]
239280
async fn update_issue(
281+
req: HttpRequest,
240282
pool: web::Data<PgPool>,
283+
redis: web::Data<RedisPool>,
284+
session_queue: web::Data<AuthQueue>,
241285
issue_id: web::Path<DelphiReportIssueId>,
242286
web::Json(update_data): web::Json<DBDelphiReportIssue>,
243287
) -> Result<HttpResponse, ApiError> {
288+
check_is_moderator_from_headers(
289+
&req,
290+
&**pool,
291+
&redis,
292+
&session_queue,
293+
Scopes::PROJECT_READ,
294+
)
295+
.await?;
296+
244297
let new_id = issue_id.into_inner();
245298

246299
let mut transaction = pool.begin().await?;

0 commit comments

Comments
 (0)