Skip to content
Open
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
4 changes: 2 additions & 2 deletions app-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Start with image that has the Rust toolchain installed
FROM rust:1.90-alpine AS chef
FROM rust:1.91-alpine AS chef
USER root
# Add cargo-chef to cache dependencies
RUN apk add --no-cache musl-dev & cargo install cargo-chef
RUN apk add --no-cache musl-dev && cargo install cargo-chef --locked
WORKDIR /app

FROM chef AS planner
Expand Down
4 changes: 2 additions & 2 deletions auth-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Start with image that has the Rust toolchain installed
FROM rust:1.90-alpine AS chef
FROM rust:1.91-alpine AS chef
USER root
# Add cargo-chef to cache dependencies
RUN apk add --no-cache musl-dev & cargo install cargo-chef
RUN apk add --no-cache musl-dev && cargo install cargo-chef --locked
WORKDIR /app

FROM chef AS planner
Expand Down
14 changes: 7 additions & 7 deletions auth-service/src/routes/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@ pub async fn login(
State(state): State<AppState>,
jar: CookieJar,
Json(request): Json<LoginRequest>,
) -> (CookieJar, Result<impl IntoResponse, AuthAPIError>) {
) -> Result<(CookieJar, impl IntoResponse), AuthAPIError> {
let password = match Password::parse(request.password) {
Ok(password) => password,
Err(_) => return (jar, Err(AuthAPIError::InvalidCredentials)),
Err(_) => return Err(AuthAPIError::InvalidCredentials),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now do something like

let password = match Password::parse(request.password).map_err(|_| => AuthAPIError::InvalidCredentials)?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, looks more idiomatic, thank you for the suggestion.
I will update it to

let password = Password::parse(request.password).map_err(|_| AuthAPIError::InvalidCredentials)?;

};

let email = match Email::parse(request.email) {
Ok(email) => email,
Err(_) => return (jar, Err(AuthAPIError::InvalidCredentials)),
Err(_) => return Err(AuthAPIError::InvalidCredentials),
};

let user_store = &state.user_store.read().await;

if user_store.validate_user(&email, &password).await.is_err() {
return (jar, Err(AuthAPIError::IncorrectCredentials));
return Err(AuthAPIError::IncorrectCredentials);
}

let user = match user_store.get_user(&email).await {
Ok(user) => user,
Err(_) => return (jar, Err(AuthAPIError::IncorrectCredentials)),
Err(_) => return Err(AuthAPIError::IncorrectCredentials),
};

let auth_cookie = match generate_auth_cookie(&user.email) {
Ok(cookie) => cookie,
Err(_) => return (jar, Err(AuthAPIError::UnexpectedError)),
Err(_) => return Err(AuthAPIError::UnexpectedError),
};

let updated_jar = jar.add(auth_cookie);

(updated_jar, Ok(StatusCode::OK.into_response()))
Ok((updated_jar, StatusCode::OK.into_response()))
}

#[derive(Deserialize)]
Expand Down
10 changes: 5 additions & 5 deletions auth-service/src/routes/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ use crate::{
pub async fn logout(
State(state): State<AppState>,
jar: CookieJar,
) -> (CookieJar, Result<impl IntoResponse, AuthAPIError>) {
) -> Result<(CookieJar, impl IntoResponse), AuthAPIError> {
let cookie = match jar.get(JWT_COOKIE_NAME) {
Some(cookie) => cookie,
None => return (jar, Err(AuthAPIError::MissingToken)),
None => return Err(AuthAPIError::MissingToken),
};

// Validate token
let token = cookie.value().to_owned();
let _ = match validate_token(&token, state.banned_token_store.clone()).await {
Ok(claims) => claims,
Err(_) => return (jar, Err(AuthAPIError::InvalidToken)),
Err(_) => return Err(AuthAPIError::InvalidToken),
};

// Add token to banned list
Expand All @@ -32,11 +32,11 @@ pub async fn logout(
.await
.is_err()
{
return (jar, Err(AuthAPIError::UnexpectedError));
return Err(AuthAPIError::UnexpectedError);
}

// Remove jwt cookie
let jar = jar.remove(cookie::Cookie::from(JWT_COOKIE_NAME));

(jar, Ok(StatusCode::OK))
Ok((jar, StatusCode::OK))
}