diff --git a/src/controllers/util.rs b/src/controllers/util.rs index 9f25147d6b1..d0cb1e32bc1 100644 --- a/src/controllers/util.rs +++ b/src/controllers/util.rs @@ -62,38 +62,42 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult { let session = req.session(); let user_id_from_session = session.get("user_id").and_then(|s| s.parse::().ok()); - let (user_id, token_id) = if let Some(id) = user_id_from_session { - (id, None) - } else { - // Otherwise, look for an `Authorization` header on the request - let maybe_authorization: Option = { - req.headers() - .get(header::AUTHORIZATION) - .and_then(|h| h.to_str().ok()) - .map(|h| h.to_string()) - }; - if let Some(header_value) = maybe_authorization { - let (user_id, token_id) = ApiToken::find_by_api_token(&conn, &header_value) - .map(|token| (token.user_id, Some(token.id))) - .map_err(|e| { - if e.is::() { - e - } else { - e.chain(internal("invalid token")).chain(forbidden()) - } - })?; - - (user_id, token_id) - } else { - // Unable to authenticate the user - return Err(internal("no cookie session or auth header found")).chain_error(forbidden); - } - }; + if let Some(id) = user_id_from_session { + let user = User::find(&conn, id) + .chain_error(|| internal("user_id from cookie not found in database"))?; + + return Ok(AuthenticatedUser { + user, + token_id: None, + }); + } + + // Otherwise, look for an `Authorization` header on the request + let maybe_authorization = req + .headers() + .get(header::AUTHORIZATION) + .and_then(|h| h.to_str().ok()); + + if let Some(header_value) = maybe_authorization { + let token = ApiToken::find_by_api_token(&conn, header_value).map_err(|e| { + if e.is::() { + e + } else { + e.chain(internal("invalid token")).chain(forbidden()) + } + })?; - let user = User::find(&conn, user_id) - .chain_error(|| internal("user_id from cookie or token not found in database"))?; + let user = User::find(&conn, token.user_id) + .chain_error(|| internal("user_id from token not found in database"))?; + + return Ok(AuthenticatedUser { + user, + token_id: Some(token.id), + }); + } - Ok(AuthenticatedUser { user, token_id }) + // Unable to authenticate the user + return Err(internal("no cookie session or auth header found")).chain_error(forbidden); } impl<'a> UserAuthenticationExt for dyn RequestExt + 'a { diff --git a/src/tests/authentication.rs b/src/tests/authentication.rs index 23a0340bff0..bc63ad80153 100644 --- a/src/tests/authentication.rs +++ b/src/tests/authentication.rs @@ -8,7 +8,7 @@ static URL: &str = "/api/v1/me/updates"; static MUST_LOGIN: &[u8] = b"{\"errors\":[{\"detail\":\"must be logged in to perform that action\"}]}"; static INTERNAL_ERROR_NO_USER: &str = - "user_id from cookie or token not found in database caused by NotFound"; + "user_id from cookie not found in database caused by NotFound"; fn call(app: &TestApp, mut request: MockRequest) -> HandlerResult { app.as_middleware().call(&mut request)