Search code examples
javaauthorizationsubstringaccess-tokenrest-assured

How to extract an auth code in Location response header String using Rest Assured


Currently I have a method that does some authorization, which is used for my access token POST request in the body to establish the proper access token. In my authorization method, I am able to extract the Location header value in the response. The only issue is that I only need to extract the values between code= and & as seen in the below string (locationHeaderForAuthCode):

https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64-9855-074a57cc1570

Since that code is used for the request body in my access token POST request, I need to extract only that portion. How do I achieve this in Rest Assured, and if not, then just using plain Java?

Any help would be appreciated. Below is my method for authorization that sets the Location header, and the other method for the access token post request:

private static void getAuthorizeCode() throws JsonException {

    response = given()
                .config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("response_type", responseType)
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("decision", decision)
            .formParam("csrf", tokenId)
            .post(urlProps.getProperty("devVGWAuthorizeUrl"))
            .then()
            .assertThat()
            .statusCode(302)
            .extract().response();

    locationHeaderForAuthCode = response.getHeader("Location");

}

private static void getAccToken() throws JsonException {
    response = given()
            .log().all().config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("grant_type", "authorization_code")
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("code", locationHeaderForAuthCode)
            .post(urlProps.getProperty("devVGWAccessTokenUrl"))
            .then()
//                .assertThat()
//                .statusCode(200)
            .log().all()
            .extract().response();

    accToken = response.jsonPath().getString("access_token");
    System.out.println("Access token is " + accToken);
}

Solution

  • You just need simple split() method of String to extract value from this text.

    String url = "https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64-9855-074a57cc1570";
    String[] texts = url.split("code=");
    String code = texts[1].split("&")[0];
    System.out.println(code); //**12WVUcPyFbmTZUOcgaluGl98r08**