Skip to content

Commit

Permalink
Merge pull request #2214 from hazendaz/master
Browse files Browse the repository at this point in the history
Continuation of cleanup
  • Loading branch information
hazendaz authored Jan 22, 2024
2 parents 952a41a + 04b9518 commit 68dc619
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testEquals_Symmetric() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.groupPrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testEquals_Symmetric() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.rolePrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testEquals_Symmetric() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.userPrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import waffle.windows.auth.IWindowsAccount;
import waffle.windows.auth.IWindowsIdentity;
Expand All @@ -53,7 +55,7 @@ public class WindowsPrincipal implements Principal, Serializable {
private final String sidString;

/** The roles. */
private final List<String> roles;
private final Set<String> roles;

/** The identity. */
private transient IWindowsIdentity identity;
Expand Down Expand Up @@ -103,9 +105,9 @@ public WindowsPrincipal(final IWindowsIdentity windowsIdentity, final PrincipalF
*
* @return the roles
*/
private static List<String> getRoles(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
private static Set<String> getRoles(final IWindowsIdentity windowsIdentity, final PrincipalFormat principalFormat,
final PrincipalFormat roleFormat) {
final List<String> roles = new ArrayList<>();
final Set<String> roles = new HashSet<>();
roles.addAll(WindowsPrincipal.getPrincipalNames(windowsIdentity, principalFormat));
for (final IWindowsAccount group : windowsIdentity.getGroups()) {
roles.addAll(WindowsPrincipal.getRoleNames(group, roleFormat));
Expand All @@ -129,6 +131,15 @@ private static Map<String, WindowsAccount> getGroups(final IWindowsAccount[] gro
return groupMap;
}

/**
* Windows groups that the user is a member of.
*
* @return A map of group names to groups.
*/
public Map<String, WindowsAccount> getGroups() {
return this.groups;
}

/**
* Byte representation of the SID.
*
Expand All @@ -147,15 +158,6 @@ public String getSidString() {
return this.sidString;
}

/**
* Windows groups that the user is a member of.
*
* @return A map of group names to groups.
*/
public Map<String, WindowsAccount> getGroups() {
return this.groups;
}

/**
* Returns a list of role principal objects.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface SecurityFilterProvider {
* @param response
* Http Response.
*/
void sendUnauthorized(final HttpServletResponse response);
void sendUnauthorized(HttpServletResponse response);

/**
* Returns true if despite having a principal authentication needs to happen.
Expand All @@ -51,7 +51,7 @@ public interface SecurityFilterProvider {
*
* @return True if authentication is required.
*/
boolean isPrincipalException(final HttpServletRequest request);
boolean isPrincipalException(HttpServletRequest request);

/**
* Execute filter.
Expand All @@ -67,7 +67,7 @@ public interface SecurityFilterProvider {
* @throws IOException
* on doFilter.
*/
IWindowsIdentity doFilter(final HttpServletRequest request, final HttpServletResponse response) throws IOException;
IWindowsIdentity doFilter(HttpServletRequest request, HttpServletResponse response) throws IOException;

/**
* Tests whether a specific security package is supported.
Expand All @@ -77,7 +77,7 @@ public interface SecurityFilterProvider {
*
* @return True if the security package is supported, false otherwise.
*/
boolean isSecurityPackageSupported(final String securityPackage);
boolean isSecurityPackageSupported(String securityPackage);

/**
* Init a parameter.
Expand All @@ -87,5 +87,5 @@ public interface SecurityFilterProvider {
* @param parameterValue
* Parameter value.
*/
void initParameter(final String parameterName, final String parameterValue);
void initParameter(String parameterName, String parameterValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
*/
public final class NtlmMessage {

// NTLM messages start with 0x4e544c4d53535000, NTLMSSP signature
/** The Constant NTLM_SSP_SIGNATURE. */
/** The Constant NTLM_SSP_SIGNATURE. NTLM messages start with 0x4e544c4d53535000, NTLMSSP signature */
private static final byte[] NTLM_SSP_SIGNATURE = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2010-2022 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
* Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -68,23 +68,23 @@
* <pre>
* This utility class collects system information and returns it as an XML document.
* </pre>
*
* <p>
* From the command line, you can write the info to stdout using:
*
* <pre>
* <code>
* java -cp "jna.jar;waffle-core.jar;waffle-api.jar;jna-platform.jar;guava-21.0.jar" waffle.util.WaffleInfo
* </code>
* </pre>
*
* <p>
* To show this information in a browser, run:
*
* <pre>
* <code>
* java -cp "..." waffle.util.WaffleInfo -show
* </code>
* </pre>
*
* <p>
* To lookup account names and return any listed info, run:
*
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public CaffeineCache(@NonNegative final long timeout) {
}

@Override
public V get(K key) {
public V get(final K key) {
return cache.asMap().get(key);
}

@Override
public void put(K key, V value) {
public void put(final K key, final V value) {
cache.put(key, value);
}

@Override
public void remove(K key) {
public void remove(final K key) {
cache.asMap().remove(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testEquals_Symmetric() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.rolePrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testEquals_Symmetric() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.userPrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

/**
* A authentication filter that implements the HTTP Negotiate mechanism. The current user is authenticated, providing
* single-sign-on Derived from net.skorgenes.security.jsecurity.negotiate.NegotiateAuthenticationFilter. see:
* single-sign-on. Derived from net.skorgenes.security.jsecurity.negotiate.NegotiateAuthenticationFilter. see:
* https://bitbucket.org/lothor
* /shiro-negotiate/src/7b25efde130b9cbcacf579b3f926c532d919aa23/src/main/java/net/skorgenes/
* security/jsecurity/negotiate/NegotiateAuthenticationFilter.java?at=default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.LMAccess;
import com.sun.jna.platform.win32.LMErr;
Expand Down Expand Up @@ -78,8 +77,8 @@ void setUp() {
this.filter.setProvider(new SecurityFilterProviderCollection(new WindowsAuthProviderImpl()));

this.userInfo = new LMAccess.USER_INFO_1();
this.userInfo.usri1_name = new WString(MockWindowsAccount.TEST_USER_NAME).toString();
this.userInfo.usri1_password = new WString(MockWindowsAccount.TEST_PASSWORD).toString();
this.userInfo.usri1_name = MockWindowsAccount.TEST_USER_NAME;
this.userInfo.usri1_password = MockWindowsAccount.TEST_PASSWORD;
this.userInfo.usri1_priv = LMAccess.USER_PRIV_USER;

this.resultOfNetAddUser = Netapi32.INSTANCE.NetUserAdd(null, 1, this.userInfo, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.LMAccess;
import com.sun.jna.platform.win32.LMErr;
Expand Down Expand Up @@ -78,8 +77,8 @@ void setUp() {
this.filter.setProvider(new SecurityFilterProviderCollection(new WindowsAuthProviderImpl()));

this.userInfo = new LMAccess.USER_INFO_1();
this.userInfo.usri1_name = new WString(MockWindowsAccount.TEST_USER_NAME).toString();
this.userInfo.usri1_password = new WString(MockWindowsAccount.TEST_PASSWORD).toString();
this.userInfo.usri1_name = MockWindowsAccount.TEST_USER_NAME;
this.userInfo.usri1_password = MockWindowsAccount.TEST_PASSWORD;
this.userInfo.usri1_priv = LMAccess.USER_PRIV_USER;

this.resultOfNetAddUser = Netapi32.INSTANCE.NetUserAdd(null, 1, this.userInfo, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public IWindowsIdentity logonDomainUserEx(final String username, final String do
@Override
public IWindowsIdentity logonUser(final String username, final String password) {
final String currentUsername = Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
if (username.equals(currentUsername)) {
if (currentUsername.equals(username)) {
return new MockWindowsIdentity(currentUsername, this.groups);
} else if (username.equals(MockWindowsAuthProvider.GUEST)) {
} else if (MockWindowsAuthProvider.GUEST.equals(username)) {
return new MockWindowsIdentity(MockWindowsAuthProvider.GUEST, this.groups);
} else {
throw new RuntimeException("Mock error: " + username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public String getQueryString() {
public void setQueryString(final String query) {
this.queryString = query;
if (this.queryString != null) {
for (final String eachParameter : this.queryString.split("[&]", -1)) {
for (final String eachParameter : this.queryString.split("&", -1)) {
final String[] pair = eachParameter.split("=", -1);
final String value = pair.length == 2 ? pair[1] : "";
this.addParameter(pair[0], value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class SimpleHttpResponse extends HttpServletResponseWrapper {
/** The out. */
private final ServletOutputStream out = new ServletOutputStream() {
@Override
public void write(final int b) throws IOException {
public void write(final int b) {
SimpleHttpResponse.this.bytes.write(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package waffle.servlet;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.LMAccess;
import com.sun.jna.platform.win32.LMErr;
Expand Down Expand Up @@ -78,8 +77,8 @@ void setUp() {
}

this.userInfo = new LMAccess.USER_INFO_1();
this.userInfo.usri1_name = new WString(MockWindowsAccount.TEST_USER_NAME).toString();
this.userInfo.usri1_password = new WString(MockWindowsAccount.TEST_PASSWORD).toString();
this.userInfo.usri1_name = MockWindowsAccount.TEST_USER_NAME;
this.userInfo.usri1_password = MockWindowsAccount.TEST_PASSWORD;
this.userInfo.usri1_priv = LMAccess.USER_PRIV_USER;

this.resultOfNetAddUser = Netapi32.INSTANCE.NetUserAdd(null, 1, this.userInfo, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.StringReader;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -109,6 +110,8 @@ void testGetInfo() throws ParserConfigurationException, SAXException, IOExceptio
private static Document loadXMLFromString(final String xml)
throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
final DocumentBuilder builder = factory.newDocumentBuilder();
final InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setUp() {
void testIsSerializable() throws IOException, ClassNotFoundException {
// serialize
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(this.windowsPrincipal);
}
assertThat(out.toByteArray()).isNotEmpty();
Expand All @@ -80,7 +80,7 @@ void testIsSerializable() throws IOException, ClassNotFoundException {
Assertions.assertEquals(this.windowsPrincipal.getName(), copy.getName());
Assertions.assertEquals(this.windowsPrincipal.getRolesString(), copy.getRolesString());
Assertions.assertEquals(this.windowsPrincipal.getSidString(), copy.getSidString());
Assertions.assertTrue(Boolean.valueOf(Arrays.equals(this.windowsPrincipal.getSid(), copy.getSid())));
Assertions.assertTrue(Arrays.equals(this.windowsPrincipal.getSid(), copy.getSid()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.LMAccess;
import com.sun.jna.platform.win32.LMErr;
Expand Down Expand Up @@ -80,14 +79,14 @@ void testLogonGuestUser() {
@Test
void testLogonUser() {
final LMAccess.USER_INFO_1 userInfo = new LMAccess.USER_INFO_1();
userInfo.usri1_name = new WString("WaffleTestUser").toString();
userInfo.usri1_password = new WString("!WAFFLEP$$Wrd0").toString();
userInfo.usri1_name = "WaffleTestUser";
userInfo.usri1_password = "!WAFFLEP$$Wrd0";
userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
// ignore test if not able to add user (need to be administrator to do this).
Assumptions.assumeTrue(LMErr.NERR_Success == Netapi32.INSTANCE.NetUserAdd(null, 1, userInfo, null));
try {
final IWindowsAuthProvider prov = new WindowsAuthProviderImpl();
final IWindowsIdentity identity = prov.logonUser(userInfo.usri1_name, userInfo.usri1_password.toString());
final IWindowsIdentity identity = prov.logonUser(userInfo.usri1_name, userInfo.usri1_password);
Assertions.assertTrue(identity.getFqn().endsWith("\\" + userInfo.usri1_name));
Assertions.assertFalse(identity.isGuest());
identity.dispose();
Expand All @@ -102,8 +101,8 @@ void testLogonUser() {
@Test
void testImpersonateLoggedOnUser() {
final LMAccess.USER_INFO_1 userInfo = new LMAccess.USER_INFO_1();
userInfo.usri1_name = new WString(MockWindowsAccount.TEST_USER_NAME).toString();
userInfo.usri1_password = new WString(MockWindowsAccount.TEST_PASSWORD).toString();
userInfo.usri1_name = MockWindowsAccount.TEST_USER_NAME;
userInfo.usri1_password = MockWindowsAccount.TEST_PASSWORD;
userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
// ignore test if not able to add user (need to be administrator to do this).
Assumptions.assumeTrue(LMErr.NERR_Success == Netapi32.INSTANCE.NetUserAdd(null, 1, userInfo, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public IWindowsIdentity logonDomainUserEx(final String username, final String do
@Override
public IWindowsIdentity logonUser(final String username, final String password) {
final String currentUsername = Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
if (username.equals(currentUsername)) {
if (currentUsername.equals(username)) {
return new MockWindowsIdentity(currentUsername, this.groups);
} else if (username.equals(MockWindowsAuthProvider.GUEST)) {
} else if (MockWindowsAuthProvider.GUEST.equals(username)) {
return new MockWindowsIdentity(MockWindowsAuthProvider.GUEST, this.groups);
} else {
throw new RuntimeException("Mock error: " + username);
Expand Down
Loading

0 comments on commit 68dc619

Please sign in to comment.