-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KNOX-3037 - Client credentials flow accepts essential parameters in t…
…he request body only
- Loading branch information
Showing
6 changed files
with
227 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
gateway-util-common/src/main/java/org/apache/knox/gateway/util/RequestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.apache.knox.gateway.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import javax.servlet.ServletRequest; | ||
|
||
public class RequestUtils { | ||
|
||
public static String getRequestBodyParameter(ServletRequest request, String parameter) throws IOException { | ||
return getRequestBodyParameter(request, parameter, false); | ||
} | ||
|
||
public static String getRequestBodyParameter(ServletRequest request, String parameter, boolean decode) throws IOException { | ||
return getRequestBodyParameter(request.getInputStream(), parameter, decode); | ||
} | ||
|
||
public static String getRequestBodyParameter(InputStream inputStream, String parameter) throws IOException { | ||
return getRequestBodyParameter(inputStream, parameter, false); | ||
} | ||
|
||
public static String getRequestBodyParameter(InputStream inputStream, String parameter, boolean decode) throws IOException { | ||
return getRequestBodyParameter(new InputStreamReader(inputStream, StandardCharsets.UTF_8), parameter, decode); | ||
} | ||
|
||
public static String getRequestBodyParameter(Reader reader, String parameter) throws IOException { | ||
return getRequestBodyParameter(reader, parameter, false); | ||
} | ||
|
||
public static String getRequestBodyParameter(Reader reader, String parameter, boolean decode) throws IOException { | ||
final BufferedReader bufferedReader = new BufferedReader(reader); | ||
final StringBuilder requestBodyBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null) { | ||
requestBodyBuilder.append(line); | ||
} | ||
|
||
final String requestBodyString = decode ? URLDecoder.decode(requestBodyBuilder.toString(), StandardCharsets.UTF_8.name()) : requestBodyBuilder.toString(); | ||
return getRequestBodyParameter(requestBodyString, parameter); | ||
} | ||
|
||
public static String getRequestBodyParameter(String requestBodyString, String parameter) { | ||
if (requestBodyString != null) { | ||
final String[] requestBodyParams = requestBodyString.split("&"); | ||
for (String requestBodyParam : requestBodyParams) { | ||
String[] keyValue = requestBodyParam.split("=", 2); | ||
if (parameter.equals(keyValue[0])) { | ||
return keyValue[1]; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
gateway-util-common/src/test/java/org/apache/knox/gateway/util/RequestUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.apache.knox.gateway.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.ServletRequest; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.knox.test.mock.MockServletInputStream; | ||
import org.easymock.EasyMock; | ||
import org.junit.Test; | ||
|
||
public class RequestUtilsTest { | ||
|
||
private static final String REQUEST_BODY_PARAM_NAME = "myParam"; | ||
private static final String REQUEST_BODY_PARAM_VALUE_RAW = "This-is_my sample text!"; | ||
private static final String REQUEST_BODY_PARAM_VALUE_ENCODED = "This-is_my%20sample%20text%21"; | ||
|
||
@Test | ||
public void testGetRequestBodyParameterEncoded() throws Exception { | ||
testGetRequestBodyParameter(true); | ||
} | ||
|
||
@Test | ||
public void testGetRequestBodyParameterRaw() throws Exception { | ||
testGetRequestBodyParameter(false); | ||
} | ||
|
||
private void testGetRequestBodyParameter(boolean decode) throws Exception { | ||
final ServletRequest request = EasyMock.createNiceMock(ServletRequest.class); | ||
EasyMock.expect(request.getInputStream()).andReturn(produceServletInputStream(decode)).anyTimes(); | ||
|
||
EasyMock.replay(request); | ||
|
||
final String requestBodyParam = RequestUtils.getRequestBodyParameter(request, REQUEST_BODY_PARAM_NAME, decode); | ||
assertEquals(REQUEST_BODY_PARAM_VALUE_RAW, requestBodyParam); | ||
} | ||
|
||
private ServletInputStream produceServletInputStream(boolean encode) { | ||
final String requestBody = REQUEST_BODY_PARAM_NAME + "=" + (encode ? REQUEST_BODY_PARAM_VALUE_ENCODED : REQUEST_BODY_PARAM_VALUE_RAW); | ||
final InputStream inputStream = IOUtils.toInputStream(requestBody, StandardCharsets.UTF_8); | ||
return new MockServletInputStream(inputStream); | ||
} | ||
|
||
} |