Skip to content

Commit a2e9db2

Browse files
committed
AuthenticationFailureListener: persist date of event instead of current date.
Fix #186
1 parent 57f0706 commit a2e9db2

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

src/main/java/ru/mystamps/web/service/SiteService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Date;
21+
2022
import ru.mystamps.web.entity.User;
2123

2224
public interface SiteService {
@@ -34,6 +36,7 @@ void logAboutFailedAuthentication(
3436
User user,
3537
String ip,
3638
String referer,
37-
String agent
39+
String agent,
40+
Date date
3841
);
3942
}

src/main/java/ru/mystamps/web/service/SiteServiceImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void logAboutAbsentPage(
5353
String referer,
5454
String agent) {
5555

56-
logEvent(PAGE_NOT_FOUND, page, user, ip, referer, agent);
56+
logEvent(PAGE_NOT_FOUND, page, user, ip, referer, agent, new Date());
5757
}
5858

5959
@Override
@@ -64,9 +64,10 @@ public void logAboutFailedAuthentication(
6464
User user,
6565
String ip,
6666
String referer,
67-
String agent) {
67+
String agent,
68+
Date date) {
6869

69-
logEvent(AUTHENTICATION_FAILED, page, user, ip, referer, agent);
70+
logEvent(AUTHENTICATION_FAILED, page, user, ip, referer, agent, date);
7071
}
7172

7273
@SuppressWarnings("PMD.UseObjectForClearerAPI")
@@ -76,7 +77,8 @@ private void logEvent(
7677
User user,
7778
String ip,
7879
String referer,
79-
String agent) {
80+
String agent,
81+
Date date) {
8082

8183
Validate.isTrue(type != null, "Type of suspicious activity was not set");
8284
Validate.isTrue(page != null, "Page should be non null");
@@ -88,7 +90,7 @@ private void logEvent(
8890
activityType.setName(type);
8991
activity.setType(activityType);
9092

91-
activity.setOccurredAt(new Date());
93+
activity.setOccurredAt(date == null ? new Date() : date);
9294
activity.setPage(page);
9395

9496
activity.setUser(user);

src/main/java/ru/mystamps/web/support/spring/security/AuthenticationFailureListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.support.spring.security;
1919

20+
import java.util.Date;
21+
2022
import javax.inject.Inject;
2123
import javax.servlet.http.HttpServletRequest;
2224

@@ -52,8 +54,9 @@ public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
5254
String ip = request.getRemoteAddr();
5355
String referer = request.getHeader("referer");
5456
String agent = request.getHeader("user-agent");
57+
Date date = new Date(event.getTimestamp());
5558

56-
siteService.logAboutFailedAuthentication(page, null, ip, referer, agent);
59+
siteService.logAboutFailedAuthentication(page, null, ip, referer, agent, date);
5760
}
5861

5962
private HttpServletRequest getRequest() {

src/test/groovy/ru/mystamps/web/service/SiteServiceImplTest.groovy

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class SiteServiceImplTest extends Specification {
176176

177177
def "logAboutFailedAuthentication() should call dao"() {
178178
when:
179-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
179+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
180180
then:
181181
1 * suspiciousActivityDao.add(_ as SuspiciousActivity)
182182
}
@@ -185,17 +185,29 @@ class SiteServiceImplTest extends Specification {
185185
given:
186186
SuspiciousActivityType expectedType = TestObjects.createAuthFailedActivityType()
187187
when:
188-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
188+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
189189
then:
190190
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
191191
assert activity?.type?.name == expectedType.name
192192
return true
193193
})
194194
}
195195

196-
def "logAboutFailedAuthentication() should assign occurred at to current date"() {
196+
def "logAboutFailedAuthentication() should assign occurred at to current date when date was provided"() {
197+
given:
198+
Date expectedDate = new Date() - 100;
199+
when:
200+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, expectedDate)
201+
then:
202+
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
203+
assert DateUtils.roughlyEqual(activity?.occurredAt, expectedDate)
204+
return true
205+
})
206+
}
207+
208+
def "logAboutFailedAuthentication() should assign occurred at to current date when date wasn't provided"() {
197209
when:
198-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
210+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
199211
then:
200212
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
201213
assert DateUtils.roughlyEqual(activity?.occurredAt, new Date())
@@ -205,14 +217,14 @@ class SiteServiceImplTest extends Specification {
205217

206218
def "logAboutFailedAuthentication() should throw exception when page is null"() {
207219
when:
208-
service.logAboutFailedAuthentication(null, null, null, null, null)
220+
service.logAboutFailedAuthentication(null, null, null, null, null, null)
209221
then:
210222
thrown IllegalArgumentException
211223
}
212224

213225
def "logAboutFailedAuthentication() should pass page to dao"() {
214226
when:
215-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
227+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
216228
then:
217229
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
218230
assert activity?.page == TEST_PAGE
@@ -222,7 +234,7 @@ class SiteServiceImplTest extends Specification {
222234

223235
def "logAboutFailedAuthentication() should pass null to dao for unknown user"() {
224236
when:
225-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
237+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
226238
then:
227239
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
228240
assert activity?.user == null
@@ -234,7 +246,7 @@ class SiteServiceImplTest extends Specification {
234246
given:
235247
User user = TestObjects.createUser()
236248
when:
237-
service.logAboutFailedAuthentication(TEST_PAGE, user, null, null, null)
249+
service.logAboutFailedAuthentication(TEST_PAGE, user, null, null, null, null)
238250
then:
239251
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
240252
assert activity?.user == user
@@ -244,7 +256,7 @@ class SiteServiceImplTest extends Specification {
244256

245257
def "logAboutFailedAuthentication() should pass ip to dao"() {
246258
when:
247-
service.logAboutFailedAuthentication(TEST_PAGE, null, TEST_IP, null, null)
259+
service.logAboutFailedAuthentication(TEST_PAGE, null, TEST_IP, null, null, null)
248260
then:
249261
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
250262
assert activity?.ip == TEST_IP
@@ -254,7 +266,7 @@ class SiteServiceImplTest extends Specification {
254266

255267
def "logAboutFailedAuthentication() should pass empty string to dao for unknown ip"() {
256268
when:
257-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
269+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
258270
then:
259271
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
260272
assert activity?.ip?.empty
@@ -264,7 +276,7 @@ class SiteServiceImplTest extends Specification {
264276

265277
def "logAboutFailedAuthentication() should pass referer to dao"() {
266278
when:
267-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, TEST_REFERER_PAGE, null)
279+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, TEST_REFERER_PAGE, null, null)
268280
then:
269281
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
270282
assert activity?.refererPage == TEST_REFERER_PAGE
@@ -274,7 +286,7 @@ class SiteServiceImplTest extends Specification {
274286

275287
def "logAboutFailedAuthentication() should pass empty string to dao for unknown referer"() {
276288
when:
277-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
289+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
278290
then:
279291
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
280292
assert activity?.refererPage?.empty
@@ -284,7 +296,7 @@ class SiteServiceImplTest extends Specification {
284296

285297
def "logAboutFailedAuthentication() should pass user agent to dao"() {
286298
when:
287-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, TEST_USER_AGENT)
299+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, TEST_USER_AGENT, null)
288300
then:
289301
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
290302
assert activity?.userAgent == TEST_USER_AGENT
@@ -294,7 +306,7 @@ class SiteServiceImplTest extends Specification {
294306

295307
def "logAboutFailedAuthentication() should pass empty string to dao for unknown user agent"() {
296308
when:
297-
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null)
309+
service.logAboutFailedAuthentication(TEST_PAGE, null, null, null, null, null)
298310
then:
299311
1 * suspiciousActivityDao.add({ SuspiciousActivity activity ->
300312
assert activity?.userAgent?.empty

0 commit comments

Comments
 (0)