This commit is contained in:
parent
292825d1a8
commit
3ddacfc771
@ -6,8 +6,6 @@ import com.auth0.jwt.exceptions.JWTCreationException;
|
|||||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
import com.auth0.jwt.interfaces.JWTVerifier;
|
import com.auth0.jwt.interfaces.JWTVerifier;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.primefactorsolutions.model.Employee;
|
import com.primefactorsolutions.model.Employee;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -16,14 +14,12 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AccountService {
|
public class AccountService {
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
private final String secret;
|
private final String secret;
|
||||||
|
|
||||||
public AccountService(final EmailService emailService, final EmployeeService employeeService,
|
public AccountService(final EmailService emailService, final EmployeeService employeeService,
|
||||||
@ -56,14 +52,16 @@ public class AccountService {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
decodedJWT = verifier.verify(token);
|
decodedJWT = verifier.verify(token);
|
||||||
final Map<String, ?> payload = (Map<String, ?>) objectMapper.readValue(decodedJWT.getPayload(), Map.class);
|
final Instant expiry = decodedJWT.getExpiresAtAsInstant();
|
||||||
|
final String claim = decodedJWT.getClaim("username").asString();
|
||||||
|
|
||||||
if (Instant.parse((String) payload.get("expire")).isBefore(Instant.now())
|
if (expiry.isBefore(Instant.now())
|
||||||
|| !username.equals(payload.get("username"))) {
|
|| !username.equals(claim)) {
|
||||||
log.warn("token invalid {} {} {}", username, payload.get("username"), payload.get("expire"));
|
log.warn("token invalid {} {} {}", username, claim, expiry);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (JWTVerificationException | JsonProcessingException e) {
|
} catch (JWTVerificationException e) {
|
||||||
|
log.warn("error updating password", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +77,8 @@ public class AccountService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
employeeService.updatePassword(employee, newPassword);
|
employeeService.updatePassword(employee, newPassword);
|
||||||
|
|
||||||
|
log.info("updated password for {}", username);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createResetPasswordLink(final String username) {
|
private String createResetPasswordLink(final String username) {
|
||||||
@ -88,10 +88,10 @@ public class AccountService {
|
|||||||
Algorithm algorithm = Algorithm.HMAC512(secret);
|
Algorithm algorithm = Algorithm.HMAC512(secret);
|
||||||
token = JWT.create()
|
token = JWT.create()
|
||||||
.withIssuer("pfs")
|
.withIssuer("pfs")
|
||||||
.withPayload(objectMapper.writeValueAsString(Map.of("username", username,
|
.withClaim("username", username)
|
||||||
"expire", Instant.now().plus(1, ChronoUnit.HOURS).toString())))
|
.withExpiresAt(Instant.now().plus(1, ChronoUnit.HOURS))
|
||||||
.sign(algorithm);
|
.sign(algorithm);
|
||||||
} catch (JWTCreationException | JsonProcessingException e) {
|
} catch (JWTCreationException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ import java.util.Collections;
|
|||||||
@Service
|
@Service
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class EmployeeService {
|
public class EmployeeService {
|
||||||
private static final String USERPASSWORD = "userpassword";
|
private static final String USERPASSWORD = "userPassword";
|
||||||
private static final String OBJECTCLASS = "objectclass";
|
private static final String OBJECTCLASS = "objectclass";
|
||||||
private static final String ORGANIZATIONAL_PERSON = "organizationalPerson";
|
private static final String ORGANIZATIONAL_PERSON = "organizationalPerson";
|
||||||
private static final String INET_ORG_PERSON = "inetOrgPerson";
|
private static final String INET_ORG_PERSON = "inetOrgPerson";
|
||||||
|
Loading…
Reference in New Issue
Block a user