File tree Expand file tree Collapse file tree 2 files changed +17
-9
lines changed Expand file tree Collapse file tree 2 files changed +17
-9
lines changed Original file line number Diff line number Diff 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) ]
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ use crate::middleware::real_ip::RealIp;
66use crate :: models:: token:: EndpointScope ;
77use crate :: models:: { Crate , User } ;
88use crate :: schema:: * ;
9- use crate :: util:: errors:: { AppResult , crate_not_found, custom, forbidden } ;
9+ use crate :: util:: errors:: { AppResult , crate_not_found, custom} ;
1010use crate :: views:: EncodableCrate ;
1111use anyhow:: Context ;
1212use 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| {
You can’t perform that action at this time.
0 commit comments