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