Skip to content

Commit 4d04474

Browse files
committed
auth: Remove obsolete blocking fns
1 parent 9fe0f5e commit 4d04474

File tree

1 file changed

+0
-124
lines changed

1 file changed

+0
-124
lines changed

src/auth.rs

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::middleware::log_request::RequestLogExt;
44
use crate::middleware::session::RequestSession;
55
use crate::models::token::{CrateScope, EndpointScope};
66
use crate::models::{ApiToken, User};
7-
use crate::util::diesel::Conn;
87
use crate::util::errors::{
98
account_locked, forbidden, internal, AppResult, InsecurelyGeneratedTokenRevoked,
109
};
@@ -58,43 +57,6 @@ impl AuthCheck {
5857
}
5958
}
6059

61-
#[instrument(name = "auth.check", skip_all)]
62-
pub fn check(&self, parts: &Parts, conn: &mut impl Conn) -> AppResult<Authentication> {
63-
let auth = authenticate(parts, conn)?;
64-
65-
if let Some(token) = auth.api_token() {
66-
if !self.allow_token {
67-
let error_message =
68-
"API Token authentication was explicitly disallowed for this API";
69-
parts.request_log().add("cause", error_message);
70-
71-
return Err(forbidden(
72-
"this action can only be performed on the crates.io website",
73-
));
74-
}
75-
76-
if !self.endpoint_scope_matches(token.endpoint_scopes.as_ref()) {
77-
let error_message = "Endpoint scope mismatch";
78-
parts.request_log().add("cause", error_message);
79-
80-
return Err(forbidden(
81-
"this token does not have the required permissions to perform this action",
82-
));
83-
}
84-
85-
if !self.crate_scope_matches(token.crate_scopes.as_ref()) {
86-
let error_message = "Crate scope mismatch";
87-
parts.request_log().add("cause", error_message);
88-
89-
return Err(forbidden(
90-
"this token does not have the required permissions to perform this action",
91-
));
92-
}
93-
}
94-
95-
Ok(auth)
96-
}
97-
9860
#[instrument(name = "auth.async_check", skip_all)]
9961
pub async fn async_check(
10062
&self,
@@ -209,32 +171,6 @@ impl Authentication {
209171
}
210172
}
211173

212-
#[instrument(skip_all)]
213-
fn authenticate_via_cookie(
214-
parts: &Parts,
215-
conn: &mut impl Conn,
216-
) -> AppResult<Option<CookieAuthentication>> {
217-
let user_id_from_session = parts
218-
.session()
219-
.get("user_id")
220-
.and_then(|s| s.parse::<i32>().ok());
221-
222-
let Some(id) = user_id_from_session else {
223-
return Ok(None);
224-
};
225-
226-
let user = User::find(conn, id).map_err(|err| {
227-
parts.request_log().add("cause", err);
228-
internal("user_id from cookie not found in database")
229-
})?;
230-
231-
ensure_not_locked(&user)?;
232-
233-
parts.request_log().add("uid", id);
234-
235-
Ok(Some(CookieAuthentication { user }))
236-
}
237-
238174
#[instrument(skip_all)]
239175
async fn async_authenticate_via_cookie(
240176
parts: &Parts,
@@ -261,43 +197,6 @@ async fn async_authenticate_via_cookie(
261197
Ok(Some(CookieAuthentication { user }))
262198
}
263199

264-
#[instrument(skip_all)]
265-
fn authenticate_via_token(
266-
parts: &Parts,
267-
conn: &mut impl Conn,
268-
) -> AppResult<Option<TokenAuthentication>> {
269-
let maybe_authorization = parts
270-
.headers()
271-
.get(header::AUTHORIZATION)
272-
.and_then(|h| h.to_str().ok());
273-
274-
let Some(header_value) = maybe_authorization else {
275-
return Ok(None);
276-
};
277-
278-
let token =
279-
HashedToken::parse(header_value).map_err(|_| InsecurelyGeneratedTokenRevoked::boxed())?;
280-
281-
let token = ApiToken::find_by_api_token(conn, &token).map_err(|e| {
282-
let cause = format!("invalid token caused by {e}");
283-
parts.request_log().add("cause", cause);
284-
285-
forbidden("authentication failed")
286-
})?;
287-
288-
let user = User::find(conn, token.user_id).map_err(|err| {
289-
parts.request_log().add("cause", err);
290-
internal("user_id from token not found in database")
291-
})?;
292-
293-
ensure_not_locked(&user)?;
294-
295-
parts.request_log().add("uid", token.user_id);
296-
parts.request_log().add("tokenid", token.id);
297-
298-
Ok(Some(TokenAuthentication { user, token }))
299-
}
300-
301200
#[instrument(skip_all)]
302201
async fn async_authenticate_via_token(
303202
parts: &Parts,
@@ -337,29 +236,6 @@ async fn async_authenticate_via_token(
337236
Ok(Some(TokenAuthentication { user, token }))
338237
}
339238

340-
#[instrument(skip_all)]
341-
fn authenticate(parts: &Parts, conn: &mut impl Conn) -> AppResult<Authentication> {
342-
controllers::util::verify_origin(parts)?;
343-
344-
match authenticate_via_cookie(parts, conn) {
345-
Ok(None) => {}
346-
Ok(Some(auth)) => return Ok(Authentication::Cookie(auth)),
347-
Err(err) => return Err(err),
348-
}
349-
350-
match authenticate_via_token(parts, conn) {
351-
Ok(None) => {}
352-
Ok(Some(auth)) => return Ok(Authentication::Token(auth)),
353-
Err(err) => return Err(err),
354-
}
355-
356-
// Unable to authenticate the user
357-
let cause = "no cookie session or auth header found";
358-
parts.request_log().add("cause", cause);
359-
360-
return Err(forbidden("this action requires authentication"));
361-
}
362-
363239
#[instrument(skip_all)]
364240
async fn async_authenticate(
365241
parts: &Parts,

0 commit comments

Comments
 (0)