/support/cas-server-support-trusted-mfa-mongo/src/main/java/org/apereo/cas/trusted/authentication/storage/MongoDbMultifactorAuthenticationTrustStorage.java

https://github.com/frett/cas · Java · 82 lines · 67 code · 9 blank · 6 comment · 4 complexity · d939aa2c3086b2ee283106c153abd24b MD5 · raw file

  1. package org.apereo.cas.trusted.authentication.storage;
  2. import org.apereo.cas.trusted.authentication.api.MultifactorAuthenticationTrustRecord;
  3. import lombok.RequiredArgsConstructor;
  4. import lombok.extern.slf4j.Slf4j;
  5. import lombok.val;
  6. import org.springframework.data.mongodb.core.MongoOperations;
  7. import org.springframework.data.mongodb.core.query.Criteria;
  8. import org.springframework.data.mongodb.core.query.Query;
  9. import java.time.LocalDateTime;
  10. import java.util.HashSet;
  11. import java.util.Set;
  12. /**
  13. * This is {@link MongoDbMultifactorAuthenticationTrustStorage}.
  14. *
  15. * @author Misagh Moayyed
  16. * @since 5.0.0
  17. */
  18. @Slf4j
  19. @RequiredArgsConstructor
  20. public class MongoDbMultifactorAuthenticationTrustStorage extends BaseMultifactorAuthenticationTrustStorage {
  21. private final String collectionName;
  22. private final MongoOperations mongoTemplate;
  23. @Override
  24. public void expire(final String key) {
  25. try {
  26. val query = new Query();
  27. query.addCriteria(Criteria.where("recordKey").is(key));
  28. val res = this.mongoTemplate.remove(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
  29. LOGGER.info("Found and removed [{}]", res.getDeletedCount());
  30. } catch (final Exception e) {
  31. if (LOGGER.isDebugEnabled()) {
  32. LOGGER.debug(e.getMessage(), e);
  33. } else {
  34. LOGGER.info("No trusted authentication records could be found");
  35. }
  36. }
  37. }
  38. @Override
  39. public void expire(final LocalDateTime onOrBefore) {
  40. try {
  41. val query = new Query();
  42. query.addCriteria(Criteria.where("recordDate").lte(onOrBefore));
  43. val res = this.mongoTemplate.remove(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
  44. LOGGER.info("Found and removed [{}]", res.getDeletedCount());
  45. } catch (final Exception e) {
  46. if (LOGGER.isDebugEnabled()) {
  47. LOGGER.debug(e.getMessage(), e);
  48. } else {
  49. LOGGER.info("No trusted authentication records could be found");
  50. }
  51. }
  52. }
  53. @Override
  54. public Set<? extends MultifactorAuthenticationTrustRecord> get(final LocalDateTime onOrAfterDate) {
  55. val query = new Query();
  56. query.addCriteria(Criteria.where("recordDate").gte(onOrAfterDate));
  57. val results = mongoTemplate.find(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
  58. return new HashSet<>(results);
  59. }
  60. @Override
  61. public Set<? extends MultifactorAuthenticationTrustRecord> get(final String principal) {
  62. val query = new Query();
  63. query.addCriteria(Criteria.where("principal").is(principal));
  64. val results =
  65. this.mongoTemplate.find(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
  66. return new HashSet<>(results);
  67. }
  68. @Override
  69. protected MultifactorAuthenticationTrustRecord setInternal(final MultifactorAuthenticationTrustRecord record) {
  70. this.mongoTemplate.save(record, this.collectionName);
  71. return record;
  72. }
  73. }