Skip to content

Commit f98c591

Browse files
authored
Merge pull request #5881 from Turbo87/auth-enum
auth: Convert `AuthenticatedUser` struct to `Authentication` enum
2 parents c0d3340 + b0c949d commit f98c591

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/auth.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl AuthCheck {
5454
}
5555
}
5656

57-
pub fn check<B>(&self, request: &Request<B>) -> AppResult<AuthenticatedUser> {
57+
pub fn check<B>(&self, request: &Request<B>) -> AppResult<Authentication> {
5858
let auth = authenticate_user(request)?;
5959

6060
if let Some(token) = auth.api_token() {
@@ -111,30 +111,47 @@ impl AuthCheck {
111111
}
112112

113113
#[derive(Debug)]
114-
pub struct AuthenticatedUser {
114+
pub enum Authentication {
115+
Cookie(CookieAuthentication),
116+
Token(TokenAuthentication),
117+
}
118+
119+
#[derive(Debug)]
120+
pub struct CookieAuthentication {
115121
user: User,
116-
token: Option<ApiToken>,
117122
}
118123

119-
impl AuthenticatedUser {
124+
#[derive(Debug)]
125+
pub struct TokenAuthentication {
126+
token: ApiToken,
127+
user: User,
128+
}
129+
130+
impl Authentication {
120131
pub fn user_id(&self) -> i32 {
121-
self.user.id
132+
self.user().id
122133
}
123134

124135
pub fn api_token_id(&self) -> Option<i32> {
125136
self.api_token().map(|token| token.id)
126137
}
127138

128139
pub fn api_token(&self) -> Option<&ApiToken> {
129-
self.token.as_ref()
140+
match self {
141+
Authentication::Token(token) => Some(&token.token),
142+
_ => None,
143+
}
130144
}
131145

132146
pub fn user(&self) -> &User {
133-
&self.user
147+
match self {
148+
Authentication::Cookie(cookie) => &cookie.user,
149+
Authentication::Token(token) => &token.user,
150+
}
134151
}
135152
}
136153

137-
fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
154+
fn authenticate_user<B>(req: &Request<B>) -> AppResult<Authentication> {
138155
controllers::util::verify_origin(req)?;
139156

140157
let conn = req.app().db_write()?;
@@ -151,7 +168,7 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
151168

152169
req.add_custom_metadata("uid", id);
153170

154-
return Ok(AuthenticatedUser { user, token: None });
171+
return Ok(Authentication::Cookie(CookieAuthentication { user }));
155172
}
156173

157174
// Otherwise, look for an `Authorization` header on the request
@@ -177,10 +194,7 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
177194
req.add_custom_metadata("uid", token.user_id);
178195
req.add_custom_metadata("tokenid", token.id);
179196

180-
return Ok(AuthenticatedUser {
181-
user,
182-
token: Some(token),
183-
});
197+
return Ok(Authentication::Token(TokenAuthentication { user, token }));
184198
}
185199

186200
// Unable to authenticate the user

src/controllers/crate_owner_invitation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::frontend_prelude::*;
22

33
use crate::auth::AuthCheck;
4-
use crate::auth::AuthenticatedUser;
4+
use crate::auth::Authentication;
55
use crate::controllers::helpers::pagination::{Page, PaginationOptions};
66
use crate::models::{Crate, CrateOwnerInvitation, Rights, User};
77
use crate::schema::{crate_owner_invitations, crates, users};
@@ -80,7 +80,7 @@ enum ListFilter {
8080

8181
fn prepare_list<B>(
8282
req: &Request<B>,
83-
auth: AuthenticatedUser,
83+
auth: Authentication,
8484
filter: ListFilter,
8585
) -> AppResult<PrivateListResponse> {
8686
let pagination: PaginationOptions = PaginationOptions::builder()

0 commit comments

Comments
 (0)