Skip to content

Commit 151f69f

Browse files
authored
Merge pull request #5886 from Turbo87/auth-fns
auth: Extract `authenticate_via_cookie/token()` fns
2 parents 0754b83 + 31f0321 commit 151f69f

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

src/auth.rs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::util::errors::{
99
account_locked, forbidden, internal, AppError, AppResult, InsecurelyGeneratedTokenRevoked,
1010
};
1111
use chrono::Utc;
12+
use diesel::PgConnection;
1213
use http::header;
1314

1415
#[derive(Debug, Clone)]
@@ -56,7 +57,7 @@ impl AuthCheck {
5657
}
5758

5859
pub fn check<T: RequestPartsExt>(&self, request: &T) -> AppResult<Authentication> {
59-
let auth = authenticate_user(request)?;
60+
let auth = authenticate(request)?;
6061

6162
if let Some(token) = auth.api_token() {
6263
if !self.allow_token {
@@ -152,50 +153,71 @@ impl Authentication {
152153
}
153154
}
154155

155-
fn authenticate_user<T: RequestPartsExt>(req: &T) -> AppResult<Authentication> {
156-
controllers::util::verify_origin(req)?;
157-
158-
let conn = req.app().db_write()?;
159-
156+
fn authenticate_via_cookie<T: RequestPartsExt>(
157+
req: &T,
158+
conn: &PgConnection,
159+
) -> AppResult<Option<CookieAuthentication>> {
160160
let user_id_from_session = req
161161
.session_get("user_id")
162162
.and_then(|s| s.parse::<i32>().ok());
163163

164-
if let Some(id) = user_id_from_session {
165-
let user = User::find(&conn, id)
166-
.map_err(|err| err.chain(internal("user_id from cookie not found in database")))?;
164+
let Some(id) = user_id_from_session else { return Ok(None) };
167165

168-
ensure_not_locked(&user)?;
166+
let user = User::find(conn, id)
167+
.map_err(|err| err.chain(internal("user_id from cookie not found in database")))?;
169168

170-
req.add_custom_metadata("uid", id);
169+
ensure_not_locked(&user)?;
171170

172-
return Ok(Authentication::Cookie(CookieAuthentication { user }));
173-
}
171+
req.add_custom_metadata("uid", id);
174172

175-
// Otherwise, look for an `Authorization` header on the request
173+
Ok(Some(CookieAuthentication { user }))
174+
}
175+
176+
fn authenticate_via_token<T: RequestPartsExt>(
177+
req: &T,
178+
conn: &PgConnection,
179+
) -> AppResult<Option<TokenAuthentication>> {
176180
let maybe_authorization = req
177181
.headers()
178182
.get(header::AUTHORIZATION)
179183
.and_then(|h| h.to_str().ok());
180184

181-
if let Some(header_value) = maybe_authorization {
182-
let token = ApiToken::find_by_api_token(&conn, header_value).map_err(|e| {
183-
if e.is::<InsecurelyGeneratedTokenRevoked>() {
184-
e
185-
} else {
186-
e.chain(internal("invalid token")).chain(forbidden())
187-
}
188-
})?;
185+
let Some(header_value) = maybe_authorization else { return Ok(None) };
189186

190-
let user = User::find(&conn, token.user_id)
191-
.map_err(|err| err.chain(internal("user_id from token not found in database")))?;
187+
let token = ApiToken::find_by_api_token(conn, header_value).map_err(|e| {
188+
if e.is::<InsecurelyGeneratedTokenRevoked>() {
189+
e
190+
} else {
191+
e.chain(internal("invalid token")).chain(forbidden())
192+
}
193+
})?;
192194

193-
ensure_not_locked(&user)?;
195+
let user = User::find(conn, token.user_id)
196+
.map_err(|err| err.chain(internal("user_id from token not found in database")))?;
194197

195-
req.add_custom_metadata("uid", token.user_id);
196-
req.add_custom_metadata("tokenid", token.id);
198+
ensure_not_locked(&user)?;
199+
200+
req.add_custom_metadata("uid", token.user_id);
201+
req.add_custom_metadata("tokenid", token.id);
202+
203+
Ok(Some(TokenAuthentication { user, token }))
204+
}
205+
206+
fn authenticate<T: RequestPartsExt>(req: &T) -> AppResult<Authentication> {
207+
controllers::util::verify_origin(req)?;
208+
209+
let conn = req.app().db_write()?;
210+
211+
match authenticate_via_cookie(req, &conn) {
212+
Ok(None) => {}
213+
Ok(Some(auth)) => return Ok(Authentication::Cookie(auth)),
214+
Err(err) => return Err(err),
215+
}
197216

198-
return Ok(Authentication::Token(TokenAuthentication { user, token }));
217+
match authenticate_via_token(req, &conn) {
218+
Ok(None) => {}
219+
Ok(Some(auth)) => return Ok(Authentication::Token(auth)),
220+
Err(err) => return Err(err),
199221
}
200222

201223
// Unable to authenticate the user

0 commit comments

Comments
 (0)