Skip to content

Commit 79e30af

Browse files
committed
AuthenticatedUser: Inline UserAuthenticationExt trait and implementation
1 parent d64f5a4 commit 79e30af

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

src/auth.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,25 @@ impl AuthCheck {
2727
}
2828

2929
pub fn check(&self, request: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
30-
let auth = request.authenticate()?;
30+
controllers::util::verify_origin(request)?;
31+
32+
let auth = authenticate_user(request)?;
33+
34+
if let Some(reason) = &auth.user.account_lock_reason {
35+
let still_locked = if let Some(until) = auth.user.account_lock_until {
36+
until > Utc::now().naive_utc()
37+
} else {
38+
true
39+
};
40+
if still_locked {
41+
return Err(account_locked(reason, auth.user.account_lock_until));
42+
}
43+
}
44+
45+
log_request::add_custom_metadata("uid", auth.user_id());
46+
if let Some(id) = auth.api_token_id() {
47+
log_request::add_custom_metadata("tokenid", id);
48+
}
3149

3250
if !self.allow_token && auth.token_id.is_some() {
3351
let error_message = "API Token authentication was explicitly disallowed for this API";
@@ -101,37 +119,3 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
101119
// Unable to authenticate the user
102120
return Err(internal("no cookie session or auth header found").chain(forbidden()));
103121
}
104-
105-
pub trait UserAuthenticationExt {
106-
fn authenticate(&self) -> AppResult<AuthenticatedUser>;
107-
}
108-
109-
impl<'a> UserAuthenticationExt for dyn RequestExt + 'a {
110-
/// Obtain `AuthenticatedUser` for the request or return an `Forbidden` error
111-
fn authenticate(&self) -> AppResult<AuthenticatedUser> {
112-
controllers::util::verify_origin(self)?;
113-
114-
let authenticated_user = authenticate_user(self)?;
115-
116-
if let Some(reason) = &authenticated_user.user.account_lock_reason {
117-
let still_locked = if let Some(until) = authenticated_user.user.account_lock_until {
118-
until > Utc::now().naive_utc()
119-
} else {
120-
true
121-
};
122-
if still_locked {
123-
return Err(account_locked(
124-
reason,
125-
authenticated_user.user.account_lock_until,
126-
));
127-
}
128-
}
129-
130-
log_request::add_custom_metadata("uid", authenticated_user.user_id());
131-
if let Some(id) = authenticated_user.api_token_id() {
132-
log_request::add_custom_metadata("tokenid", id);
133-
}
134-
135-
Ok(authenticated_user)
136-
}
137-
}

src/controllers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ mod prelude {
1515
pub use conduit::{header, RequestExt, StatusCode};
1616
pub use conduit_router::RequestParams;
1717

18-
pub use crate::auth::UserAuthenticationExt;
1918
pub use crate::db::RequestTransaction;
2019
pub use crate::middleware::app::RequestApp;
2120
pub use crate::util::errors::{cargo_err, AppError, AppResult}; // TODO: Remove cargo_err from here

0 commit comments

Comments
 (0)