-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AuthenticationProviderBasic.java
170 lines (151 loc) · 6.76 KB
/
AuthenticationProviderBasic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* 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.pulsar.broker.authentication;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.AuthenticationException;
import lombok.Cleanup;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.metrics.AuthenticationMetrics;
public class AuthenticationProviderBasic implements AuthenticationProvider {
private static final String HTTP_HEADER_NAME = "Authorization";
private static final String CONF_SYSTEM_PROPERTY_KEY = "pulsar.auth.basic.conf";
private static final String CONF_PULSAR_PROPERTY_KEY = "basicAuthConf";
private Map<String, String> users;
@Override
public void close() throws IOException {
// noop
}
@Override
public void initialize(ServiceConfiguration config) throws IOException {
String data = config.getProperties().getProperty(CONF_PULSAR_PROPERTY_KEY);
if (StringUtils.isEmpty(data)) {
data = System.getProperty(CONF_SYSTEM_PROPERTY_KEY);
}
if (StringUtils.isEmpty(data)) {
throw new IOException("No basic authentication config provided");
}
@Cleanup BufferedReader reader = null;
if (org.apache.commons.codec.binary.Base64.isBase64(data)) {
reader = new BufferedReader(new StringReader(new String(Base64.getDecoder().decode(data),
StandardCharsets.UTF_8)));
} else {
File confFile = new File(data);
if (!confFile.exists()) {
throw new IOException("The password auth conf file does not exist");
} else if (!confFile.isFile()) {
throw new IOException("The path is not a file");
}
reader = new BufferedReader(new FileReader(confFile));
}
users = new HashMap<>();
for (String line : reader.lines().toArray(s -> new String[s])) {
List<String> splitLine = Arrays.asList(line.split(":"));
if (splitLine.size() != 2) {
throw new IOException("The format of the password auth conf file is invalid");
}
users.put(splitLine.get(0), splitLine.get(1));
}
}
@Override
public String getAuthMethodName() {
return "basic";
}
@Override
public String authenticate(AuthenticationDataSource authData) throws AuthenticationException {
AuthParams authParams = new AuthParams(authData);
String userId = authParams.getUserId();
String password = authParams.getPassword();
String msg = "Unknown user or invalid password";
try {
if (users.get(userId) == null) {
throw new AuthenticationException(msg);
}
String encryptedPassword = users.get(userId);
// For md5 algorithm
if ((users.get(userId).startsWith("$apr1"))) {
List<String> splitEncryptedPassword = Arrays.asList(encryptedPassword.split("\\$"));
if (splitEncryptedPassword.size() != 4 || !encryptedPassword
.equals(Md5Crypt.apr1Crypt(password.getBytes(), splitEncryptedPassword.get(2)))) {
throw new AuthenticationException(msg);
}
// For crypt algorithm
} else if (!encryptedPassword.equals(Crypt.crypt(password.getBytes(), encryptedPassword.substring(0, 2)))) {
throw new AuthenticationException(msg);
}
} catch (AuthenticationException exception) {
AuthenticationMetrics.authenticateFailure(getClass().getSimpleName(), getAuthMethodName(),
exception.getMessage());
throw exception;
}
AuthenticationMetrics.authenticateSuccess(getClass().getSimpleName(), getAuthMethodName());
return userId;
}
private class AuthParams {
private String userId;
private String password;
public AuthParams(AuthenticationDataSource authData) throws AuthenticationException {
String authParams;
if (authData.hasDataFromCommand()) {
authParams = authData.getCommandData();
} else if (authData.hasDataFromHttp()) {
String rawAuthToken = authData.getHttpHeader(HTTP_HEADER_NAME);
// parsing and validation
if (StringUtils.isBlank(rawAuthToken) || !rawAuthToken.toUpperCase().startsWith("BASIC ")) {
throw new AuthenticationException("Authentication token has to be started with \"Basic \"");
}
String[] splitRawAuthToken = rawAuthToken.split(" ");
if (splitRawAuthToken.length != 2) {
throw new AuthenticationException("Base64 encoded token is not found");
}
try {
authParams = new String(Base64.getDecoder().decode(splitRawAuthToken[1]));
} catch (Exception e) {
throw new AuthenticationException("Base64 decoding is failure: " + e.getMessage());
}
} else {
throw new AuthenticationException("Authentication data source does not have data");
}
String[] parsedAuthParams = authParams.split(":");
if (parsedAuthParams.length != 2) {
throw new AuthenticationException("Base64 decoded params are invalid");
}
userId = parsedAuthParams[0];
password = parsedAuthParams[1];
}
public String getUserId() {
return userId;
}
public String getPassword() {
return password;
}
}
}