1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.apache;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.catalina.realm.GenericPrincipal;
32
33 import waffle.windows.auth.IWindowsAccount;
34 import waffle.windows.auth.IWindowsIdentity;
35 import waffle.windows.auth.PrincipalFormat;
36 import waffle.windows.auth.WindowsAccount;
37
38
39
40
41 public class GenericWindowsPrincipal extends GenericPrincipal {
42
43
44 private static final long serialVersionUID = 1L;
45
46
47 private final byte[] sid;
48
49
50 private final String sidString;
51
52
53 private final Map<String, WindowsAccount> groups;
54
55
56
57
58
59
60
61
62
63
64
65 public GenericWindowsPrincipal(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
66 final PrincipalFormat roleFormat) {
67 super(windowsIdentity.getFqn(), "",
68 GenericWindowsPrincipal.getRoles(windowsIdentity, principalFormat, roleFormat));
69 this.sid = windowsIdentity.getSid();
70 this.sidString = windowsIdentity.getSidString();
71 this.groups = GenericWindowsPrincipal.getGroups(windowsIdentity.getGroups());
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86 private static List<String> getRoles(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
87 final PrincipalFormat roleFormat) {
88 final List<String> roles = new ArrayList<>();
89 roles.addAll(GenericWindowsPrincipal.getPrincipalNames(windowsIdentity, principalFormat));
90 for (final IWindowsAccount group : windowsIdentity.getGroups()) {
91 roles.addAll(GenericWindowsPrincipal.getRoleNames(group, roleFormat));
92 }
93 return roles;
94 }
95
96
97
98
99
100
101
102
103
104 private static Map<String, WindowsAccount> getGroups(final IWindowsAccount[] groups) {
105 final Map<String, WindowsAccount> groupMap = new HashMap<>();
106 for (final IWindowsAccount group : groups) {
107 groupMap.put(group.getFqn(), new WindowsAccount(group));
108 }
109 return groupMap;
110 }
111
112
113
114
115
116
117 public byte[] getSid() {
118 return this.sid.clone();
119 }
120
121
122
123
124
125
126 public String getSidString() {
127 return this.sidString;
128 }
129
130
131
132
133
134
135 public Map<String, WindowsAccount> getGroups() {
136 return this.groups;
137 }
138
139
140
141
142
143
144
145
146
147
148
149 private static List<String> getRoleNames(final IWindowsAccount group, final PrincipalFormat principalFormat) {
150 final List<String> principals = new ArrayList<>();
151 switch (principalFormat) {
152 case FQN:
153 principals.add(group.getFqn());
154 break;
155 case SID:
156 principals.add(group.getSidString());
157 break;
158 case BOTH:
159 principals.add(group.getFqn());
160 principals.add(group.getSidString());
161 break;
162 case NONE:
163 default:
164 break;
165 }
166 return principals;
167 }
168
169
170
171
172
173
174
175
176
177
178
179 private static List<String> getPrincipalNames(final IWindowsIdentity windowsIdentity,
180 final PrincipalFormat principalFormat) {
181 final List<String> principals = new ArrayList<>();
182 switch (principalFormat) {
183 case FQN:
184 principals.add(windowsIdentity.getFqn());
185 break;
186 case SID:
187 principals.add(windowsIdentity.getSidString());
188 break;
189 case BOTH:
190 principals.add(windowsIdentity.getFqn());
191 principals.add(windowsIdentity.getSidString());
192 break;
193 case NONE:
194 default:
195 break;
196 }
197 return principals;
198 }
199
200
201
202
203
204
205 public String getRolesString() {
206 return String.join(", ", this.getRoles());
207 }
208
209 }