Skip to content

Commit 9680a62

Browse files
committed
controllers::util: Reduce nesting
1 parent c745557 commit 9680a62

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/controllers/util.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,34 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
7070
user,
7171
token_id: None,
7272
});
73-
} else {
74-
// Otherwise, look for an `Authorization` header on the request
75-
let maybe_authorization = req
76-
.headers()
77-
.get(header::AUTHORIZATION)
78-
.and_then(|h| h.to_str().ok());
79-
80-
if let Some(header_value) = maybe_authorization {
81-
let token = ApiToken::find_by_api_token(&conn, header_value).map_err(|e| {
82-
if e.is::<InsecurelyGeneratedTokenRevoked>() {
83-
e
84-
} else {
85-
e.chain(internal("invalid token")).chain(forbidden())
86-
}
87-
})?;
88-
89-
let user = User::find(&conn, token.user_id)
90-
.chain_error(|| internal("user_id from token not found in database"))?;
91-
92-
return Ok(AuthenticatedUser {
93-
user,
94-
token_id: Some(token.id),
95-
});
96-
} else {
97-
// Unable to authenticate the user
98-
return Err(internal("no cookie session or auth header found")).chain_error(forbidden);
99-
}
10073
}
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+
})?;
89+
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+
}
98+
99+
// Unable to authenticate the user
100+
return Err(internal("no cookie session or auth header found")).chain_error(forbidden);
101101
}
102102

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

0 commit comments

Comments
 (0)