From 3db9e93ba7e9ffd1f31a62c1fd3d5d551fb21801 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 6 Dec 2024 21:00:01 +0300 Subject: [PATCH] Code cleanup --- .../foxogram/controllers/AuthenticationController.java | 9 +++------ .../su/foxogram/controllers/ChannelsController.java | 8 ++++---- .../su/foxogram/controllers/MessagesController.java | 7 +++---- .../java/su/foxogram/controllers/UsersController.java | 10 ++++------ .../main/java/su/foxogram/services/UsersService.java | 8 ++------ .../interceptors/AuthenticationInterceptor.java | 4 ++-- .../su/foxogram/services/AuthenticationService.java | 10 ++-------- 7 files changed, 20 insertions(+), 36 deletions(-) diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/AuthenticationController.java b/foxogram-api/src/main/java/su/foxogram/controllers/AuthenticationController.java index 7765163..969b759 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/AuthenticationController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/AuthenticationController.java @@ -2,7 +2,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -17,8 +16,6 @@ import su.foxogram.models.User; import su.foxogram.services.AuthenticationService; -import java.security.NoSuchAlgorithmException; - @Slf4j @RestController @Tag(name = "Authentication") @@ -33,7 +30,7 @@ public AuthenticationController(AuthenticationService authenticationService) { @Operation(summary = "Register") @PostMapping("/register") - public TokenDTO register(@Valid @RequestBody UserSignUpDTO body) throws UserCredentialsDuplicateException, NoSuchAlgorithmException { + public TokenDTO register(@Valid @RequestBody UserSignUpDTO body) throws UserCredentialsDuplicateException { String username = body.getUsername(); String email = body.getEmail(); String password = body.getPassword(); @@ -58,7 +55,7 @@ public TokenDTO login(@Valid @RequestBody UserLoginDTO body) throws UserCredenti @Operation(summary = "Verify email") @PostMapping("/email/verify/{code}") - public OkDTO emailVerify(@RequestAttribute(value = AttributesConstants.USER) User user, @PathVariable String code, HttpServletRequest request) throws CodeIsInvalidException, CodeExpiredException { + public OkDTO emailVerify(@RequestAttribute(value = AttributesConstants.USER) User user, @PathVariable String code) throws CodeIsInvalidException, CodeExpiredException { log.info("EMAIL verification for USER ({}, {}) request", user.getId(), user.getEmail()); authenticationService.verifyEmail(user, code); @@ -68,7 +65,7 @@ public OkDTO emailVerify(@RequestAttribute(value = AttributesConstants.USER) Use @Operation(summary = "Resend email") @PostMapping("/email/resend") - public OkDTO resendEmail(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.ACCESS_TOKEN) String accessToken, HttpServletRequest request) throws CodeIsInvalidException, NeedToWaitBeforeResendException { + public OkDTO resendEmail(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.ACCESS_TOKEN) String accessToken) throws CodeIsInvalidException, NeedToWaitBeforeResendException { log.info("USER email verify resend requested ({}, {}) request", user.getId(), user.getEmail()); authenticationService.resendEmail(user, accessToken); diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java b/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java index 54aa25c..3c3e45f 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java @@ -46,7 +46,7 @@ public ChannelDTO createChannel(@RequestAttribute(value = AttributesConstants.US @Operation(summary = "Get channel") @GetMapping("/{id}") - public ChannelDTO getChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { + public ChannelDTO getChannel(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { log.info("CHANNEL info ({}) request", channel.getId()); return new ChannelDTO(channel); @@ -74,7 +74,7 @@ public OkDTO leaveChannel(@RequestAttribute(value = AttributesConstants.USER) Us @Operation(summary = "Edit channel") @PatchMapping("/{id}") - public ChannelDTO editChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody ChannelEditDTO body) throws MissingPermissionsException { + public ChannelDTO editChannel(@RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody ChannelEditDTO body) throws MissingPermissionsException { log.info("CHANNEL edit ({}) request", channel.getId()); channel = channelsService.editChannel(member, channel, body); @@ -94,7 +94,7 @@ public OkDTO deleteChannel(@RequestAttribute(value = AttributesConstants.USER) U @Operation(summary = "Get members") @GetMapping("/{id}/members") - public List getMembers(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { + public List getMembers(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { log.info("CHANNEL get members ({}) request", channel.getId()); return channelsService.getMembers(channel); @@ -102,7 +102,7 @@ public List getMembers(@RequestAttribute(value = AttributesConstants. @Operation(summary = "Get member") @GetMapping("/{id}/members/{memberId}") - public MemberDTO getMember(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String memberId) throws MemberInChannelNotFoundException { + public MemberDTO getMember(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String memberId) throws MemberInChannelNotFoundException { log.info("CHANNEL get member ({}, {}) request", channel.getId(), memberId); Member member = channelsService.getMember(channel, memberId); diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java index 02bb562..b3e1ca2 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java @@ -2,7 +2,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -37,7 +36,7 @@ public MessagesController(MessagesService messagesService) { @Operation(summary = "Get messages") @GetMapping("/channel/{channelId}") - public MessagesDTO getMessages(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @RequestParam(required = false, defaultValue = "0") long before, @RequestParam(required = false, defaultValue = "0") int limit, HttpServletRequest request) throws MessageNotFoundException { + public MessagesDTO getMessages(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @RequestParam(required = false, defaultValue = "0") long before, @RequestParam(required = false, defaultValue = "0") int limit) { log.info("MESSAGES ({}, {}) from CHANNEL ({}) by USER ({}, {}) requested", before, limit, channel.getId(), user.getId(), user.getEmail()); List messagesArray = messagesService.getMessages(before, limit, channel); @@ -67,7 +66,7 @@ public OkDTO createMessage(@RequestAttribute(value = AttributesConstants.USER) U @Operation(summary = "Delete message") @DeleteMapping("/channel/{channelId}/{id}") - public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String id, HttpServletRequest request) throws MessageNotFoundException, MissingPermissionsException { + public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String id) throws MessageNotFoundException, MissingPermissionsException { log.info("MESSAGE ({}) delete from CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); messagesService.deleteMessage(id, member, channel); @@ -77,7 +76,7 @@ public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) U @Operation(summary = "Edit message") @PatchMapping("/channel/{channelId}/{id}") - public MessagesDTO editMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody MessageCreateDTO body, @PathVariable String id, HttpServletRequest request) throws MessageNotFoundException, MissingPermissionsException { + public MessagesDTO editMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody MessageCreateDTO body, @PathVariable String id) throws MessageNotFoundException, MissingPermissionsException { log.info("MESSAGE ({}) patch in CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); List message = List.of(messagesService.editMessage(id, channel, member, body)); diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java index 476f804..75e4291 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java @@ -2,7 +2,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -17,7 +16,6 @@ import su.foxogram.models.User; import su.foxogram.services.UsersService; -import java.security.NoSuchAlgorithmException; import java.util.Objects; @Slf4j @@ -52,7 +50,7 @@ public UserDTO editUser(@RequestAttribute(value = AttributesConstants.USER) User @Operation(summary = "Delete") @DeleteMapping("/@me") - public OkDTO deleteUser(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.ACCESS_TOKEN) String accessToken, @RequestBody UserDeleteDTO body, HttpServletRequest request) throws UserCredentialsIsInvalidException, CodeIsInvalidException { + public OkDTO deleteUser(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestBody UserDeleteDTO body) throws UserCredentialsIsInvalidException { String password = body.getPassword(); log.info("USER deletion requested ({}, {}) request", user.getId(), user.getEmail()); @@ -63,7 +61,7 @@ public OkDTO deleteUser(@RequestAttribute(value = AttributesConstants.USER) User @Operation(summary = "Confirm delete") @PostMapping("/@me/delete/confirm/") - public OkDTO deleteUserConfirm(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.ACCESS_TOKEN) String accessToken, HttpServletRequest request) throws CodeIsInvalidException, CodeExpiredException { + public OkDTO deleteUserConfirm(@RequestAttribute(value = AttributesConstants.USER) User user) { log.info("USER deletion confirm ({}, {}) request", user.getId(), user.getEmail()); usersService.confirmUserDelete(user); @@ -73,7 +71,7 @@ public OkDTO deleteUserConfirm(@RequestAttribute(value = AttributesConstants.USE @Operation(summary = "Setup MFA") @PostMapping("/@me/mfa") - public MFAKeyDTO setupMFA(@RequestAttribute(value = AttributesConstants.USER) User user) throws NoSuchAlgorithmException, MFAIsAlreadySetException { + public MFAKeyDTO setupMFA(@RequestAttribute(value = AttributesConstants.USER) User user) throws MFAIsAlreadySetException { log.info("USER mfa setup ({}, {}) request", user.getId(), user.getEmail()); String key = usersService.setupMFA(user); @@ -83,7 +81,7 @@ public MFAKeyDTO setupMFA(@RequestAttribute(value = AttributesConstants.USER) Us @Operation(summary = "Delete MFA") @DeleteMapping("/@me/mfa") - public OkDTO deleteMFA(@RequestAttribute(value = AttributesConstants.USER) User user) throws NoSuchAlgorithmException, MFAIsAlreadySetException, MFAIsNotSetException { + public OkDTO deleteMFA(@RequestAttribute(value = AttributesConstants.USER) User user) throws MFAIsNotSetException { log.info("USER mfa delete ({}, {}) request", user.getId(), user.getEmail()); usersService.deleteMFA(user); diff --git a/foxogram-api/src/main/java/su/foxogram/services/UsersService.java b/foxogram-api/src/main/java/su/foxogram/services/UsersService.java index 46536b0..aea0d90 100644 --- a/foxogram-api/src/main/java/su/foxogram/services/UsersService.java +++ b/foxogram-api/src/main/java/su/foxogram/services/UsersService.java @@ -10,7 +10,6 @@ import su.foxogram.dtos.request.UserEditDTO; import su.foxogram.exceptions.*; import su.foxogram.models.User; -import su.foxogram.repositories.CodeRepository; import su.foxogram.repositories.UserRepository; import su.foxogram.util.CodeGenerator; import su.foxogram.util.Encryptor; @@ -26,13 +25,10 @@ public class UsersService { private final EmailService emailService; - private final CodeRepository codeRepository; - @Autowired - public UsersService(UserRepository userRepository, EmailService emailService, CodeRepository codeRepository) { + public UsersService(UserRepository userRepository, EmailService emailService) { this.userRepository = userRepository; this.emailService = emailService; - this.codeRepository = codeRepository; } public User getUser(String key) throws UserNotFoundException { @@ -60,7 +56,7 @@ public User editUser(User user, UserEditDTO body) throws UserCredentialsDuplicat return user; } - public void requestUserDelete(User user, String password) throws UserCredentialsIsInvalidException, CodeIsInvalidException { + public void requestUserDelete(User user, String password) throws UserCredentialsIsInvalidException { if (!Encryptor.verifyPassword(password, user.getPassword())) throw new UserCredentialsIsInvalidException(); diff --git a/foxogram-common/src/main/java/su/foxogram/interceptors/AuthenticationInterceptor.java b/foxogram-common/src/main/java/su/foxogram/interceptors/AuthenticationInterceptor.java index be2be2e..ea840ba 100644 --- a/foxogram-common/src/main/java/su/foxogram/interceptors/AuthenticationInterceptor.java +++ b/foxogram-common/src/main/java/su/foxogram/interceptors/AuthenticationInterceptor.java @@ -37,7 +37,7 @@ public AuthenticationInterceptor(AuthenticationService authenticationService) { } @Override - public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws UserUnauthorizedException, UserEmailNotVerifiedException, MFAIsInvalidException, TOTPKeyIsInvalidException, CodeExpiredException, CodeIsInvalidException { + public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws UserUnauthorizedException, UserEmailNotVerifiedException, MFAIsInvalidException, CodeExpiredException, CodeIsInvalidException { String requestURI = request.getRequestURI(); boolean MFAValidationRequired = MFA_REQUIRED_PATHS.stream().anyMatch(requestURI::contains); boolean ignoreEmailVerification = EMAIL_VERIFICATION_IGNORE_PATHS.stream().anyMatch(requestURI::contains); @@ -59,7 +59,7 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons return true; } - private void validateMFA(User user, HttpServletRequest request) throws MFAIsInvalidException, TOTPKeyIsInvalidException, CodeExpiredException, CodeIsInvalidException { + private void validateMFA(User user, HttpServletRequest request) throws MFAIsInvalidException, CodeExpiredException, CodeIsInvalidException { String code = request.getHeader("Code"); if (!user.hasFlag(UserConstants.Flags.MFA_ENABLED)) { diff --git a/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java b/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java index 9dd78d2..f838cb1 100644 --- a/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java +++ b/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java @@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; -import su.foxogram.configs.APIConfig; import su.foxogram.constants.CodesConstants; import su.foxogram.constants.EmailConstants; import su.foxogram.constants.UserConstants; @@ -18,8 +17,6 @@ import su.foxogram.util.CodeGenerator; import su.foxogram.util.Encryptor; -import java.security.NoSuchAlgorithmException; - @Slf4j @Service public class AuthenticationService { @@ -31,15 +28,12 @@ public class AuthenticationService { private final JwtService jwtService; - private final APIConfig apiConfig; - @Autowired - public AuthenticationService(UserRepository userRepository, CodeRepository codeRepository, EmailService emailService, JwtService jwtService, APIConfig apiConfig) { + public AuthenticationService(UserRepository userRepository, CodeRepository codeRepository, EmailService emailService, JwtService jwtService) { this.userRepository = userRepository; this.codeRepository = codeRepository; this.emailService = emailService; this.jwtService = jwtService; - this.apiConfig = apiConfig; } public User getUser(String header, boolean ignoreEmailVerification) throws UserUnauthorizedException, UserEmailNotVerifiedException { @@ -56,7 +50,7 @@ public User validate(String token, boolean ignoreEmailVerification) throws UserU return userRepository.findById(userId).orElseThrow(UserUnauthorizedException::new); } - public String userSignUp(String username, String email, String password) throws UserCredentialsDuplicateException, NoSuchAlgorithmException { + public String userSignUp(String username, String email, String password) throws UserCredentialsDuplicateException { User user = createUser(username, email, password); try { userRepository.save(user);