1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.michaelo.tomcat.realm.mapper;
17
18 import java.util.Arrays;
19 import java.util.Locale;
20
21 import javax.naming.Name;
22 import javax.naming.NameParser;
23 import javax.naming.NamingException;
24 import javax.naming.directory.DirContext;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.ietf.jgss.GSSException;
28 import org.ietf.jgss.GSSName;
29 import org.ietf.jgss.Oid;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class SamAccountNameRfc2247Mapper extends SamAccountNameMapper {
48
49 protected final static Oid KRB5_NT_PRINCIPAL;
50
51 static {
52 try {
53 KRB5_NT_PRINCIPAL = new Oid("1.2.840.113554.1.2.2.1");
54 } catch (GSSException e) {
55 throw new IllegalStateException("Failed to create OID for KRB5_NT_PRINCIPAL");
56 }
57 }
58
59 private static final Oid[] SUPPORTED_STRING_NAME_TYPES = new Oid[] { KRB5_NT_PRINCIPAL };
60
61 @Override
62 public Oid[] getSupportedStringNameTypes() {
63 return Arrays.copyOf(SUPPORTED_STRING_NAME_TYPES, SUPPORTED_STRING_NAME_TYPES.length);
64 }
65
66 @Override
67 public boolean supportsGssName(GSSName gssName) {
68 try {
69 return gssName.getStringNameType().containedIn(SUPPORTED_STRING_NAME_TYPES);
70 } catch (GSSException e) {
71
72 return false;
73 }
74 }
75
76 public synchronized MappedValues map(DirContext context, GSSName gssName)
77 throws NamingException {
78 if (!supportsGssName(gssName))
79 throw new IllegalArgumentException("GSS name '" + gssName + "' is not supported");
80
81 String[] upnComponents = StringUtils.split(gssName.toString(), '@');
82 String samAccountName = upnComponents[0];
83 String realm = upnComponents[1];
84 String searchBase = StringUtils.EMPTY;
85
86 String[] realmComponents = StringUtils.split(realm, '.');
87 NameParser parser = context.getNameParser(StringUtils.EMPTY);
88 Name searchBaseName = parser.parse(StringUtils.EMPTY);
89
90 for (int i = realmComponents.length - 1; i >= 0; i--) {
91 searchBaseName.add("DC=" + realmComponents[i].toLowerCase(Locale.ROOT));
92 }
93
94 searchBase = searchBaseName.toString();
95
96 return new SamAccountNameMappedValues(searchBase, samAccountName);
97
98 }
99 }