Skip to content

Commit 5e17d5e

Browse files
committed
Auto merge of #3328 - Turbo87:authenticate-user, r=JohnTitor
Simplify `authenticate_user()` function The function was a bit hard to read and difficult to extend (see #3307). This PR simplifies it a little bit by returning early if a suitable authentication method was found. r? `@jtgeibel`
2 parents 5d9766a + 9680a62 commit 5e17d5e

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/controllers/util.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,42 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
6262
let session = req.session();
6363
let user_id_from_session = session.get("user_id").and_then(|s| s.parse::<i32>().ok());
6464

65-
let (user_id, token_id) = if let Some(id) = user_id_from_session {
66-
(id, None)
67-
} else {
68-
// Otherwise, look for an `Authorization` header on the request
69-
let maybe_authorization: Option<String> = {
70-
req.headers()
71-
.get(header::AUTHORIZATION)
72-
.and_then(|h| h.to_str().ok())
73-
.map(|h| h.to_string())
74-
};
75-
if let Some(header_value) = maybe_authorization {
76-
let (user_id, token_id) = ApiToken::find_by_api_token(&conn, &header_value)
77-
.map(|token| (token.user_id, Some(token.id)))
78-
.map_err(|e| {
79-
if e.is::<InsecurelyGeneratedTokenRevoked>() {
80-
e
81-
} else {
82-
e.chain(internal("invalid token")).chain(forbidden())
83-
}
84-
})?;
85-
86-
(user_id, token_id)
87-
} else {
88-
// Unable to authenticate the user
89-
return Err(internal("no cookie session or auth header found")).chain_error(forbidden);
90-
}
91-
};
65+
if let Some(id) = user_id_from_session {
66+
let user = User::find(&conn, id)
67+
.chain_error(|| internal("user_id from cookie not found in database"))?;
68+
69+
return Ok(AuthenticatedUser {
70+
user,
71+
token_id: None,
72+
});
73+
}
74+
75+
// Otherwise, look for an `Authorization` header on the request
76+
let maybe_authorization = req
77+
.headers()
78+
.get(header::AUTHORIZATION)
79+
.and_then(|h| h.to_str().ok());
80+
81+
if let Some(header_value) = maybe_authorization {
82+
let token = ApiToken::find_by_api_token(&conn, header_value).map_err(|e| {
83+
if e.is::<InsecurelyGeneratedTokenRevoked>() {
84+
e
85+
} else {
86+
e.chain(internal("invalid token")).chain(forbidden())
87+
}
88+
})?;
9289

93-
let user = User::find(&conn, user_id)
94-
.chain_error(|| internal("user_id from cookie or token not found in database"))?;
90+
let user = User::find(&conn, token.user_id)
91+
.chain_error(|| internal("user_id from token not found in database"))?;
92+
93+
return Ok(AuthenticatedUser {
94+
user,
95+
token_id: Some(token.id),
96+
});
97+
}
9598

96-
Ok(AuthenticatedUser { user, token_id })
99+
// Unable to authenticate the user
100+
return Err(internal("no cookie session or auth header found")).chain_error(forbidden);
97101
}
98102

99103
impl<'a> UserAuthenticationExt for dyn RequestExt + 'a {

src/tests/authentication.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static URL: &str = "/api/v1/me/updates";
88
static MUST_LOGIN: &[u8] =
99
b"{\"errors\":[{\"detail\":\"must be logged in to perform that action\"}]}";
1010
static INTERNAL_ERROR_NO_USER: &str =
11-
"user_id from cookie or token not found in database caused by NotFound";
11+
"user_id from cookie not found in database caused by NotFound";
1212

1313
fn call(app: &TestApp, mut request: MockRequest) -> HandlerResult {
1414
app.as_middleware().call(&mut request)

0 commit comments

Comments
 (0)