Revert "????????"

This reverts commit bc1e598987.
This commit is contained in:
2025-12-28 22:10:13 +01:00
parent bc1e598987
commit f7630b6754
18 changed files with 507 additions and 67 deletions

View File

@@ -0,0 +1,80 @@
package be.naaturel.boardmateapi.controllers;
import be.naaturel.boardmateapi.common.models.Client;
import be.naaturel.boardmateapi.controllers.dtos.*;
import be.naaturel.boardmateapi.services.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jwt.JwsHeader;
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
@RestController
public class AuthController {
private final ClientService service;
private final JwtEncoder jwtEncoder;
@Autowired
public AuthController(ClientService service, JwtEncoder jwtEncoder) {
this.service = service;
this.jwtEncoder = jwtEncoder;
}
@PostMapping("/authenticate")
public ResponseEntity<ResponseBody<AuthResponseDto>> login(@RequestBody AuthRequestDto request) {
ResponseBody<AuthResponseDto> result = ResponseBody.createEmpty();
try {
Client user = service.authenticate(
request.getUsername(),
request.getKey()
);
Instant now = Instant.now();
JwtClaimsSet claims = JwtClaimsSet.builder()
.subject(user.getId())
.claim("name", user.getName())
.claim("username", user.getUsername())
.issuedAt(now)
.expiresAt(now.plusSeconds(3600*12))
.build();
JwtEncoderParameters params =
JwtEncoderParameters.from(
JwsHeader.with(MacAlgorithm.HS256).build(),
claims
);
String token = jwtEncoder.encode(params).getTokenValue();
AuthResponseDto response = new AuthResponseDto();
response.setName(user.getName());
response.setUsername(user.getUsername());
response.setClientId(user.getId());
response.setAuthToken(token);
result.setSuccess(true);
result.setData(response);
return ResponseEntity
.status(HttpStatus.OK)
.body(result);
} catch (Exception e){
e.printStackTrace();
result.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(result);
}
}
}