|
|
|
@ -20,6 +20,7 @@ import io.swagger.annotations.ApiOperation; |
|
|
|
import io.swagger.annotations.ApiParam; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
@ -41,11 +42,13 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
|
import org.thingsboard.server.common.data.security.UserCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent; |
|
|
|
import org.thingsboard.server.common.data.security.event.UserSessionInvalidationEvent; |
|
|
|
import org.thingsboard.server.common.data.security.model.SecuritySettings; |
|
|
|
import org.thingsboard.server.common.data.security.model.UserPasswordPolicy; |
|
|
|
import org.thingsboard.server.common.msg.tools.TbRateLimits; |
|
|
|
import org.thingsboard.server.dao.audit.AuditLogService; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.security.auth.rest.RestAuthenticationDetails; |
|
|
|
@ -62,6 +65,8 @@ import org.thingsboard.server.service.security.system.SystemSecurityService; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@TbCoreComponent |
|
|
|
@ -69,6 +74,10 @@ import java.net.URISyntaxException; |
|
|
|
@Slf4j |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class AuthController extends BaseController { |
|
|
|
|
|
|
|
@Value("${server.rest.rate_limits.reset_password_per_user:5:3600}") |
|
|
|
private String defaultLimitsConfiguration; |
|
|
|
private final ConcurrentMap<UserId, TbRateLimits> resetPasswordRateLimits = new ConcurrentHashMap<>(); |
|
|
|
private final BCryptPasswordEncoder passwordEncoder; |
|
|
|
private final JwtTokenFactory tokenFactory; |
|
|
|
private final MailService mailService; |
|
|
|
@ -211,7 +220,12 @@ public class AuthController extends BaseController { |
|
|
|
HttpStatus responseStatus; |
|
|
|
String resetURI = "/login/resetPassword"; |
|
|
|
UserCredentials userCredentials = userService.findUserCredentialsByResetToken(TenantId.SYS_TENANT_ID, resetToken); |
|
|
|
|
|
|
|
if (userCredentials != null) { |
|
|
|
TbRateLimits tbRateLimits = getTbRateLimits(userCredentials.getUserId()); |
|
|
|
if (!tbRateLimits.tryConsume()) { |
|
|
|
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build(); |
|
|
|
} |
|
|
|
try { |
|
|
|
URI location = new URI(resetURI + "?resetToken=" + resetToken); |
|
|
|
headers.setLocation(location); |
|
|
|
@ -323,4 +337,9 @@ public class AuthController extends BaseController { |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private TbRateLimits getTbRateLimits(UserId userId) { |
|
|
|
return resetPasswordRateLimits.computeIfAbsent(userId, |
|
|
|
key -> new TbRateLimits(defaultLimitsConfiguration, true)); |
|
|
|
} |
|
|
|
} |
|
|
|
|