YiDing-Duke commented on a change in pull request #11284: URL: https://github.com/apache/kafka/pull/11284#discussion_r732068749
########## File path: clients/src/main/java/org/apache/kafka/common/security/oauthbearer/secured/OAuthBearerLoginCallbackHandler.java ########## @@ -0,0 +1,188 @@ +/* + * 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.kafka.common.security.oauthbearer.secured; + +import static org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_TOKEN_ENDPOINT_URI; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.sasl.SaslException; + +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.config.ConfigException; +import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler; +import org.apache.kafka.common.security.auth.SaslExtensions; +import org.apache.kafka.common.security.auth.SaslExtensionsCallback; +import org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule; +import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken; +import org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback; +import org.apache.kafka.common.security.oauthbearer.internals.OAuthBearerClientInitialResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OAuthBearerLoginCallbackHandler implements AuthenticateCallbackHandler { + + private static final Logger log = LoggerFactory.getLogger(OAuthBearerLoginCallbackHandler.class); + + public static final String CLIENT_ID_CONFIG = "clientId"; + public static final String CLIENT_SECRET_CONFIG = "clientSecret"; + public static final String SCOPE_CONFIG = "scope"; + + public static final String CLIENT_ID_DOC = "The OAuth/OIDC identity provider-issued " + + "client ID to uniquely identify the service account to use for authentication for " + + "this client. The value must be paired with a corresponding " + CLIENT_SECRET_CONFIG + " " + + "value and is provided to the OAuth provider using the OAuth " + + "clientcredentials grant type."; + + public static final String CLIENT_SECRET_DOC = "The OAuth/OIDC identity provider-issued " + + "client secret serves a similar function as a password to the " + CLIENT_ID_CONFIG + " " + + "account and identifies the service account to use for authentication for " + + "this client. The value must be paired with a corresponding " + CLIENT_ID_CONFIG + " " + + "value and is provided to the OAuth provider using the OAuth " + + "clientcredentials grant type."; + + public static final String SCOPE_DOC = "The (optional) HTTP/HTTPS login request to the " + + "token endpoint (" + SASL_OAUTHBEARER_TOKEN_ENDPOINT_URI + ") may need to specify an " + + "OAuth \"scope\". If so, the " + SCOPE_CONFIG + " is used to provide the value to " + + "include with the login request."; + + private static final String EXTENSION_PREFIX = "extension_"; + + private Map<String, Object> moduleOptions; + + private AccessTokenRetriever accessTokenRetriever; + + private AccessTokenValidator accessTokenValidator; + + private boolean isConfigured = false; + + @Override + public void configure(Map<String, ?> configs, String saslMechanism, List<AppConfigurationEntry> jaasConfigEntries) { + if (!OAuthBearerLoginModule.OAUTHBEARER_MECHANISM.equals(saslMechanism)) + throw new IllegalArgumentException(String.format("Unexpected SASL mechanism: %s", saslMechanism)); + + if (Objects.requireNonNull(jaasConfigEntries).size() != 1 || jaasConfigEntries.get(0) == null) + throw new IllegalArgumentException(String.format("Must supply exactly 1 non-null JAAS mechanism configuration (size was %d)", jaasConfigEntries.size())); + + moduleOptions = Collections.unmodifiableMap(jaasConfigEntries.get(0).getOptions()); + AccessTokenRetriever accessTokenRetriever = AccessTokenRetrieverFactory.create(configs, saslMechanism, moduleOptions); + AccessTokenValidator accessTokenValidator = AccessTokenValidatorFactory.create(configs, saslMechanism); + configure(accessTokenRetriever, accessTokenValidator); + } + + public void configure(AccessTokenRetriever accessTokenRetriever, AccessTokenValidator accessTokenValidator) { + this.accessTokenRetriever = accessTokenRetriever; + this.accessTokenValidator = accessTokenValidator; + + try { + this.accessTokenRetriever.init(); + } catch (IOException e) { + throw new KafkaException("The OAuth login configuration encountered an error when initializing the AccessTokenRetriever", e); + } + + isConfigured = true; + } + + @Override + public void close() { + if (accessTokenRetriever != null) { + try { + this.accessTokenRetriever.close(); + } catch (IOException e) { + log.warn("The OAuth login configuration encountered an error when closing the AccessTokenRetriever", e); + } + } + } + + @Override + public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { + checkConfigured(); + + for (Callback callback : callbacks) { + if (callback instanceof OAuthBearerTokenCallback) { + handle((OAuthBearerTokenCallback) callback); + } else if (callback instanceof SaslExtensionsCallback) { + handle((SaslExtensionsCallback) callback); + } else { + throw new UnsupportedCallbackException(callback); + } + } + } + + void handle(OAuthBearerTokenCallback callback) throws IOException { Review comment: nit: Should we name these two handle functions separetely? like handleCallback() and handleExtensionsCallback()? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org