Skip to content

Commit c06e7c2

Browse files
committed
Address PR review comments
1 parent d0b9bcc commit c06e7c2

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Diff for: src/controllers/util.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ use crate::models::{ApiToken, User};
55
use crate::util::errors::{internal, AppError, AppResult, ChainError, Unauthorized};
66

77
#[derive(Debug)]
8-
pub struct AuthenticatedUser(i32, Option<i32>);
8+
pub struct AuthenticatedUser {
9+
user_id: i32,
10+
token_id: Option<i32>,
11+
}
912

1013
impl AuthenticatedUser {
1114
pub fn user_id(&self) -> i32 {
12-
self.0
15+
self.user_id
1316
}
1417

1518
pub fn api_token_id(&self) -> Option<i32> {
16-
self.1
19+
self.token_id
1720
}
1821

1922
pub fn find_user(&self, conn: &PgConnection) -> AppResult<User> {
@@ -23,16 +26,22 @@ impl AuthenticatedUser {
2326
}
2427

2528
impl<'a> UserAuthenticationExt for dyn Request + 'a {
26-
/// Obtain `CurrentUserIds` for the request or return an `Unauthorized` error
29+
/// Obtain `AuthenticatedUser` for the request or return an `Unauthorized` error
2730
fn authenticate(&self, conn: &PgConnection) -> AppResult<AuthenticatedUser> {
2831
if let Some(id) = self.extensions().find::<TrustedUserId>() {
2932
// A trusted user_id was provided by a signed cookie (or a test `MockCookieUser`)
30-
Ok(AuthenticatedUser(id.0, None))
33+
Ok(AuthenticatedUser {
34+
user_id: id.0,
35+
token_id: None,
36+
})
3137
} else {
3238
// Otherwise, look for an `Authorization` header on the request
3339
if let Some(headers) = self.headers().find("Authorization") {
3440
ApiToken::find_by_api_token(conn, headers[0])
35-
.map(|token| AuthenticatedUser(token.user_id, Some(token.id)))
41+
.map(|token| AuthenticatedUser {
42+
user_id: token.user_id,
43+
token_id: Some(token.id),
44+
})
3645
// Convert a NotFound (or other database error) into Unauthorized
3746
.map_err(|_| Box::new(Unauthorized) as Box<dyn AppError>)
3847
} else {

0 commit comments

Comments
 (0)