Skip to content

Commit 617f373

Browse files
Splitting User info from Client Supplier
Breaking the user-specific information out of the ClientInfoSupplier.
1 parent f4921e3 commit 617f373

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* OWASP Enterprise Security API (ESAPI)
3+
*
4+
* This file is part of the Open Web Application Security Project (OWASP)
5+
* Enterprise Security API (ESAPI) project. For details, please see
6+
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
7+
*
8+
* Copyright (c) 2007 - The OWASP Foundation
9+
*
10+
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
11+
* LICENSE before you use, modify, and/or redistribute this software.
12+
*
13+
* @created 2019
14+
*/
15+
16+
package org.owasp.esapi.logging.appender;
17+
18+
import java.util.function.Supplier;
19+
20+
import org.owasp.esapi.ESAPI;
21+
import org.owasp.esapi.User;
22+
23+
/**
24+
* Supplier which can provide a String representing the client-side connection
25+
* information.
26+
*/
27+
public class UserInfoSupplier implements Supplier<String> {
28+
/** Default UserName string if the Authenticated user is null.*/
29+
private static final String DEFAULT_USERNAME = "#ANONYMOUS#";
30+
31+
/** Whether to log the user info from this instance. */
32+
private boolean logUserInfo = true;
33+
34+
@Override
35+
public String get() {
36+
// log user information - username:session@ipaddr
37+
User user = ESAPI.authenticator().getCurrentUser();
38+
39+
String userInfo = "";
40+
if (logUserInfo) {
41+
if (user == null) {
42+
userInfo = DEFAULT_USERNAME;
43+
} else {
44+
userInfo = user.getAccountName();
45+
}
46+
}
47+
48+
return userInfo;
49+
}
50+
51+
/**
52+
* Specify whether the instance should record the client info.
53+
*
54+
* @param log {@code true} to record
55+
*/
56+
public void setLogUserInfo(boolean log) {
57+
this.logUserInfo = log;
58+
}
59+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.owasp.esapi.logging.appender;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.times;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
10+
import static org.powermock.api.mockito.PowerMockito.when;
11+
12+
import javax.servlet.http.HttpServletRequest;
13+
import javax.servlet.http.HttpSession;
14+
15+
import org.junit.Before;
16+
import org.junit.Rule;
17+
import org.junit.Test;
18+
import org.junit.rules.TestName;
19+
import org.junit.runner.RunWith;
20+
import org.mockito.ArgumentMatchers;
21+
import org.owasp.esapi.Authenticator;
22+
import org.owasp.esapi.ESAPI;
23+
import org.owasp.esapi.Randomizer;
24+
import org.owasp.esapi.User;
25+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
26+
import org.powermock.core.classloader.annotations.PrepareForTest;
27+
import org.powermock.modules.junit4.PowerMockRunner;
28+
29+
@RunWith(PowerMockRunner.class)
30+
@PrepareForTest({ESAPI.class})
31+
@PowerMockIgnore("javax.security.*") //Required since User extends javax.security.Principal
32+
public class UserInfoSupplierTest {
33+
private static final String ESAPI_SESSION_ATTR = "ESAPI_SESSION";
34+
35+
@Rule
36+
public TestName testName = new TestName();
37+
38+
private Authenticator mockAuth;
39+
private User mockUser;
40+
41+
@Before
42+
public void before() throws Exception {
43+
mockAuth =mock(Authenticator.class);
44+
mockUser =mock(User.class);
45+
46+
mockStatic(ESAPI.class);
47+
when(ESAPI.class, "authenticator").thenReturn(mockAuth);
48+
49+
when(mockUser.getAccountName()).thenReturn(testName.getMethodName() + "-USER");
50+
51+
52+
when(mockAuth.getCurrentUser()).thenReturn(mockUser);
53+
}
54+
55+
@Test
56+
public void testHappyPath() throws Exception {
57+
UserInfoSupplier uis = new UserInfoSupplier();
58+
uis.setLogUserInfo(true);
59+
String result = uis.get();
60+
61+
assertEquals(testName.getMethodName() + "-USER", result);
62+
63+
verify(mockAuth,times(1)).getCurrentUser();
64+
verify(mockUser,times(1)).getAccountName();
65+
66+
verifyNoMoreInteractions(mockAuth, mockUser);
67+
}
68+
69+
@Test
70+
public void testLogUserOff() {
71+
UserInfoSupplier uis = new UserInfoSupplier();
72+
uis.setLogUserInfo(false);
73+
String result = uis.get();
74+
75+
assertTrue(result.isEmpty());
76+
verify(mockAuth,times(1)).getCurrentUser();
77+
78+
verifyNoMoreInteractions(mockAuth, mockUser);
79+
}
80+
81+
@Test
82+
public void testLogUserNull() {
83+
when(mockAuth.getCurrentUser()).thenReturn(null);
84+
UserInfoSupplier uis = new UserInfoSupplier();
85+
uis.setLogUserInfo(true);
86+
String result = uis.get();
87+
88+
assertEquals("#ANONYMOUS#", result);
89+
90+
verify(mockAuth,times(1)).getCurrentUser();
91+
92+
verifyNoMoreInteractions(mockAuth, mockUser);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)