Skip to content

Commit faf22a3

Browse files
authored
auth: Extract reject_legacy_tokens() fn (#12432)
1 parent 64fe1bf commit faf22a3

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/auth.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ impl Authentication {
219219
Authentication::Token(token) => &token.user,
220220
}
221221
}
222+
223+
/// Returns an error if the request was authenticated with a legacy API token.
224+
///
225+
/// Legacy tokens are tokens without any endpoint scopes. They were created
226+
/// before the scoped token feature was introduced.
227+
pub fn reject_legacy_tokens(&self) -> AppResult<()> {
228+
if let Some(token) = self.api_token()
229+
&& token.endpoint_scopes.is_none()
230+
{
231+
return Err(forbidden(
232+
"This endpoint cannot be used with legacy API tokens. Use a scoped API token instead.",
233+
));
234+
}
235+
Ok(())
236+
}
222237
}
223238

224239
#[instrument(skip_all)]

src/controllers/krate/update.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::middleware::real_ip::RealIp;
66
use crate::models::token::EndpointScope;
77
use crate::models::{Crate, User};
88
use crate::schema::*;
9-
use crate::util::errors::{AppResult, crate_not_found, custom, forbidden};
9+
use crate::util::errors::{AppResult, crate_not_found, custom};
1010
use crate::views::EncodableCrate;
1111
use anyhow::Context;
1212
use axum::{Extension, Json};
@@ -70,14 +70,7 @@ pub async fn update_crate(
7070
.check(&req, &mut conn)
7171
.await?;
7272

73-
if auth
74-
.api_token()
75-
.is_some_and(|token| token.endpoint_scopes.is_none())
76-
{
77-
return Err(forbidden(
78-
"This endpoint cannot be used with legacy API tokens. Use a scoped API token instead.",
79-
));
80-
}
73+
auth.reject_legacy_tokens()?;
8174

8275
// Update crate settings in a transaction
8376
conn.transaction(|conn| {

0 commit comments

Comments
 (0)