[DMAAP-KAFKA] Release image 1.1.0
[dmaap/kafka11aaf.git] / src / main / java / org / onap / dmaap / kafkaauthorize / PlainSaslServer1.java
1 /******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
12 *  
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=========================================================
19  *  
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.kafkaauthorize;
23
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Map;
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;
36
37 /**
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.
41  * <p>
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.
45  * <p>
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.
49  *
50  */
51 public class PlainSaslServer1 implements SaslServer {
52
53     public static final String PLAIN_MECHANISM = "PLAIN";
54
55     private boolean complete;
56     private String authorizationId;
57     private static final String AUTH_EXC_NOT_COMPLETE = "Authentication exchange has not completed";
58
59
60     /**
61      * @throws SaslAuthenticationException if username/password combination is invalid or if the requested
62      *         authorization id is not the same as username.
63      * <p>
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.
68      * </p>
69      */
70     @Override
71     public byte[] evaluateResponse(byte[] responseBytes) throws SaslAuthenticationException {
72         /*
73          * Message format (from https://tools.ietf.org/html/rfc4616):
74          *
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
80          *
81          * SAFE      = UTF1 / UTF2 / UTF3 / UTF4
82          *                ;; any UTF-8 encoded Unicode character except NUL
83          */
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);
89
90         if (username.isEmpty()) {
91             throw new SaslAuthenticationException("Authentication failed: username not specified");
92         }
93         if (password.isEmpty()) {
94             throw new SaslAuthenticationException("Authentication failed: password not specified");
95         }
96
97         String aafResponse = "Not Verified";
98                 try {
99                         aafResponse = AuthorizationProviderFactory.getProviderFactory().getProvider().authenticate(username,
100                                         password);
101                 } catch (Exception ignored) {
102             throw new SaslAuthenticationException("Authentication failed: " + aafResponse + " User " + username);
103                 }
104                 if (null != aafResponse) {
105                         throw new SaslAuthenticationException("Authentication failed: " + aafResponse + " User " + username);
106                 }
107
108         if (!authorizationIdFromClient.isEmpty() && !authorizationIdFromClient.equals(username))
109             throw new SaslAuthenticationException("Authentication failed: Client requested an authorization id that is different from username");
110
111         this.authorizationId = username;
112
113         complete = true;
114         return new byte[0];
115     }
116
117     private List<String> extractTokens(String string) {
118         List<String> tokens = new ArrayList<>();
119         int startIndex = 0;
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));
124                 break;
125             }
126             tokens.add(string.substring(startIndex, endIndex));
127             startIndex = endIndex + 1;
128         }
129
130         if (tokens.size() != 3)
131             throw new SaslAuthenticationException("Invalid SASL/PLAIN response: expected 3 tokens, got " +
132                 tokens.size());
133
134         return tokens;
135     }
136
137     @Override
138     public String getAuthorizationID() {
139         if (!complete)
140             throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
141         return authorizationId;
142     }
143
144     @Override
145     public String getMechanismName() {
146         return PLAIN_MECHANISM;
147     }
148
149     @Override
150     public Object getNegotiatedProperty(String propName) {
151         if (!complete)
152             throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
153         return null;
154     }
155
156     @Override
157     public boolean isComplete() {
158         return complete;
159     }
160
161     @Override
162     public byte[] unwrap(byte[] incoming, int offset, int len) {
163         if (!complete)
164             throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
165         return Arrays.copyOfRange(incoming, offset, offset + len);
166     }
167
168     @Override
169     public byte[] wrap(byte[] outgoing, int offset, int len) {
170         if (!complete)
171             throw new IllegalStateException(AUTH_EXC_NOT_COMPLETE);
172         return Arrays.copyOfRange(outgoing, offset, offset + len);
173     }
174
175     @Override
176     public void dispose() {
177         // TODO Auto-generate method stub
178     }
179
180     public static class PlainSaslServerFactory1 implements SaslServerFactory {
181
182         @Override
183         public SaslServer createSaslServer(String mechanism, String protocol, String serverName, Map<String, ?> props, CallbackHandler cbh)
184             throws SaslException {
185
186             if (!PLAIN_MECHANISM.equals(mechanism))
187                 throw new SaslException(String.format("Mechanism '%s' is not supported. Only PLAIN is supported.", mechanism));
188
189             return new PlainSaslServer1();
190         }
191
192         @Override
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[]{};
198             else
199                 return new String[]{PLAIN_MECHANISM};
200         }
201     }
202 }
203