This commit is contained in:
2025-12-28 22:07:39 +01:00
parent 5ef53991d4
commit bc1e598987
18 changed files with 67 additions and 507 deletions

View File

@@ -1,80 +0,0 @@
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);
}
}
}

View File

@@ -1,42 +0,0 @@
package be.naaturel.boardmateapi.controllers;
import be.naaturel.boardmateapi.controllers.dtos.AuthRequestDto;
import be.naaturel.boardmateapi.controllers.dtos.AuthResponseDto;
import be.naaturel.boardmateapi.controllers.dtos.ClientDto;
import be.naaturel.boardmateapi.controllers.dtos.ResponseBody;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
private final ClientService service;
@Autowired
public ClientController(ClientService service){
this.service = service;
}
@PostMapping("/client/create")
public ResponseEntity<ResponseBody<String>> create(@RequestBody ClientDto dto) {
ResponseBody<String> result = ResponseBody.createEmpty();
try{
String clientId = service.create(dto.getName(), dto.getUsername(), dto.getKey());
result.setData(clientId);
return ResponseEntity.
status(HttpStatus.OK)
.body(result);
} catch (Exception e){
result.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(result);
}
}
}

View File

@@ -1,22 +0,0 @@
package be.naaturel.boardmateapi.controllers.dtos;
public class AuthRequestDto {
private String username;
private String key;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}

View File

@@ -1,40 +0,0 @@
package be.naaturel.boardmateapi.controllers.dtos;
public class AuthResponseDto {
private String clientId;
private String name;
private String username;
private String authToken;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthToken() {
return authToken;
}
public void setAuthToken(String authToken) {
this.authToken = authToken;
}
}

View File

@@ -1,32 +0,0 @@
package be.naaturel.boardmateapi.controllers.dtos;
public class ClientDto {
private String name;
private String username;
private String key;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}