1 /******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Modification copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 *******************************************************************************/
22 package org.onap.dmaap.kafkaAuthorize;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
29 import javax.security.auth.callback.CallbackHandler;
30 import javax.security.sasl.Sasl;
31 import javax.security.sasl.SaslException;
32 import javax.security.sasl.SaslServer;
33 import javax.security.sasl.SaslServerFactory;
34 import org.apache.kafka.common.errors.SaslAuthenticationException;
35 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
38 * Simple SaslServer implementation for SASL/PLAIN. In order to make this
39 * implementation fully pluggable, authentication of username/password is fully
40 * contained within the server implementation.
42 * Valid users with passwords are specified in the Jaas configuration file. Each
43 * user is specified with user_<username> as key and <password> as value. This
44 * is consistent with Zookeeper Digest-MD5 implementation.
46 * To avoid storing clear passwords on disk or to integrate with external
47 * authentication servers in production systems, this module can be replaced
48 * with a different implementation.
51 public class PlainSaslServer1 implements SaslServer {
53 public static final String PLAIN_MECHANISM = "PLAIN";
55 private boolean complete;
56 private String authorizationId;
57 private static final String AUTH_EXC_NOT_COMPLETE = "Authentication exchange has not completed";
61 * @throws SaslAuthenticationException if username/password combination is invalid or if the requested
62 * authorization id is not the same as username.
64 * <b>Note:</b> This method may throw {@link SaslAuthenticationException} to provide custom error messages
65 * to clients. But care should be taken to avoid including any information in the exception message that
66 * should not be leaked to unauthenticated clients. It may be safer to throw {@link SaslException} in
67 * some cases so that a standard error message is returned to clients.
71 public byte[] evaluateResponse(byte[] responseBytes) throws SaslAuthenticationException {
73 * Message format (from https://tools.ietf.org/html/rfc4616):
75 * message = [authzid] UTF8NUL authcid UTF8NUL passwd
76 * authcid = 1*SAFE ; MUST accept up to 255 octets
77 * authzid = 1*SAFE ; MUST accept up to 255 octets
78 * passwd = 1*SAFE ; MUST accept up to 255 octets
79 * UTF8NUL = %x00 ; UTF-8 encoded NUL character
81 * SAFE = UTF1 / UTF2 / UTF3 / UTF4
82 * ;; any UTF-8 encoded Unicode character except NUL
84 String response = new String(responseBytes, StandardCharsets.UTF_8);
85 List<String> tokens = extractTokens(response);
86 String authorizationIdFromClient = tokens.get(0);
87 String username = tokens.get(1);
88 String password = tokens.get(2);
90 if (username.isEmpty()) {
91 throw new SaslAuthenticationException("Authentication failed: username not specified");
93 if (password.isEmpty()) {
94 throw new SaslAuthenticationException("Authentication failed: password not specified");
97 String aafResponse = "Not Verified";
99 aafResponse = AuthorizationProviderFactory.getProviderFactory().getProvider().authenticate(username,
101 } catch (Exception ignored) {
102 throw new SaslAuthenticationException("Authentication failed: " + aafResponse + " User " + username);
104 if (null != aafResponse) {
105 throw new SaslAuthenticationException("Authentication failed: " + aafResponse + " User " + username);
108 if (!authorizationIdFromClient.isEmpty() && !authorizationIdFromClient.equals(username))
109 throw new SaslAuthenticationException("Authentication failed: Client requested an authorization id that is different from username");
111 this.authorizationId = username;
117 private List<String> extractTokens(String string) {
118 List<String> tokens = new ArrayList<>();
120 for (int i = 0; i < 4; ++i) {
121 int endIndex = string.indexOf("\u0000", startIndex);
122 if (endIndex == -1) {
123 tokens.add(string.substring(startIndex));
126 tokens.add(string.substring(startIndex, endIndex));
127 startIndex = endIndex + 1;
130 if (tokens.size() != 3)
131 throw new SaslAuthenticationException("Invalid SASL/PLAIN response: expected 3 tokens, got " +
138 public String getAuthorizationID() {
140 throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
141 return authorizationId;
145 public String getMechanismName() {
146 return PLAIN_MECHANISM;
150 public Object getNegotiatedProperty(String propName) {
152 throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
157 public boolean isComplete() {
162 public byte[] unwrap(byte[] incoming, int offset, int len) {
164 throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
165 return Arrays.copyOfRange(incoming, offset, offset + len);
169 public byte[] wrap(byte[] outgoing, int offset, int len) {
171 throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
172 return Arrays.copyOfRange(outgoing, offset, offset + len);
176 public void dispose() {
177 // TODO Auto-generate method stub
180 public static class PlainSaslServerFactory1 implements SaslServerFactory {
183 public SaslServer createSaslServer(String mechanism, String protocol, String serverName, Map<String, ?> props, CallbackHandler cbh)
184 throws SaslException {
186 if (!PLAIN_MECHANISM.equals(mechanism))
187 throw new SaslException(String.format("Mechanism '%s' is not supported. Only PLAIN is supported.", mechanism));
189 return new PlainSaslServer1();
193 public String[] getMechanismNames(Map<String, ?> props) {
194 if (props == null) return new String[]{PLAIN_MECHANISM};
195 String noPlainText = (String) props.get(Sasl.POLICY_NOPLAINTEXT);
196 if ("true".equals(noPlainText))
197 return new String[]{};
199 return new String[]{PLAIN_MECHANISM};