Skip to content

Commit

Permalink
Merge pull request #2 from CxRepositories/V2.4
Browse files Browse the repository at this point in the history
V2.4
  • Loading branch information
cx-muhammed authored Sep 21, 2020
2 parents bf23e6c + 0d8aad4 commit b49231e
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 63 deletions.
6 changes: 3 additions & 3 deletions CX-SDK-API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>

<properties>
Expand All @@ -19,12 +19,12 @@
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Application</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Infrastructure</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
Expand Down
4 changes: 2 additions & 2 deletions CX-SDK-Application-Contracts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>

<properties>
Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Domain</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
</dependencies>

Expand Down
6 changes: 3 additions & 3 deletions CX-SDK-Application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>

<properties>
Expand All @@ -19,12 +19,12 @@
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Domain</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Application-Contracts</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
2 changes: 1 addition & 1 deletion CX-SDK-Domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>

<properties>
Expand Down
10 changes: 5 additions & 5 deletions CX-SDK-Infrastructure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>1.19.3</jersey.version>
<cxf.version>3.3.6</cxf.version>
<cxf.version>3.3.7</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Domain</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-Application-Contracts</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.cx.sdk</groupId>
<artifactId>CX-SDK-OIDC-login</artifactId>
<version>2.2</version>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
Expand Down
11 changes: 3 additions & 8 deletions CX-SDK-OIDC-login/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>checkmarx-sdk</artifactId>
<groupId>com.cx.sdk</groupId>
<version>2.2</version>
<version>2.7</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>CX-SDK-OIDC-login</artifactId>
Expand Down Expand Up @@ -55,12 +55,7 @@
<version>${teamdev.version}</version>
<scope>provided</scope>
</dependency>
<!--<dependency>
<groupId>teamdev.license</groupId>
<artifactId>runtime</artifactId>
<version>${teamdev.license.version}</version>
<scope>provided</scope>
</dependency>-->

<dependency>
<groupId>com.teamdev.jxbrowser</groupId>
<artifactId>jxbrowser-cross-platform</artifactId>
Expand All @@ -86,7 +81,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@


import com.cx.sdk.oidcLogin.exceptions.CxRestLoginException;
import com.cx.sdk.oidcLogin.exceptions.CxValidateResponseException;
import com.cx.sdk.oidcLogin.restClient.ICxServer;
import com.cx.sdk.oidcLogin.webBrowsing.AuthenticationData;
import com.cx.sdk.oidcLogin.webBrowsing.IOIDCWebBrowser;
import com.cx.sdk.oidcLogin.webBrowsing.LoginData;

import java.io.IOException;

public class CxOIDCConnector {
private ICxServer cxServer;
private String clientName;
Expand All @@ -19,8 +22,15 @@ public CxOIDCConnector(ICxServer cxServer, IOIDCWebBrowser webBrowser, String cl
}

public LoginData connect() throws Exception {
if (cxServer.getCxVersion().equals("Pre 9.0")) {
throw new CxRestLoginException("The Cx version is either older than 9.0 or the server can't be reached");
String version ="";
try {
version= (String) cxServer.getCxVersion(clientName);
} catch (IOException|CxValidateResponseException e) {
throw new CxRestLoginException(e.getMessage());
}

if ("Pre 9.0".equals(version) ) {
throw new CxRestLoginException("sast version is older than 9.x");
}

AuthenticationData authenticationData = webBrowser.browseAuthenticationData(cxServer.getServerURL(), clientName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AccessTokenDTO {
private String accessToken;

@JsonProperty("expires_in")
private int expiresIn;
private long expiresIn;

@JsonProperty("token_type")
private String tokenType;
Expand All @@ -35,11 +35,11 @@ public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public int getExpiresIn() {
public long getExpiresIn() {
return expiresIn;
}

public void setExpiresIn(int expiresIn) {
public void setExpiresIn(long expiresIn) {
this.expiresIn = expiresIn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,25 @@ public String getServerURL() {
return serverURL;
}

public String getCxVersion() {
public String getCxVersion() throws IOException, CxValidateResponseException {
return getCxVersion("");
}

public String getCxVersion(String clientName) throws CxValidateResponseException, IOException {
HttpResponse response;
HttpUriRequest request;
String version;
try {
request = RequestBuilder
.get()
.setUri(versionURL)
.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString())
.build();
response = client.execute(request);
validateResponse(response, 200, GET_VERSION_ERROR);
version = new BasicResponseHandler().handleResponse(response);
} catch (IOException | CxValidateResponseException e) {
version = "Pre 9.0";
}

request = RequestBuilder
.get()
.setUri(versionURL)
.setHeader("cxOrigin", clientName)
.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString())
.build();
response = client.execute(request);
validateResponse(response, 200, GET_VERSION_ERROR);
version = new BasicResponseHandler().handleResponse(response);


return version;
}
Expand All @@ -121,6 +124,7 @@ public LoginData login(String code) throws CxRestLoginException, CxValidateRespo
Long accessTokenExpirationInMilli = getAccessTokenExpirationInMilli(jsonResponse.getExpiresIn());
return new LoginData(jsonResponse.getAccessToken(), jsonResponse.getRefreshToken(), accessTokenExpirationInMilli, jsonResponse.getIdToken());
} catch (IOException e) {
logger.trace("Failed to login", e);
throw new CxRestLoginException("Failed to login: " + e.getMessage());
} finally {
HttpClientUtils.closeQuietly(loginResponse);
Expand All @@ -146,6 +150,7 @@ public LoginData getAccessTokenFromRefreshToken(String refreshToken) throws CxRe
Long accessTokenExpirationInMilli = getAccessTokenExpirationInMilli(jsonResponse.getExpiresIn());
return new LoginData(jsonResponse.getAccessToken(), jsonResponse.getRefreshToken(), accessTokenExpirationInMilli, jsonResponse.getIdToken());
} catch (IOException e) {
logger.trace("Failed to get new access token from refresh token: ", e);
throw new CxRestLoginException("Failed to get new access token from refresh token: " + e.getMessage());
} finally {
HttpClientUtils.closeQuietly(loginResponse);
Expand Down Expand Up @@ -185,7 +190,7 @@ private Permissions getPermissions(UserInfoDTO jsonResponse) {
sastPermissions.contains(Consts.MANAGE_RESULTS_EXPLOITABILITY));
}

private Long getAccessTokenExpirationInMilli(int accessTokenExpirationInSec) {
private Long getAccessTokenExpirationInMilli(long accessTokenExpirationInSec) {
long currentTime = System.currentTimeMillis();
long accessTokenExpInMilli = accessTokenExpirationInSec * 1000;
return currentTime + accessTokenExpInMilli;
Expand All @@ -205,6 +210,7 @@ private static void validateResponse(HttpResponse response, int status, String m
}
}
} catch (IOException e) {
e.printStackTrace();
throw new CxValidateResponseException("Error parse REST response body: " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.cx.sdk.oidcLogin.restClient.entities.Permissions;
import com.cx.sdk.oidcLogin.webBrowsing.LoginData;

import java.io.IOException;

public interface ICxServer {

String getServerURL();
Expand All @@ -17,5 +19,7 @@ public interface ICxServer {

Permissions getPermissionsFromUserInfo(String accessToken) throws CxValidateResponseException;

String getCxVersion();
String getCxVersion() throws IOException, CxValidateResponseException;

Object getCxVersion(String clientName) throws CxValidateResponseException, IOException;
}
Loading

0 comments on commit b49231e

Please sign in to comment.