use of com.checkmarx.sdk.dto.cx.CxAuthResponse in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.
the class CxAuthService method getAuthToken.
/**
* Get Auth Token
*/
@Override
public String getAuthToken(String username, String password, String clientId, String clientSecret, String scope) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", username);
map.add("password", password);
map.add("grant_type", "password");
map.add("scope", cxProperties.getScope());
map.add("client_id", clientId);
if (!ScanUtils.empty(cxProperties.getClientSecret())) {
map.add("client_secret", clientSecret);
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
try {
// get the access token
log.info("Logging into Checkmarx {}", cxProperties.getUrl().concat(LOGIN));
CxAuthResponse response = restTemplate.postForObject(cxProperties.getUrl().concat(LOGIN), requestEntity, CxAuthResponse.class);
if (response == null) {
throw new InvalidCredentialsException();
}
token = response.getAccessToken();
// expire 500 seconds early
tokenExpires = LocalDateTime.now().plusSeconds(response.getExpiresIn() - 500);
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
shard.setAccessToken(token);
shard.setTokenExpires(tokenExpires);
}
} catch (NullPointerException | HttpStatusCodeException e) {
log.error("Error occurred white obtaining Access Token. Possibly incorrect credentials");
log.error(ExceptionUtils.getStackTrace(e));
throw new InvalidCredentialsException();
}
return token;
}
use of com.checkmarx.sdk.dto.cx.CxAuthResponse in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.
the class CxAuthService method getSoapAuthToken.
/**
* Get Auth Token specific to SOAP API Calls
*/
@Override
public String getSoapAuthToken(String username, String password) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
username = shard.getUsername();
password = shard.getPassword();
}
map.add("username", username);
map.add("password", password);
map.add("grant_type", "password");
map.add("scope", cxProperties.getSoapScope());
map.add("client_id", cxProperties.getSoapClientId());
if (!ScanUtils.empty(cxProperties.getSoapClientSecret())) {
map.add("client_secret", cxProperties.getSoapClientSecret());
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
try {
// get the access token
log.info("Logging into Checkmarx for SOAP token {}", cxProperties.getUrl().concat(LOGIN));
CxAuthResponse response = restTemplate.postForObject(cxProperties.getUrl().concat(LOGIN), requestEntity, CxAuthResponse.class);
if (response == null) {
throw new InvalidCredentialsException();
}
soapToken = response.getAccessToken();
// expire 500 seconds early
soapTokenExpires = LocalDateTime.now().plusSeconds(response.getExpiresIn() - 500);
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
shard.setSoapToken(soapToken);
shard.setSoapTokenExpires(soapTokenExpires);
}
} catch (NullPointerException | HttpStatusCodeException e) {
log.error("Error occurred white obtaining Access Token. Possibly incorrect credentials");
log.error(ExceptionUtils.getStackTrace(e));
throw new InvalidCredentialsException();
}
return soapToken;
}
Aggregations