ilgrosso commented on code in PR #1413: URL: https://github.com/apache/syncope/pull/1413#discussion_r3372531467
########## core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthenticationAttemptThrottler.java: ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.core.spring.security; + +import java.time.Duration; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.LongSupplier; +import org.apache.commons.lang3.StringUtils; + +public class AuthenticationAttemptThrottler { + + private static final class Attempts { Review Comment: use `record` ########## core/self-keymaster-starter/src/main/java/org/apache/syncope/core/keymaster/rest/security/SelfKeymasterUsernamePasswordAuthenticationProvider.java: ########## @@ -48,11 +49,13 @@ public SelfKeymasterUsernamePasswordAuthenticationProvider( public Authentication authenticate(final Authentication authentication) { if (keymasterProperties.getUsername().equals(authentication.getName())) { return finalizeAuthentication( - SyncopeAuthenticationDetails.class.cast(authentication.getDetails()).getDomain(), + ((SyncopeAuthenticationDetails) Objects.requireNonNull(authentication.getDetails())).getDomain(), Review Comment: Objects.requireNonNull on `authentication.getDetails()` or `authentication.getDetails()` is not needed. Please, remove. ########## core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthenticationAttemptThrottler.java: ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.core.spring.security; + +import java.time.Duration; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.LongSupplier; +import org.apache.commons.lang3.StringUtils; + +public class AuthenticationAttemptThrottler { + + private static final class Attempts { + + private final Deque<Long> failures = new ArrayDeque<>(); + + private long blockedUntil; + } + + private final SecurityProperties securityProperties; + + private final LongSupplier clock; + + private final Map<String, Attempts> attempts = new ConcurrentHashMap<>(); Review Comment: This class will just use memory to store the failure, resulting into fallacies in HA scenarios. Please use a JCache instance to store `attempts` rather than just a simple `Map` ########## core/spring/src/main/java/org/apache/syncope/core/spring/security/UsernamePasswordAuthenticationProvider.java: ########## @@ -96,23 +104,26 @@ public Authentication authenticate(final Authentication authentication) { } if (username.get() == null) { - username.setValue(authentication.getPrincipal().toString()); + username.setValue(authenticatingPrincipal); } return finalizeAuthentication( domainKey, + authenticatingPrincipal, Review Comment: why passing both `authenticatingPrincipal` and `username`? ########## fit/core-reference/src/test/java/org/apache/syncope/fit/core/AuthenticationITCase.java: ########## @@ -387,6 +388,38 @@ public void checkUserSuspension() { assertEquals(0, goodPwdClient.self().user().getFailedLogins().intValue()); } + @Test + public void authenticationThrottle() { + UserCR userCR = UserITCase.getUniqueSample("[email protected]"); + userCR.getRoles().add("User manager"); + + UserTO userTO = createUser(userCR).getEntity(); + assertNotNull(userTO); + + for (int i = 0; i < AUTHENTICATION_THROTTLE_MAX_ATTEMPTS - 1; i++) { + try { + CLIENT_FACTORY.create(userTO.getUsername(), "wrongpwd").self(); + fail("This should not happen"); + } catch (NotAuthorizedException e) { + assertNotNull(e); + } + } + + try { + CLIENT_FACTORY.create(userTO.getUsername(), "wrongpwd").self(); Review Comment: use `assertThrows()` ########## fit/core-reference/src/test/java/org/apache/syncope/fit/core/AuthenticationITCase.java: ########## @@ -387,6 +388,38 @@ public void checkUserSuspension() { assertEquals(0, goodPwdClient.self().user().getFailedLogins().intValue()); } + @Test + public void authenticationThrottle() { + UserCR userCR = UserITCase.getUniqueSample("[email protected]"); + userCR.getRoles().add("User manager"); + + UserTO userTO = createUser(userCR).getEntity(); + assertNotNull(userTO); + + for (int i = 0; i < AUTHENTICATION_THROTTLE_MAX_ATTEMPTS - 1; i++) { + try { + CLIENT_FACTORY.create(userTO.getUsername(), "wrongpwd").self(); Review Comment: use `assertDoesNotThrow()` ########## core/spring/src/main/java/org/apache/syncope/core/spring/security/UsernamePasswordAuthenticationProvider.java: ########## @@ -65,11 +68,16 @@ public UsernamePasswordAuthenticationProvider( this.provisioningManager = provisioningManager; this.securityProperties = securityProperties; this.encryptorManager = encryptorManager; + this.authenticationAttemptThrottler = new AuthenticationAttemptThrottler(securityProperties); } @Override public Authentication authenticate(final Authentication authentication) { - String domainKey = SyncopeAuthenticationDetails.class.cast(authentication.getDetails()).getDomain(); + String domainKey = ((SyncopeAuthenticationDetails) + Objects.requireNonNull(authentication.getDetails())).getDomain(); Review Comment: same here, `Objects.requireNotNull()` not needed, please revert ########## fit/core-reference/src/test/java/org/apache/syncope/fit/core/AuthenticationITCase.java: ########## @@ -387,6 +388,38 @@ public void checkUserSuspension() { assertEquals(0, goodPwdClient.self().user().getFailedLogins().intValue()); } + @Test + public void authenticationThrottle() { + UserCR userCR = UserITCase.getUniqueSample("[email protected]"); + userCR.getRoles().add("User manager"); + + UserTO userTO = createUser(userCR).getEntity(); + assertNotNull(userTO); + + for (int i = 0; i < AUTHENTICATION_THROTTLE_MAX_ATTEMPTS - 1; i++) { + try { + CLIENT_FACTORY.create(userTO.getUsername(), "wrongpwd").self(); + fail("This should not happen"); + } catch (NotAuthorizedException e) { + assertNotNull(e); + } + } + + try { + CLIENT_FACTORY.create(userTO.getUsername(), "wrongpwd").self(); + fail("This should not happen"); + } catch (WebApplicationException e) { + assertEquals(Response.Status.TOO_MANY_REQUESTS.getStatusCode(), e.getResponse().getStatus()); + } + + try { + CLIENT_FACTORY.create(userTO.getUsername(), "password123").self(); Review Comment: use `assertThrows()` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
