Search in sources :

Example 1 with OAuthBearerExtensionsValidatorCallback

use of org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback in project kafka by apache.

the class OAuthBearerSaslServerTest method throwsAuthenticationExceptionOnInvalidExtensions.

/**
 * If the callback handler handles the `OAuthBearerExtensionsValidatorCallback`
 *  and finds an invalid extension, SaslServer should throw an authentication exception
 */
@Test
public void throwsAuthenticationExceptionOnInvalidExtensions() {
    OAuthBearerUnsecuredValidatorCallbackHandler invalidHandler = new OAuthBearerUnsecuredValidatorCallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof OAuthBearerValidatorCallback) {
                    OAuthBearerValidatorCallback validationCallback = (OAuthBearerValidatorCallback) callback;
                    validationCallback.token(new OAuthBearerTokenMock());
                } else if (callback instanceof OAuthBearerExtensionsValidatorCallback) {
                    OAuthBearerExtensionsValidatorCallback extensionsCallback = (OAuthBearerExtensionsValidatorCallback) callback;
                    extensionsCallback.error("firstKey", "is not valid");
                    extensionsCallback.error("secondKey", "is not valid either");
                } else
                    throw new UnsupportedCallbackException(callback);
            }
        }
    };
    saslServer = new OAuthBearerSaslServer(invalidHandler);
    Map<String, String> customExtensions = new HashMap<>();
    customExtensions.put("firstKey", "value");
    customExtensions.put("secondKey", "value");
    assertThrows(SaslAuthenticationException.class, () -> saslServer.evaluateResponse(clientInitialResponse(null, false, customExtensions)));
}
Also used : OAuthBearerUnsecuredValidatorCallbackHandler(org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerUnsecuredValidatorCallbackHandler) OAuthBearerTokenMock(org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenMock) OAuthBearerTokenCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback) OAuthBearerValidatorCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback) OAuthBearerExtensionsValidatorCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback) Callback(javax.security.auth.callback.Callback) HashMap(java.util.HashMap) OAuthBearerValidatorCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback) OAuthBearerExtensionsValidatorCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Test(org.junit.jupiter.api.Test)

Example 2 with OAuthBearerExtensionsValidatorCallback

use of org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback in project kafka by apache.

the class OAuthBearerSaslServer method processExtensions.

private Map<String, String> processExtensions(OAuthBearerToken token, SaslExtensions extensions) throws SaslException {
    OAuthBearerExtensionsValidatorCallback extensionsCallback = new OAuthBearerExtensionsValidatorCallback(token, extensions);
    try {
        callbackHandler.handle(new Callback[] { extensionsCallback });
    } catch (UnsupportedCallbackException e) {
    // backwards compatibility - no extensions will be added
    } catch (IOException e) {
        handleCallbackError(e);
    }
    if (!extensionsCallback.invalidExtensions().isEmpty()) {
        String errorMessage = String.format("Authentication failed: %d extensions are invalid! They are: %s", extensionsCallback.invalidExtensions().size(), Utils.mkString(extensionsCallback.invalidExtensions(), "", "", ": ", "; "));
        log.debug(errorMessage);
        throw new SaslAuthenticationException(errorMessage);
    }
    return extensionsCallback.validatedExtensions();
}
Also used : OAuthBearerExtensionsValidatorCallback(org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException)

Aggregations

UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)2 OAuthBearerExtensionsValidatorCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Callback (javax.security.auth.callback.Callback)1 SaslAuthenticationException (org.apache.kafka.common.errors.SaslAuthenticationException)1 OAuthBearerTokenCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback)1 OAuthBearerTokenMock (org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenMock)1 OAuthBearerValidatorCallback (org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback)1 OAuthBearerUnsecuredValidatorCallbackHandler (org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerUnsecuredValidatorCallbackHandler)1 Test (org.junit.jupiter.api.Test)1