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
43
44
45 public class ActiveDirectoryPrincipal implements TomcatPrincipal {
46
47 private final GSSName gssName;
48 private final Sid sid;
49 private final transient GSSCredential gssCredential;
50 private final String[] roles;
51 private final Map<String, Object> additionalAttributes;
52
53
54
55
56 public ActiveDirectoryPrincipal(GSSName gssName, Sid sid, GSSCredential gssCredential) {
57 this(gssName, sid, null, gssCredential, null);
58 }
59
60
61
62
63 public ActiveDirectoryPrincipal(GSSName gssName, Sid sid, List<String> roles,
64 GSSCredential gssCredential, Map<String, Object> additionalAttributes) {
65 this.gssName = gssName;
66 this.sid = sid;
67 if (roles == null || roles.isEmpty())
68 this.roles = new String[0];
69 else {
70 this.roles = roles.toArray(new String[0]);
71 Arrays.sort(this.roles);
72 }
73 this.gssCredential = gssCredential;
74 if (additionalAttributes == null || additionalAttributes.isEmpty())
75 this.additionalAttributes = Collections.emptyMap();
76 else
77 this.additionalAttributes = Collections.unmodifiableMap(additionalAttributes);
78 }
79
80 @Override
81 public Principal getUserPrincipal() {
82 return this;
83 }
84
85 @Override
86 public String getName() {
87 return gssName.toString();
88 }
89
90
91
92
93
94
95 public GSSName getGssName() {
96 return gssName;
97 }
98
99
100
101
102
103
104 public Sid getSid() {
105 return sid;
106 }
107
108 @Override
109 public GSSCredential getGssCredential() {
110 return gssCredential;
111 }
112
113
114
115
116
117
118
119
120 public boolean hasRole(String role) {
121 if ("*".equals(role))
122 return true;
123 if (role == null)
124 return false;
125 return Arrays.binarySearch(roles, role) >= 0;
126 }
127
128
129
130
131
132
133 public String[] getRoles() {
134 return Arrays.copyOf(roles, roles.length);
135 }
136
137
138
139
140
141
142 public Map<String, Object> getAdditionalAttributes() {
143 return additionalAttributes;
144 }
145
146 @Override
147 public boolean equals(Object obj) {
148 if (obj == null)
149 return false;
150
151 if (!(obj instanceof ActiveDirectoryPrincipal))
152 return false;
153
154 ActiveDirectoryPrincipal other = (ActiveDirectoryPrincipal) obj;
155
156 return gssName.equals((Object) other.gssName);
157 }
158
159 @Override
160 public int hashCode() {
161 return gssName.hashCode();
162 }
163
164 @Override
165 public String toString() {
166 return gssName.toString();
167 }
168
169 @Override
170 public void logout() throws Exception {
171 if (gssCredential != null) {
172 gssCredential.dispose();
173 }
174 }
175
176 }