View Javadoc
1   /*
2    * Copyright 2013–2019 Michael Osipov
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.michaelo.tomcat.realm.mapper;
17  
18  import javax.naming.NamingException;
19  import javax.naming.directory.DirContext;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.ietf.jgss.GSSName;
23  
24  /**
25   * A mapper for the AD attribute {@code userPrincipalName}. This mapper maps the GSS name to the AD
26   * attribute {@code userPrincipalName} which by default contains the implicit UPN unless it's
27   * overwritten by the explicit (enterprise) UPN. In this case, the result will be empty. No
28   * assumption is made about the root DN set in the given context, so you can narrow down your search
29   * base if you like.
30   *
31   * @version $Id: UserPrincipalNameSearchMapper.java 317 2019-03-09 21:26:28Z michael-o $
32   */
33  public class UserPrincipalNameSearchMapper implements UsernameSearchMapper {
34  
35  	protected static class UserPrincipalNameMappedValues implements MappedValues {
36  
37  		private String searchUsername;
38  
39  		protected UserPrincipalNameMappedValues(String searchUsername) {
40  			this.searchUsername = searchUsername;
41  		}
42  
43  		@Override
44  		public String getSearchBase() {
45  			return StringUtils.EMPTY;
46  		}
47  
48  		@Override
49  		public String getSearchAttributeName() {
50  			return "userPrincipalName";
51  		}
52  
53  		@Override
54  		public String getSearchUsername() {
55  			return searchUsername;
56  		}
57  
58  	}
59  
60  	public synchronized MappedValues map(DirContext context, GSSName gssName)
61  			throws NamingException {
62  
63  		return new UserPrincipalNameMappedValues(gssName.toString());
64  	}
65  
66  }