-
Notifications
You must be signed in to change notification settings - Fork 27
/
LdapUserInfoRepository.java
190 lines (149 loc) · 5.01 KB
/
LdapUserInfoRepository.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package edu.mit.kit.repository.impl;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.mitre.openid.connect.model.DefaultUserInfo;
import org.mitre.openid.connect.model.UserInfo;
import org.mitre.openid.connect.repository.UserInfoRepository;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.Filter;
import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
/**
* Looks up the user information from an LDAP template and maps the results
* into a UserInfo object. This object is then cached.
*
* @author jricher
*
*/
// TODO: Make this class more pluggable and configurable
public class LdapUserInfoRepository implements UserInfoRepository {
private LdapTemplate ldapTemplate;
private String emailSuffix = "@example.com";
public LdapTemplate getLdapTemplate() {
return ldapTemplate;
}
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
//
// This code does the heavy lifting that maps the LDAP attributes into UserInfo attributes
//
private AttributesMapper attributesMapper = new AttributesMapper() {
@Override
public Object mapFromAttributes(Attributes attr) throws NamingException {
if (attr.get("uid") == null) {
return null; // we can't go on if there's no UID to look up
}
UserInfo ui = new DefaultUserInfo();
// save the UID as the preferred username
ui.setPreferredUsername(attr.get("uid").get().toString());
// for now we use the UID as the subject as well (this should probably be different)
ui.setSub(attr.get("uid").get().toString());
// add in the optional fields
// email address
if (attr.get("mail") != null) {
ui.setEmail(attr.get("mail").get().toString());
// if this domain also provisions email addresses, this should be set to true
ui.setEmailVerified(false);
}
// phone number
if (attr.get("telephoneNumber") != null) {
ui.setPhoneNumber(attr.get("telephoneNumber").get().toString());
// if this domain also provisions phone numbers, this should be set to true
ui.setPhoneNumberVerified(false);
}
// name structure
if (attr.get("displayName") != null) {
ui.setName(attr.get("displayName").get().toString());
}
if (attr.get("givenName") != null) {
ui.setGivenName(attr.get("givenName").get().toString());
}
if (attr.get("sn") != null) {
ui.setFamilyName(attr.get("sn").get().toString());
}
if (attr.get("initials") != null) {
ui.setMiddleName(attr.get("initials").get().toString());
}
return ui;
}
};
// lookup result cache, key from username to userinfo
private LoadingCache<String, UserInfo> cache;
private CacheLoader<String, UserInfo> cacheLoader = new CacheLoader<String, UserInfo>() {
@Override
public UserInfo load(String username) throws Exception {
Filter find = new EqualsFilter("uid", username);
List res = ldapTemplate.search("", find.encode(), attributesMapper);
if (res.isEmpty()) {
// user not found, error
throw new IllegalArgumentException("User not found: " + username);
} else if (res.size() == 1) {
// exactly one user found, return them
return (UserInfo) res.get(0);
} else {
// more than one user found, error
throw new IllegalArgumentException("User not found: " + username);
}
}
};
public LdapUserInfoRepository() {
this.cache = CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterAccess(14, TimeUnit.DAYS)
.build(cacheLoader);
}
@Override
public UserInfo getByUsername(String username) {
try {
return cache.get(username);
} catch (UncheckedExecutionException ue) {
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}
/**
* Strip off the suffix defined in emailSuffix and use the rest as a username
*/
/* (non-Javadoc)
* @see org.mitre.openid.connect.repository.UserInfoRepository#getByEmailAddress(java.lang.String)
*/
@Override
public UserInfo getByEmailAddress(String email) {
if (!Strings.isNullOrEmpty(email)) {
if (email.endsWith(getEmailSuffix())) {
String username = email.substring(0, email.length() - getEmailSuffix().length());
return getByUsername(username);
} else {
// email doesn't match, end
return null;
}
} else {
// email was null, end
return null;
}
}
/**
* @return the emailSuffix
*/
public String getEmailSuffix() {
return emailSuffix;
}
/**
* @param emailSuffix the emailSuffix to set
*/
public void setEmailSuffix(String emailSuffix) {
this.emailSuffix = emailSuffix;
}
}