Search in sources :

Example 1 with CxAuthResponse

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CxAuthResponse(com.checkmarx.sdk.dto.cx.CxAuthResponse) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) InvalidCredentialsException(com.checkmarx.sdk.exception.InvalidCredentialsException) ShardSession(com.checkmarx.sdk.ShardManager.ShardSession) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 2 with CxAuthResponse

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CxAuthResponse(com.checkmarx.sdk.dto.cx.CxAuthResponse) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ShardSession(com.checkmarx.sdk.ShardManager.ShardSession) InvalidCredentialsException(com.checkmarx.sdk.exception.InvalidCredentialsException) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Aggregations

ShardSession (com.checkmarx.sdk.ShardManager.ShardSession)2 CxAuthResponse (com.checkmarx.sdk.dto.cx.CxAuthResponse)2 InvalidCredentialsException (com.checkmarx.sdk.exception.InvalidCredentialsException)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)2