2020
2121public class SimvaApiClient {
2222 private static final Logger logger = Logger .getLogger (SimvaApiClient .class );
23+ private static KeycloakOAuth2Client keycloakClient = new KeycloakOAuth2Client ();
2324
2425 private static SimvaApiConfig apiConfig = new SimvaApiConfig ();
2526 private String bearerToken ;
@@ -38,20 +39,15 @@ public boolean isAuthentificated() {
3839 }
3940
4041 public void authenticate () throws IOException {
41- // Create an ObjectMapper instance
42- ObjectMapper objectMapper = new ObjectMapper ();
43- // Create a JSON object using ObjectNode
44- ObjectNode payload = objectMapper .createObjectNode ();
45- payload .put ("username" , apiConfig .getAdminUsername ());
46- payload .put ("password" , apiConfig .getAdminPassword ());
47- // Convert the JSON object to a string
48- String jsonPayload = objectMapper .writeValueAsString (payload );
49- // Define the API URL
50- String apiUrl = "/users/login" ;
51- // Read the body of the response into a hashmap
52- Map <String ,Object > responseMap = this .sendPostRequest (apiUrl , jsonPayload );
53- // Read the value of the "access_token" key from the hashmap
54- this .bearerToken = "Bearer " + (String )responseMap .get ("token" );
42+ // Validate admin credentials to get a token
43+ boolean isValid = keycloakClient .validateUserCredentials (apiConfig .getAdminUsername (), apiConfig .getAdminPassword ());
44+ if (!isValid ) {
45+ throw new IOException ("Invalid admin credentials for Simva API" );
46+ } else {
47+ logger .info ("Admin credentials validated" );
48+ }
49+ // Store the bearer token for future requests
50+ this .bearerToken = "Bearer " + keycloakClient .getAccessToken ();
5551 logger .info ("Token: " + this .bearerToken );
5652 }
5753
0 commit comments