Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
krabiworld committed Dec 6, 2024
1 parent 1d7a48c commit 3db9e93
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,8 +16,6 @@
import su.foxogram.models.User;
import su.foxogram.services.AuthenticationService;

import java.security.NoSuchAlgorithmException;

@Slf4j
@RestController
@Tag(name = "Authentication")
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -94,15 +94,15 @@ public OkDTO deleteChannel(@RequestAttribute(value = AttributesConstants.USER) U

@Operation(summary = "Get members")
@GetMapping("/{id}/members")
public List<MemberDTO> getMembers(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) {
public List<MemberDTO> getMembers(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) {
log.info("CHANNEL get members ({}) request", channel.getId());

return channelsService.getMembers(channel);
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Message> messagesArray = messagesService.getMessages(before, limit, channel);
Expand Down Expand Up @@ -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);
Expand All @@ -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> message = List.of(messagesService.editMessage(id, channel, member, body));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -17,7 +16,6 @@
import su.foxogram.models.User;
import su.foxogram.services.UsersService;

import java.security.NoSuchAlgorithmException;
import java.util.Objects;

@Slf4j
Expand Down Expand Up @@ -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());

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,8 +17,6 @@
import su.foxogram.util.CodeGenerator;
import su.foxogram.util.Encryptor;

import java.security.NoSuchAlgorithmException;

@Slf4j
@Service
public class AuthenticationService {
Expand All @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 3db9e93

Please sign in to comment.