1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.io.encoder;
21
22
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.security.auth.kerberos.KerberosPrincipal;
28
29 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
30 import org.apache.directory.shared.asn1.der.DERGeneralString;
31 import org.apache.directory.shared.asn1.der.DERInteger;
32 import org.apache.directory.shared.asn1.der.DERSequence;
33 import org.apache.directory.shared.asn1.der.DERTaggedObject;
34
35
36
37
38
39
40 public class PrincipalNameEncoder
41 {
42 private static final String COMPONENT_SEPARATOR = "/";
43 private static final String REALM_SEPARATOR = "@";
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static DERSequence encode( KerberosPrincipal principal )
58 {
59 DERSequence vector = new DERSequence();
60
61 vector.add( new DERTaggedObject( 0, DERInteger.valueOf( principal.getNameType() ) ) );
62 vector.add( new DERTaggedObject( 1, encodeNameSequence( principal ) ) );
63
64 return vector;
65 }
66
67
68
69
70
71
72
73
74 public static DERSequence encode( PrincipalName name )
75 {
76 DERSequence vector = new DERSequence();
77
78 vector.add( new DERTaggedObject( 0, DERInteger.valueOf( name.getNameType().getOrdinal() ) ) );
79 vector.add( new DERTaggedObject( 1, encodeNameSequence( name ) ) );
80
81 return vector;
82 }
83
84
85 private static DERSequence encodeNameSequence( KerberosPrincipal principal )
86 {
87 Iterator<String> it = getNameStrings( principal ).iterator();
88
89 DERSequence vector = new DERSequence();
90
91 while ( it.hasNext() )
92 {
93 vector.add( DERGeneralString.valueOf( it.next() ) );
94 }
95
96 return vector;
97 }
98
99
100 private static List<String> getNameStrings( KerberosPrincipal principal )
101 {
102 String nameComponent = principal.getName().split( REALM_SEPARATOR )[0];
103 String[] components = nameComponent.split( COMPONENT_SEPARATOR );
104 return Arrays.asList( components );
105 }
106
107
108 private static DERSequence encodeNameSequence( PrincipalName principalName )
109 {
110 DERSequence vector = new DERSequence();
111
112 for ( String name:principalName.getNames() )
113 {
114 vector.add( DERGeneralString.valueOf( name ) );
115 }
116
117 return vector;
118 }
119 }