Skip to content

auth: Convert AuthenticatedUser struct to Authentication enum #5881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl AuthCheck {
}
}

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

if let Some(token) = auth.api_token() {
Expand Down Expand Up @@ -111,30 +111,47 @@ impl AuthCheck {
}

#[derive(Debug)]
pub struct AuthenticatedUser {
pub enum Authentication {
Cookie(CookieAuthentication),
Token(TokenAuthentication),
}

#[derive(Debug)]
pub struct CookieAuthentication {
user: User,
token: Option<ApiToken>,
}

impl AuthenticatedUser {
#[derive(Debug)]
pub struct TokenAuthentication {
token: ApiToken,
user: User,
}

impl Authentication {
pub fn user_id(&self) -> i32 {
self.user.id
self.user().id
}

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

pub fn api_token(&self) -> Option<&ApiToken> {
self.token.as_ref()
match self {
Authentication::Token(token) => Some(&token.token),
_ => None,
}
}

pub fn user(&self) -> &User {
&self.user
match self {
Authentication::Cookie(cookie) => &cookie.user,
Authentication::Token(token) => &token.user,
}
}
}

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

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

req.add_custom_metadata("uid", id);

return Ok(AuthenticatedUser { user, token: None });
return Ok(Authentication::Cookie(CookieAuthentication { user }));
}

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

return Ok(AuthenticatedUser {
user,
token: Some(token),
});
return Ok(Authentication::Token(TokenAuthentication { user, token }));
}

// Unable to authenticate the user
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::frontend_prelude::*;

use crate::auth::AuthCheck;
use crate::auth::AuthenticatedUser;
use crate::auth::Authentication;
use crate::controllers::helpers::pagination::{Page, PaginationOptions};
use crate::models::{Crate, CrateOwnerInvitation, Rights, User};
use crate::schema::{crate_owner_invitations, crates, users};
Expand Down Expand Up @@ -80,7 +80,7 @@ enum ListFilter {

fn prepare_list<B>(
req: &Request<B>,
auth: AuthenticatedUser,
auth: Authentication,
filter: ListFilter,
) -> AppResult<PrivateListResponse> {
let pagination: PaginationOptions = PaginationOptions::builder()
Expand Down