Skip to content

Commit

Permalink
Fix : Ajout controle sur les droits à l'authentification et envoie d'…
Browse files Browse the repository at this point in the history
…une exception si accès interdit
  • Loading branch information
pierre-maraval committed Jun 12, 2024
1 parent d1753a9 commit 80229bb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Authentication authenticate(Authentication authentication)

u.setMail(this.getEmail(Integer.parseInt(u.getUserNum())));
List<GrantedAuthority> authorities;
if (u.getRole() != null) {
if (u.getRole() != null && (u.getRole().equals("USER") || u.getRole().equals("ADMIN"))) {
authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(u.getRole()));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.abes.item.web;

import fr.abes.item.core.exception.ForbiddenException;
import fr.abes.item.security.JwtAuthenticationResponse;
import fr.abes.item.security.JwtTokenProvider;
import fr.abes.item.security.LoginRequest;
Expand Down Expand Up @@ -32,10 +33,13 @@ public AuthenticationController(AuthenticationManager authenticationManager, Jwt
@Operation(summary = "permet de s'authentifier et de récupérer un token.",
description = "le token doit être utilisé pour accéder aux ressources protegées.")
@PostMapping("/signin")
public ResponseEntity<JwtAuthenticationResponse> authenticateUser(@RequestBody LoginRequest loginRequest) {
public ResponseEntity<JwtAuthenticationResponse> authenticateUser(@RequestBody LoginRequest loginRequest) throws ForbiddenException {
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
User user = (User)authentication.getPrincipal();
if (user.getAuthorities().isEmpty()) {
throw new ForbiddenException("Ce login ne dispose pas des droits nécessaires pour accéder à Item");
}
String jwt = tokenProvider.generateToken(user);

return ResponseEntity.ok(new JwtAuthenticationResponse(jwt, user.getUserNum(), user.getShortName(), user.getIln(), user.getRole(), user.getMail()));
Expand Down

0 comments on commit 80229bb

Please sign in to comment.