Skip to content

Commit

Permalink
DXE-4104 AkamaiOPEN-edgegrid-java failing when param is an url - adde…
Browse files Browse the repository at this point in the history
…d unit tests and modified code for easier testing
  • Loading branch information
mgwoj committed Aug 12, 2024
1 parent d1d3218 commit b8ddc52
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ public AbstractEdgeGridRequestSigner(ClientCredential clientCredential) {
*/
public AbstractEdgeGridRequestSigner(ClientCredentialProvider clientCredentialProvider) {
this.clientCredentialProvider = clientCredentialProvider;
this.edgeGridSigner = new EdgeGridV1Signer();
this.edgeGridSigner = createEdgeGridSigner();
}

/**
* Returns new instance of EdgeGridV1Signer.
*
* @return a {@link EdgeGridV1Signer} new instance
*/
protected EdgeGridV1Signer createEdgeGridSigner() {
return new EdgeGridV1Signer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,23 @@ public EdgeGridV1Signer() {
*/
public String getSignature(Request request, ClientCredential credential)
throws RequestSigningException {
return getSignature(request, credential, System.currentTimeMillis(), generateNonce());
return getSignature(request, credential, getTimestamp(), getNonce());
}

/**
* Returns timestamp needed for signing
* @return returns current time stamp
*/
protected long getTimestamp() {
return System.currentTimeMillis();
}

/**
* Returns nonce needed for signing
* @return returns generated nonce
*/
protected String getNonce() {
return generateNonce();
}

private static String generateNonce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ public RequestBuilder uri(URI uri) {
return this;
}

/**
* <p>
* Sets the URI of the HTTP request without any processing it, which is important when URI contains
* path parameters which consists of encoded URLs.
* This URI <i>MUST</i> have the correct path and query segments set. Scheme is assumed to be "HTTPS" for the purpose of this library. Host is
* actually taken from a {@link ClientCredential} at signing time; any value in this URI is
* discarded. Fragments are not removed from signing process.
* </p>
* <p>
* A path and/or query string is required.
* </p>
*
* @param uri a {@link URI}
* @return reference back to this builder instance
*/
public RequestBuilder rawUri(URI uri) {
Objects.requireNonNull(uri, "uri cannot be empty");
this.uri = uri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class RestAssuredEdgeGridFilter implements Filter {

private final RestAssuredEdgeGridRequestSigner binding;
protected RestAssuredEdgeGridRequestSigner binding;

/**
* Creates an EdgeGrid signing interceptor using the same {@link ClientCredential} for each
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import com.akamai.edgegrid.signer.ClientCredential;
import com.akamai.edgegrid.signer.EdgeGridV1Signer;
import com.akamai.edgegrid.signer.exceptions.RequestSigningException;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
Expand Down Expand Up @@ -261,6 +262,61 @@ public void dontSignRequestWithMultipartContent() throws URISyntaxException, IOE
.then().statusCode(200);
}

class MockedEdgeGridV1Signer extends EdgeGridV1Signer {
String fixedNonce = "ec9d20ee-1e9b-4c1f-925a-f0017754f86c";
// Fixed timestamp corresponds to 2016-08-04T07:00:00+0000.
long fixedTimestamp = 1470294000000L;

protected long getTimestamp() {
return fixedTimestamp;
}

protected String getNonce() {
return fixedNonce;
}

}

class MockedRestAssuredEdgeGridRequestSigner extends RestAssuredEdgeGridRequestSigner {

public MockedRestAssuredEdgeGridRequestSigner(ClientCredential clientCredential) {
super(clientCredential);
}

@Override
protected EdgeGridV1Signer createEdgeGridSigner() {
return new MockedEdgeGridV1Signer();
}
}

class MockedRestAssuredEdgeGridFilter extends RestAssuredEdgeGridFilter {

public MockedRestAssuredEdgeGridFilter(ClientCredential credential) {
super(credential);
this.binding = new MockedRestAssuredEdgeGridRequestSigner(credential);
}
}
@Test
public void signRequestWithPathParamContainingURL() throws URISyntaxException, IOException {

wireMockServer.stubFor(get(urlPathMatching("/sso-config/v1/idps/https%3A%2F%2Ffdef2ea8-64b1-4b78-ad36-bacae87af167/certificates"))
.withHeader("Authorization", equalTo("EG1-HMAC-SHA256 client_token=akaa-k7glklzuxkkh2ycw-oadjphopvpn6yjoj;access_token=akaa-dm5g2bfwoodqnc6k-ju7vlao2wz6oz2rp;timestamp=20160804T07:00:00+0000;nonce=ec9d20ee-1e9b-4c1f-925a-f0017754f86c;signature=2KunLDWST5ZgrbL8CuTF2Gxp7UfsIy/DxELcajvziTo="))
.withHeader("Host", equalTo(SERVICE_MOCK))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("<response>Some content</response>")));

RestAssuredEdgeGridFilter filter = new MockedRestAssuredEdgeGridFilter(credential);
RestAssured.given()
.relaxedHTTPSValidation()
.filter(filter)
.get("/sso-config/v1/idps/{id}/certificates", "https://fdef2ea8-64b1-4b78-ad36-bacae87af167")
.then().statusCode(200);

assertThat(wireMockServer.findAllUnmatchedRequests().size(), CoreMatchers.equalTo(0));
}

@AfterClass
public void tearDownAll() {
wireMockServer.stop();
Expand Down
Loading

0 comments on commit b8ddc52

Please sign in to comment.