Search in sources :

Example 1 with AuthToken

use of com.hp.octane.integrations.services.vulnerabilities.ssc.dto.AuthToken in project octane-ci-java-sdk by MicroFocus.

the class SSCServerSimulator method setDefaultAuth.

public void setDefaultAuth() {
    setAuthHanler(request -> {
        try {
            AuthToken.AuthTokenData authTokenData = new AuthToken.AuthTokenData();
            authTokenData.token = "DUMMY TOKEN";
            authTokenData.terminalDate = "TERMINAL_DATE";
            AuthToken authToken = new AuthToken();
            authToken.setData(authTokenData);
            request.getResponse().setStatus(HttpStatus.SC_OK);
            request.getResponse().getWriter().write(SSCTestUtils.getJson(authToken));
            request.getResponse().getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}
Also used : AuthToken(com.hp.octane.integrations.services.vulnerabilities.ssc.dto.AuthToken) IOException(java.io.IOException)

Example 2 with AuthToken

use of com.hp.octane.integrations.services.vulnerabilities.ssc.dto.AuthToken in project octane-ci-java-sdk by MicroFocus.

the class SSCRestClientImpl method sendReqAuth.

private AuthToken.AuthTokenData sendReqAuth(SSCProjectConfiguration sscProjectConfiguration) {
    // "/{SSC Server Context}/api/v1"
    // String url = "http://" + serverURL + "/ssc/api/v1/projects?q=id:2743&fulltextsearch=true";
    String url = sscProjectConfiguration.getSSCUrl() + "/api/v1/tokens";
    HttpPost request = new HttpPost(url);
    request.addHeader("Authorization", sscProjectConfiguration.getSSCBaseAuthToken());
    request.addHeader("Accept", "application/json");
    request.addHeader("Host", getNetHost(sscProjectConfiguration.getSSCUrl()));
    request.addHeader("Content-Type", "application/json;charset=UTF-8");
    String body = "{\"type\": \"UnifiedLoginToken\"}";
    CloseableHttpResponse response = null;
    try {
        HttpEntity entity = new ByteArrayEntity(body.getBytes(StandardCharsets.UTF_8));
        request.setEntity(entity);
        response = httpClient.execute(request);
        if (succeeded(response.getStatusLine().getStatusCode())) {
            String toString = CIPluginSDKUtils.inputStreamToUTF8String(response.getEntity().getContent());
            AuthToken authToken = new ObjectMapper().readValue(toString, TypeFactory.defaultInstance().constructType(AuthToken.class));
            return authToken.getData();
        } else {
            throw new PermanentException("Couldn't Authenticate SSC user, need to check SSC configuration in Octane plugin");
        }
    } catch (Throwable t) {
        throw new PermanentException(t);
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AuthToken(com.hp.octane.integrations.services.vulnerabilities.ssc.dto.AuthToken) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AuthToken (com.hp.octane.integrations.services.vulnerabilities.ssc.dto.AuthToken)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PermanentException (com.hp.octane.integrations.exceptions.PermanentException)1 IOException (java.io.IOException)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1