1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.michaelo.tomcat.realm;
17
18 import java.security.Principal;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.catalina.TomcatPrincipal;
25 import org.ietf.jgss.GSSCredential;
26 import org.ietf.jgss.GSSName;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class ActiveDirectoryPrincipal implements TomcatPrincipal {
43
44 private final GSSName gssName;
45 private final Sid sid;
46 private final transient GSSCredential gssCredential;
47 private final String[] roles;
48 private final Map<String, Object> additionalAttributes;
49
50
51
52
53 public ActiveDirectoryPrincipal(GSSName gssName, Sid sid, GSSCredential gssCredential) {
54 this(gssName, sid, null, gssCredential, null);
55 }
56
57
58
59
60 public ActiveDirectoryPrincipal(GSSName gssName, Sid sid, List<String> roles,
61 GSSCredential gssCredential, Map<String, Object> additionalAttributes) {
62 this.gssName = gssName;
63 this.sid = sid;
64 if (roles == null || roles.isEmpty())
65 this.roles = new String[0];
66 else {
67 this.roles = roles.toArray(new String[0]);
68 Arrays.sort(this.roles);
69 }
70 this.gssCredential = gssCredential;
71 if (additionalAttributes == null || additionalAttributes.isEmpty())
72 this.additionalAttributes = Collections.emptyMap();
73 else
74 this.additionalAttributes = Collections.unmodifiableMap(additionalAttributes);
75 }
76
77 @Override
78 public Principal getUserPrincipal() {
79 return this;
80 }
81
82 @Override
83 public String getName() {
84 return gssName.toString();
85 }
86
87
88
89
90
91
92 public GSSName getGssName() {
93 return gssName;
94 }
95
96
97
98
99
100
101 public Sid getSid() {
102 return sid;
103 }
104
105 @Override
106 public GSSCredential getGssCredential() {
107 return gssCredential;
108 }
109
110
111
112
113
114
115
116
117 public boolean hasRole(String role) {
118 if ("*".equals(role))
119 return true;
120 if (role == null)
121 return false;
122 return Arrays.binarySearch(roles, role) >= 0;
123 }
124
125
126
127
128
129
130 public String[] getRoles() {
131 return Arrays.copyOf(roles, roles.length);
132 }
133
134
135
136
137
138
139 public Map<String, Object> getAdditionalAttributes() {
140 return additionalAttributes;
141 }
142
143 @Override
144 public boolean equals(Object obj) {
145 if (obj == null)
146 return false;
147
148 if (!(obj instanceof ActiveDirectoryPrincipal))
149 return false;
150
151 ActiveDirectoryPrincipal other = (ActiveDirectoryPrincipal) obj;
152
153 return gssName.equals((Object) other.gssName);
154 }
155
156 @Override
157 public int hashCode() {
158 return gssName.hashCode();
159 }
160
161 @Override
162 public String toString() {
163 return gssName.toString();
164 }
165
166 @Override
167 public void logout() throws Exception {
168 if (gssCredential != null) {
169 gssCredential.dispose();
170 }
171 }
172
173 }