AuthController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package fr.triplea.demovote.web.controller;
  2. import java.util.List;
  3. import java.util.Locale;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.MessageSource;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.security.core.userdetails.UserDetails;
  13. import org.springframework.security.crypto.password.PasswordEncoder;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.servlet.LocaleResolver;
  20. import fr.triplea.demovote.dao.ParticipantRepository;
  21. import fr.triplea.demovote.dto.JourneesTransfer;
  22. import fr.triplea.demovote.dto.RefreshTokenTransfer;
  23. import fr.triplea.demovote.dto.UserCredentials;
  24. import fr.triplea.demovote.model.Participant;
  25. import fr.triplea.demovote.model.RefreshToken;
  26. import fr.triplea.demovote.model.Role;
  27. import fr.triplea.demovote.security.MyUserDetailsService;
  28. import fr.triplea.demovote.security.jwt.JwtTokenUtil;
  29. import fr.triplea.demovote.security.jwt.RefreshTokenException;
  30. import fr.triplea.demovote.security.jwt.RefreshTokenService;
  31. import jakarta.servlet.http.HttpServletRequest;
  32. import jakarta.validation.Valid;
  33. @RestController
  34. @RequestMapping("/sign")
  35. public class AuthController
  36. {
  37. @SuppressWarnings("unused")
  38. private static final Logger LOG = LoggerFactory.getLogger(AuthController.class);
  39. @Autowired
  40. public PasswordEncoder passwordEncoder;
  41. @Autowired
  42. public MyUserDetailsService myUserDetailsService;
  43. @Autowired
  44. private JwtTokenUtil jwtTokenUtil;
  45. @Autowired
  46. RefreshTokenService refreshTokenService;
  47. @Autowired
  48. private ParticipantRepository participantRepository;
  49. @Autowired
  50. private LocaleResolver localeResolver;
  51. @Autowired
  52. private MessageSource messageSource;
  53. @GetMapping(value = "/hello")
  54. public ResponseEntity<JourneesTransfer> getDaysLabels()
  55. {
  56. JourneesTransfer jt = new JourneesTransfer();
  57. jt.setJour1Court("Hello");
  58. return ResponseEntity.ok(jt);
  59. }
  60. @PostMapping(value = "/in")
  61. public ResponseEntity<UserCredentials> signIn(@RequestBody UserCredentials uc, HttpServletRequest request)
  62. {
  63. Locale locale = localeResolver.resolveLocale(request);
  64. String usrn = uc.getUsername(); if (usrn == null) { usrn = ""; } else { usrn = usrn.trim(); }
  65. String pass = uc.getPassword(); if (pass == null) { pass = ""; } else { pass = pass.trim(); }
  66. if (usrn.isEmpty() || pass.isEmpty()) { return ResponseEntity.notFound().build(); }
  67. Participant found = participantRepository.findByPseudonyme(usrn);
  68. if (found != null)
  69. {
  70. UserDetails userDetails = myUserDetailsService.loadUserByUsername(usrn);
  71. Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) ;
  72. if (passwordEncoder.matches(pass, userDetails.getPassword()))
  73. {
  74. SecurityContextHolder.getContext().setAuthentication(authentication);
  75. String token = jwtTokenUtil.generateJwtToken(authentication);
  76. refreshTokenService.deleteByNumeroParticipant(found.getNumeroParticipant());
  77. RefreshToken refreshToken = refreshTokenService.createRefreshToken(found.getNumeroParticipant());
  78. uc = new UserCredentials();
  79. uc.setUsername(usrn);
  80. uc.setPassword("<success@auth>");
  81. uc.setNom(found.getNom());
  82. uc.setPrenom(found.getPrenom());
  83. uc.setDelaiAvantDeconnexion(found.getDelaiDeconnexion());
  84. uc.setAccessToken(token);
  85. uc.setRefreshToken(refreshToken.getToken());
  86. uc.setErreur("");
  87. List<Role> roles = found.getRoles();
  88. if (!(uc.hasRole())) { for (Role role : roles) { if (role.isRole("ADMIN")) { uc.setRole("ADMIN"); } } }
  89. if (!(uc.hasRole())) { for (Role role : roles) { if (role.isRole("ORGA")) { uc.setRole("ORGA"); } } }
  90. if (!(uc.hasRole())) { uc.setRole("USER"); }
  91. return ResponseEntity.ok(uc);
  92. }
  93. else
  94. {
  95. uc = new UserCredentials();
  96. uc.setUsername("");
  97. uc.setPassword("");
  98. uc.setNom("");
  99. uc.setPrenom("");
  100. uc.setDelaiAvantDeconnexion(15);
  101. uc.setAccessToken("");
  102. uc.setRefreshToken("");
  103. uc.setRole("");
  104. uc.setErreur(messageSource.getMessage("auth.password.mismatches", null, locale));
  105. return ResponseEntity.ok(uc);
  106. }
  107. }
  108. uc = new UserCredentials();
  109. uc.setUsername("");
  110. uc.setPassword("");
  111. uc.setNom("");
  112. uc.setPrenom("");
  113. uc.setDelaiAvantDeconnexion(15);
  114. uc.setAccessToken("");
  115. uc.setRefreshToken("");
  116. uc.setRole("");
  117. uc.setErreur(messageSource.getMessage("auth.user.notfound", null, locale));
  118. return ResponseEntity.ok(uc);
  119. }
  120. @PostMapping("/refresh")
  121. public ResponseEntity<?> refreshtoken(@Valid @RequestBody RefreshTokenTransfer rtt, HttpServletRequest request)
  122. {
  123. Locale locale = localeResolver.resolveLocale(request);
  124. String refreshTokenActif = rtt.getRefreshToken();
  125. RefreshToken found = refreshTokenService.findByToken(refreshTokenActif);
  126. if (found == null) { throw new RefreshTokenException(refreshTokenActif, messageSource.getMessage("refreshtoken.notfound", null, locale)); }
  127. found = refreshTokenService.verifyExpiration(found);
  128. if (found == null) { throw new RefreshTokenException(refreshTokenActif, messageSource.getMessage("refreshtoken.expired", null, locale)); }
  129. Participant participant = found.getParticipant();
  130. rtt.setAccessToken(jwtTokenUtil.generateTokenFromPseudonyme(participant.getPseudonyme()));
  131. return ResponseEntity.ok(rtt);
  132. }
  133. @PostMapping("/out")
  134. public ResponseEntity<UserCredentials> signOut(final Authentication authentication)
  135. {
  136. if (authentication != null)
  137. {
  138. Participant found = participantRepository.findByPseudonyme(authentication.getName());
  139. if (found != null) { refreshTokenService.deleteByNumeroParticipant(found.getNumeroParticipant()); }
  140. }
  141. SecurityContextHolder.clearContext();
  142. UserCredentials uc = new UserCredentials();
  143. uc.setUsername("");
  144. uc.setPassword("");
  145. uc.setNom("");
  146. uc.setPrenom("");
  147. uc.setDelaiAvantDeconnexion(15);
  148. uc.setAccessToken("");
  149. uc.setRefreshToken("");
  150. uc.setRole("");
  151. return ResponseEntity.ok(uc);
  152. }
  153. }