use of org.apache.jmeter.protocol.http.control.AuthManager.Mechanism in project jmeter by apache.
the class ProxyControl method createAuthorization.
/**
* Detect Header manager in subConfigs,
* Find(if any) Authorization header
* Construct Authentication object
* Removes Authorization if present
*
* @param testElements {@link TestElement}[]
* @param result {@link HTTPSampleResult}
* @return {@link Authorization}
*/
private Authorization createAuthorization(final TestElement[] testElements, SampleResult result) {
Header authHeader;
Authorization authorization = null;
// Iterate over subconfig elements searching for HeaderManager
for (TestElement te : testElements) {
if (te instanceof HeaderManager) {
// headers should only contain the correct classes
@SuppressWarnings("unchecked") List<TestElementProperty> headers = (ArrayList<TestElementProperty>) ((HeaderManager) te).getHeaders().getObjectValue();
for (Iterator<?> iterator = headers.iterator(); iterator.hasNext(); ) {
TestElementProperty tep = (TestElementProperty) iterator.next();
if (tep.getName().equals(HTTPConstants.HEADER_AUTHORIZATION)) {
// Construct Authorization object from HEADER_AUTHORIZATION
authHeader = (Header) tep.getObjectValue();
String headerValue = authHeader.getValue().trim();
// $NON-NLS-1$
String[] authHeaderContent = headerValue.split(" ");
String authType;
String authCredentialsBase64;
if (authHeaderContent.length >= 2) {
authType = authHeaderContent[0];
// if HEADER_AUTHORIZATION contains "Basic"
// then set Mechanism.BASIC_DIGEST, otherwise Mechanism.KERBEROS
Mechanism mechanism;
switch(authType) {
case BEARER_AUTH:
// This one will need to be correlated manually by user
return null;
case DIGEST_AUTH:
mechanism = Mechanism.DIGEST;
break;
case BASIC_AUTH:
mechanism = Mechanism.BASIC;
break;
default:
mechanism = Mechanism.KERBEROS;
break;
}
authCredentialsBase64 = authHeaderContent[1];
authorization = new Authorization();
authorization.setURL(computeAuthUrl(result.getUrlAsString()));
authorization.setMechanism(mechanism);
if (BASIC_AUTH.equals(authType)) {
String authCred = new String(Base64.decodeBase64(authCredentialsBase64), StandardCharsets.UTF_8);
// $NON-NLS-1$
String[] loginPassword = authCred.split(":");
if (loginPassword.length == 2) {
authorization.setUser(loginPassword[0]);
authorization.setPass(loginPassword[1]);
} else {
log.error("Error parsing BASIC Auth authorization header:'{}', decoded value:'{}'", authCredentialsBase64, authCred);
// we keep initial header
return null;
}
} else {
// Digest or Kerberos
// $NON-NLS-1$
authorization.setUser("${AUTH_LOGIN}");
// $NON-NLS-1$
authorization.setPass("${AUTH_PASSWORD}");
}
}
// remove HEADER_AUTHORIZATION from HeaderManager
// because it's useless after creating Authorization object
iterator.remove();
break;
}
}
}
}
return authorization;
}
Aggregations