Search in sources :

Example 1 with Mechanism

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;
}
Also used : Authorization(org.apache.jmeter.protocol.http.control.Authorization) Header(org.apache.jmeter.protocol.http.control.Header) TestElementProperty(org.apache.jmeter.testelement.property.TestElementProperty) ArrayList(java.util.ArrayList) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) NonTestElement(org.apache.jmeter.testelement.NonTestElement) Mechanism(org.apache.jmeter.protocol.http.control.AuthManager.Mechanism) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Aggregations

ArrayList (java.util.ArrayList)1 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)1 Mechanism (org.apache.jmeter.protocol.http.control.AuthManager.Mechanism)1 Authorization (org.apache.jmeter.protocol.http.control.Authorization)1 Header (org.apache.jmeter.protocol.http.control.Header)1 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)1 NonTestElement (org.apache.jmeter.testelement.NonTestElement)1 TestElement (org.apache.jmeter.testelement.TestElement)1 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)1