Skip to content

Commit

Permalink
Implement Authentication #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-w1lde committed Nov 5, 2024
1 parent b015920 commit 68df7ba
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 53 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.authentication.dto.LoginRequest;
import net.furizon.backend.feature.authentication.dto.LoginResponse;
import net.furizon.backend.feature.authentication.dto.LogoutUserResponse;
import net.furizon.backend.feature.authentication.dto.RegisterUserRequest;
import net.furizon.backend.feature.authentication.dto.RegisterUserResponse;
import net.furizon.backend.feature.authentication.usecase.CreateLoginSessionUseCase;
import net.furizon.backend.feature.authentication.usecase.LoginUserUseCase;
import net.furizon.backend.feature.authentication.usecase.LogoutUserUseCase;
import net.furizon.backend.feature.authentication.usecase.RegisterUserUseCase;
import net.furizon.backend.infrastructure.security.FurizonUser;
import net.furizon.backend.infrastructure.usecase.UseCaseExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
Expand All @@ -30,8 +35,8 @@ public LoginResponse loginUser(
HttpServletRequest httpServletRequest
) {
return executor.execute(
CreateLoginSessionUseCase.class,
new CreateLoginSessionUseCase.Input(
LoginUserUseCase.class,
new LoginUserUseCase.Input(
loginRequest.getEmail(),
loginRequest.getPassword(),
httpServletRequest.getRemoteAddr(),
Expand All @@ -40,6 +45,19 @@ public LoginResponse loginUser(
);
}

@PostMapping("/logout")
public LogoutUserResponse logoutUser(
@AuthenticationPrincipal @NotNull final FurizonUser user
) {
executor.execute(
LogoutUserUseCase.class,
new LogoutUserUseCase.Input(user.getSessionId())
);

return LogoutUserResponse.SUCCESS;
}


@PostMapping("/register")
public RegisterUserResponse registerUser(
@RequestBody final RegisterUserRequest registerUserRequest
Expand All @@ -49,6 +67,6 @@ public RegisterUserResponse registerUser(
registerUserRequest
);

return new RegisterUserResponse(true);
return RegisterUserResponse.SUCCESS;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.furizon.backend.feature.authentication.dto;

import lombok.Data;

@Data
public class LogoutUserResponse {
public static final LogoutUserResponse SUCCESS = new LogoutUserResponse(true);

private final boolean success;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

@Data
public class RegisterUserResponse {
public static final RegisterUserResponse SUCCESS = new RegisterUserResponse(true);

private final boolean success;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.furizon.backend.feature.authentication.action.createSession.CreateSessionAction;
import net.furizon.backend.infrastructure.security.session.action.createSession.CreateSessionAction;
import net.furizon.backend.feature.authentication.dto.LoginResponse;
import net.furizon.backend.feature.authentication.validation.CreateLoginSessionValidation;
import net.furizon.backend.infrastructure.security.SecurityConfig;
Expand All @@ -19,7 +19,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
public class CreateLoginSessionUseCase implements UseCase<CreateLoginSessionUseCase.Input, LoginResponse> {
public class LoginUserUseCase implements UseCase<LoginUserUseCase.Input, LoginResponse> {
private final CreateLoginSessionValidation validation;

private final CreateSessionAction createSessionAction;
Expand All @@ -34,7 +34,7 @@ public class CreateLoginSessionUseCase implements UseCase<CreateLoginSessionUseC

@Transactional
@Override
public @NotNull LoginResponse executor(@NotNull CreateLoginSessionUseCase.Input input) {
public @NotNull LoginResponse executor(@NotNull LoginUserUseCase.Input input) {
final var userId = validation.validateAndGetUserId(input);
int sessionsCount = sessionFinder.getUserSessionsCount(userId);
if (sessionsCount >= securityConfig.getSession().getMaxAllowedSessionsSize()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.furizon.backend.feature.authentication.usecase;

import lombok.RequiredArgsConstructor;
import net.furizon.backend.infrastructure.security.session.action.deleteSession.DeleteSessionAction;
import net.furizon.backend.infrastructure.usecase.UseCase;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Component
@RequiredArgsConstructor
public class LogoutUserUseCase implements UseCase<LogoutUserUseCase.Input, Boolean> {
private final DeleteSessionAction deleteSessionAction;

@Transactional
@Override
public @NotNull Boolean executor(@NotNull Input input) {
deleteSessionAction.invoke(input.sessionId);

return true;
}


public record Input(@NotNull UUID sessionId) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.authentication.AuthenticationErrorCode;
import net.furizon.backend.feature.authentication.finder.AuthenticationFinder;
import net.furizon.backend.feature.authentication.usecase.CreateLoginSessionUseCase;
import net.furizon.backend.feature.authentication.usecase.LoginUserUseCase;
import net.furizon.backend.infrastructure.security.SecurityConfig;
import net.furizon.backend.infrastructure.web.exception.ApiException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,7 +19,7 @@ public class CreateLoginSessionValidation {

private final PasswordEncoder passwordEncoder;

public long validateAndGetUserId(@NotNull CreateLoginSessionUseCase.Input input) {
public long validateAndGetUserId(@NotNull LoginUserUseCase.Input input) {
AuthenticationException.validateEmailOrThrow(input.email());

final var authentication = authenticationFinder.findByEmail(input.email());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class UserController {

@GetMapping("/me")
public FurizonUser getMe(
@AuthenticationPrincipal @NotNull FurizonUser user
@AuthenticationPrincipal @NotNull final FurizonUser user
) {
return user;
}

@GetMapping("/me/sessions")
public List<UserSession> getMeSessions(
@AuthenticationPrincipal @NotNull FurizonUser user
@AuthenticationPrincipal @NotNull final FurizonUser user
) {
return executor.execute(
GetUserSessionsUseCase.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.furizon.backend.feature.authentication.action.createSession;
package net.furizon.backend.infrastructure.security.session.action.createSession;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.furizon.backend.feature.authentication.action.createSession;
package net.furizon.backend.infrastructure.security.session.action.createSession;

import com.github.f4b6a3.uuid.UuidCreator;
import lombok.RequiredArgsConstructor;
Expand Down

0 comments on commit 68df7ba

Please sign in to comment.