• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Waffle / waffle / 6516

11 Mar 2026 12:15AM UTC coverage: 46.217%. Remained the same
6516

push

github

web-flow
Update dependency org.eclipse.jdt:ecj to v3.45.0

276 of 734 branches covered (37.6%)

Branch coverage included in aggregate %.

1019 of 2068 relevant lines covered (49.27%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.74
/Source/JNA/waffle-jna/src/main/java/waffle/servlet/WindowsPrincipal.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6
 */
7
package waffle.servlet;
8

9
import java.io.Serializable;
10
import java.security.Principal;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17

18
import waffle.windows.auth.IWindowsAccount;
19
import waffle.windows.auth.IWindowsIdentity;
20
import waffle.windows.auth.PrincipalFormat;
21
import waffle.windows.auth.WindowsAccount;
22

23
/**
24
 * A Windows Principal.
25
 */
26
public class WindowsPrincipal implements Principal, Serializable {
27

28
    /** The Constant serialVersionUID. */
29
    private static final long serialVersionUID = 1L;
30

31
    /** The fqn. */
32
    private final String fqn;
33

34
    /** The sid. */
35
    private final byte[] sid;
36

37
    /** The sid string. */
38
    private final String sidString;
39

40
    /** The roles. */
41
    private final Set<String> roles;
42

43
    /** The identity. */
44
    private transient IWindowsIdentity identity;
45

46
    /** The groups. */
47
    private final Map<String, WindowsAccount> groups;
48

49
    /**
50
     * A windows principal.
51
     *
52
     * @param windowsIdentity
53
     *            Windows identity.
54
     */
1✔
55
    public WindowsPrincipal(final IWindowsIdentity windowsIdentity) {
1✔
56
        this(windowsIdentity, PrincipalFormat.FQN, PrincipalFormat.FQN);
1✔
57
    }
1✔
58

59
    /**
60
     * A windows principal.
61
     *
62
     * @param windowsIdentity
63
     *            Windows identity.
64
     * @param principalFormat
65
     *            Principal format.
66
     * @param roleFormat
67
     *            Role format.
68
     */
1✔
69
    public WindowsPrincipal(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
1✔
70
            final PrincipalFormat roleFormat) {
2✔
71
        this.identity = windowsIdentity;
2✔
72
        this.fqn = windowsIdentity.getFqn();
2✔
73
        this.sid = windowsIdentity.getSid();
2✔
74
        this.sidString = windowsIdentity.getSidString();
2✔
75
        this.groups = WindowsPrincipal.getGroups(windowsIdentity.getGroups());
2✔
76
        this.roles = WindowsPrincipal.getRoles(windowsIdentity, principalFormat, roleFormat);
1✔
77
    }
1✔
78

79
    /**
80
     * Gets the roles.
81
     *
82
     * @param windowsIdentity
83
     *            the windows identity
84
     * @param principalFormat
85
     *            the principal format
86
     * @param roleFormat
87
     *            the role format
88
     *
89
     * @return the roles
90
     */
91
    private static Set<String> getRoles(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
1✔
92
            final PrincipalFormat roleFormat) {
1✔
93
        final Set<String> roles = new HashSet<>();
2!
94
        roles.addAll(WindowsPrincipal.getPrincipalNames(windowsIdentity, principalFormat));
1✔
95
        for (final IWindowsAccount group : windowsIdentity.getGroups()) {
1!
96
            roles.addAll(WindowsPrincipal.getRoleNames(group, roleFormat));
1✔
97
        }
98
        return roles;
1✔
99
    }
100

101
    /**
102
     * Gets the groups.
103
     *
104
     * @param groups
105
     *            the groups
106
     *
107
     * @return the groups
108
     */
1✔
109
    private static Map<String, WindowsAccount> getGroups(final IWindowsAccount[] groups) {
1!
110
        final Map<String, WindowsAccount> groupMap = new HashMap<>();
1✔
111
        for (final IWindowsAccount group : groups) {
1!
112
            groupMap.put(group.getFqn(), new WindowsAccount(group));
1✔
113
        }
114
        return groupMap;
1✔
115
    }
116

117
    /**
118
     * Windows groups that the user is a member of.
119
     *
120
     * @return A map of group names to groups.
121
     */
×
122
    public Map<String, WindowsAccount> getGroups() {
123
        return this.groups;
×
124
    }
125

126
    /**
127
     * Byte representation of the SID.
128
     *
129
     * @return Array of bytes.
130
     */
×
131
    public byte[] getSid() {
132
        return this.sid.clone();
×
133
    }
134

135
    /**
136
     * String representation of the SID.
137
     *
138
     * @return String.
139
     */
×
140
    public String getSidString() {
141
        return this.sidString;
×
142
    }
143

144
    /**
145
     * Returns a list of role principal objects.
146
     *
147
     * @param group
148
     *            Windows group.
149
     * @param principalFormat
150
     *            Principal format.
151
     *
152
     * @return List of role principal objects.
153
     */
×
154
    private static List<String> getRoleNames(final IWindowsAccount group, final PrincipalFormat principalFormat) {
×
155
        final List<String> principals = new ArrayList<>();
×
156
        switch (principalFormat) {
×
157
            case FQN:
×
158
                principals.add(group.getFqn());
×
159
                break;
×
160
            case SID:
×
161
                principals.add(group.getSidString());
×
162
                break;
×
163
            case BOTH:
×
164
                principals.add(group.getFqn());
×
165
                principals.add(group.getSidString());
×
166
                break;
×
167
            case NONE:
168
            default:
169
                break;
×
170
        }
171
        return principals;
×
172
    }
173

174
    /**
175
     * Returns a list of user principal objects.
176
     *
177
     * @param windowsIdentity
178
     *            Windows identity.
179
     * @param principalFormat
180
     *            Principal format.
181
     *
182
     * @return A list of user principal objects.
183
     */
184
    private static List<String> getPrincipalNames(final IWindowsIdentity windowsIdentity,
1✔
185
            final PrincipalFormat principalFormat) {
1!
186
        final List<String> principals = new ArrayList<>();
1✔
187
        switch (principalFormat) {
2!
188
            case FQN:
1✔
189
                principals.add(windowsIdentity.getFqn());
1✔
190
                break;
1✔
191
            case SID:
×
192
                principals.add(windowsIdentity.getSidString());
×
193
                break;
×
194
            case BOTH:
×
195
                principals.add(windowsIdentity.getFqn());
×
196
                principals.add(windowsIdentity.getSidString());
×
197
                break;
×
198
            case NONE:
199
            default:
200
                break;
1✔
201
        }
202
        return principals;
1✔
203
    }
204

205
    /**
206
     * Get an array of roles as a string.
207
     *
208
     * @return Role1, Role2, ...
209
     */
×
210
    public String getRolesString() {
211
        return String.join(", ", this.roles);
×
212
    }
213

214
    /**
215
     * Checks whether the principal has a given role.
216
     *
217
     * @param role
218
     *            Role name.
219
     *
220
     * @return True if the principal has a role, false otherwise.
221
     */
×
222
    public boolean hasRole(final String role) {
223
        return this.roles.contains(role);
×
224
    }
225

226
    /**
227
     * Fully qualified name.
228
     *
229
     * @return String.
230
     */
231
    @Override
1✔
232
    public String getName() {
233
        return this.fqn;
1✔
234
    }
235

236
    /**
237
     * Underlying identity.
238
     *
239
     * @return String.
240
     */
×
241
    public IWindowsIdentity getIdentity() {
242
        return this.identity;
×
243
    }
244

245
    @Override
1✔
246
    public String toString() {
247
        return this.getName();
1✔
248
    }
249

250
    @Override
1!
251
    public boolean equals(final Object o) {
×
252
        if (this == o) {
1!
253
            return true;
×
254
        }
1!
255

1✔
256
        if (o instanceof WindowsPrincipal) {
1!
257
            return this.getName().equals(((WindowsPrincipal) o).getName());
1✔
258
        }
×
259

260
        return false;
×
261
    }
262

263
    @Override
1✔
264
    public int hashCode() {
265
        return this.getName().hashCode();
1✔
266
    }
267

268
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc