|
|
@@ -11,8 +11,6 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -25,55 +23,6 @@ public class AuthClient {
|
|
|
@Value("${auth.api}")
|
|
|
private String BASE_URL;
|
|
|
|
|
|
- public AuthenticatedUser register(String username, String password) {
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
-
|
|
|
- Map<String, String> requestBody = new HashMap<>();
|
|
|
- requestBody.put("username", username);
|
|
|
- requestBody.put("password", password);
|
|
|
-
|
|
|
- HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody, headers);
|
|
|
-
|
|
|
- try {
|
|
|
- ResponseEntity<AuthenticatedUser> response = rest.exchange(BASE_URL + "/register", HttpMethod.POST, entity, AuthenticatedUser.class);
|
|
|
-
|
|
|
- if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
- return response.getBody();
|
|
|
- } else {
|
|
|
- throw new UnauthorizedException("Unexpected response status: " + response.getStatusCode());
|
|
|
- }
|
|
|
- } catch (HttpClientErrorException e) {
|
|
|
- throw new UnauthorizedException("Error: " + e.getStatusCode() + " - " + e.getResponseBodyAsString());
|
|
|
- } catch (Exception e) {
|
|
|
- throw new UnauthorizedException("An error occurred: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public AuthenticatedUser authenticate(String username, String password) {
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
-
|
|
|
- Map<String, String> requestBody = new HashMap<>();
|
|
|
- requestBody.put("username", username);
|
|
|
- requestBody.put("password", password);
|
|
|
-
|
|
|
- HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody, headers);
|
|
|
-
|
|
|
- try {
|
|
|
- ResponseEntity<AuthenticatedUser> response = rest.exchange(BASE_URL + "/authenticate", HttpMethod.POST, entity, AuthenticatedUser.class);
|
|
|
- if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
- return response.getBody();
|
|
|
- } else {
|
|
|
- throw new UnauthorizedException("Unexpected response status: " + response.getStatusCode());
|
|
|
- }
|
|
|
- } catch (HttpClientErrorException e) {
|
|
|
- throw new UnauthorizedException("Error: " + e.getStatusCode() + " - " + e.getResponseBodyAsString());
|
|
|
- } catch (Exception e) {
|
|
|
- throw new UnauthorizedException("An error occurred: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public CurrentUser current(String token) {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.add("Authorization", token);
|
|
|
@@ -95,16 +44,6 @@ public class AuthClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Data
|
|
|
- @AllArgsConstructor
|
|
|
- @NoArgsConstructor
|
|
|
- public static class AuthenticatedUser {
|
|
|
- private String id;
|
|
|
- private String username;
|
|
|
- private String token;
|
|
|
- private Instant expirationDate;
|
|
|
- }
|
|
|
-
|
|
|
@Data
|
|
|
@AllArgsConstructor
|
|
|
@NoArgsConstructor
|