-
Notifications
You must be signed in to change notification settings - Fork 93
/
LdapSecurityProvider.java
132 lines (112 loc) · 4.96 KB
/
LdapSecurityProvider.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
/*
* 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.brooklyn.rest.security.provider;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.CharMatcher;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.config.StringConfigMap;
import org.apache.brooklyn.rest.BrooklynWebConfig;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.text.Strings;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
/**
* A {@link SecurityProvider} implementation that relies on LDAP to authenticate.
*
* @author Peter Veentjer.
*/
public class LdapSecurityProvider extends AbstractSecurityProvider implements SecurityProvider {
public static final Logger LOG = LoggerFactory.getLogger(LdapSecurityProvider.class);
public static final String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private final String ldapUrl;
private final String ldapRealm;
private final String organizationUnit;
public LdapSecurityProvider(ManagementContext mgmt) {
StringConfigMap properties = mgmt.getConfig();
ldapUrl = properties.getConfig(BrooklynWebConfig.LDAP_URL);
Strings.checkNonEmpty(ldapUrl, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_URL);
ldapRealm = CharMatcher.isNot('"').retainFrom(properties.getConfig(BrooklynWebConfig.LDAP_REALM));
Strings.checkNonEmpty(ldapRealm, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_REALM);
if(Strings.isBlank(properties.getConfig(BrooklynWebConfig.LDAP_OU))) {
LOG.info("Setting LDAP ou attribute to: Users");
organizationUnit = "Users";
} else {
organizationUnit = CharMatcher.isNot('"').retainFrom(properties.getConfig(BrooklynWebConfig.LDAP_OU));
}
Strings.checkNonEmpty(ldapRealm, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_OU);
}
public LdapSecurityProvider(String ldapUrl, String ldapRealm, String organizationUnit) {
this.ldapUrl = ldapUrl;
this.ldapRealm = ldapRealm;
this.organizationUnit = organizationUnit;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean authenticate(HttpSession session, String user, String password) {
if (session==null || user==null) return false;
checkCanLoad();
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, getUserDN(user));
env.put(Context.SECURITY_CREDENTIALS, password);
try {
new InitialDirContext(env);
return allow(session, user);
} catch (NamingException e) {
return false;
}
}
/**
* Returns the LDAP path for the user
*
* @param user
* @return String
*/
protected String getUserDN(String user) {
List<String> domain = Lists.transform(Arrays.asList(ldapRealm.split("\\.")), new Function<String, String>() {
@Override
public String apply(String input) {
return "dc=" + input;
}
});
String dc = Joiner.on(",").join(domain).toLowerCase();
return "cn=" + user + ",ou=" + organizationUnit + "," + dc;
}
static boolean triedLoading = false;
public synchronized static void checkCanLoad() {
if (triedLoading) return;
try {
Class.forName(LDAP_CONTEXT_FACTORY);
triedLoading = true;
} catch (Throwable e) {
throw Exceptions.propagate(new ClassNotFoundException("Unable to load LDAP classes ("+LDAP_CONTEXT_FACTORY+") required for Brooklyn LDAP security provider"));
}
}
}