Skip to content

Commit 4e9e8ed

Browse files
committed
AuthenticatedUser: Replace token_id field with full token
This will be needed to check the token scopes.
1 parent d702b01 commit 4e9e8ed

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/auth.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl AuthCheck {
4747
log_request::add_custom_metadata("tokenid", id);
4848
}
4949

50-
if !self.allow_token && auth.token_id.is_some() {
50+
if !self.allow_token && auth.token.is_some() {
5151
let error_message = "API Token authentication was explicitly disallowed for this API";
5252
return Err(internal(error_message).chain(forbidden()));
5353
}
@@ -59,7 +59,7 @@ impl AuthCheck {
5959
#[derive(Debug)]
6060
pub struct AuthenticatedUser {
6161
user: User,
62-
token_id: Option<i32>,
62+
token: Option<ApiToken>,
6363
}
6464

6565
impl AuthenticatedUser {
@@ -68,7 +68,11 @@ impl AuthenticatedUser {
6868
}
6969

7070
pub fn api_token_id(&self) -> Option<i32> {
71-
self.token_id
71+
self.api_token().map(|token| token.id)
72+
}
73+
74+
pub fn api_token(&self) -> Option<&ApiToken> {
75+
self.token.as_ref()
7276
}
7377

7478
pub fn user(self) -> User {
@@ -86,10 +90,7 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
8690
let user = User::find(&conn, id)
8791
.map_err(|err| err.chain(internal("user_id from cookie not found in database")))?;
8892

89-
return Ok(AuthenticatedUser {
90-
user,
91-
token_id: None,
92-
});
93+
return Ok(AuthenticatedUser { user, token: None });
9394
}
9495

9596
// Otherwise, look for an `Authorization` header on the request
@@ -112,7 +113,7 @@ fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
112113

113114
return Ok(AuthenticatedUser {
114115
user,
115-
token_id: Some(token.id),
116+
token: Some(token),
116117
});
117118
}
118119

0 commit comments

Comments
 (0)