/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
- package org.apereo.cas.trusted.authentication.storage;
- import org.apereo.cas.trusted.authentication.api.MultifactorAuthenticationTrustRecord;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import lombok.val;
- import org.springframework.data.mongodb.core.MongoOperations;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
- import java.time.LocalDateTime;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * This is {@link MongoDbMultifactorAuthenticationTrustStorage}.
- *
- * @author Misagh Moayyed
- * @since 5.0.0
- */
- @Slf4j
- @RequiredArgsConstructor
- public class MongoDbMultifactorAuthenticationTrustStorage extends BaseMultifactorAuthenticationTrustStorage {
- private final String collectionName;
- private final MongoOperations mongoTemplate;
- @Override
- public void expire(final String key) {
- try {
- val query = new Query();
- query.addCriteria(Criteria.where("recordKey").is(key));
- val res = this.mongoTemplate.remove(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
- LOGGER.info("Found and removed [{}]", res.getDeletedCount());
- } catch (final Exception e) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(e.getMessage(), e);
- } else {
- LOGGER.info("No trusted authentication records could be found");
- }
- }
- }
- @Override
- public void expire(final LocalDateTime onOrBefore) {
- try {
- val query = new Query();
- query.addCriteria(Criteria.where("recordDate").lte(onOrBefore));
- val res = this.mongoTemplate.remove(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
- LOGGER.info("Found and removed [{}]", res.getDeletedCount());
- } catch (final Exception e) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(e.getMessage(), e);
- } else {
- LOGGER.info("No trusted authentication records could be found");
- }
- }
- }
- @Override
- public Set<? extends MultifactorAuthenticationTrustRecord> get(final LocalDateTime onOrAfterDate) {
- val query = new Query();
- query.addCriteria(Criteria.where("recordDate").gte(onOrAfterDate));
- val results = mongoTemplate.find(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
- return new HashSet<>(results);
- }
- @Override
- public Set<? extends MultifactorAuthenticationTrustRecord> get(final String principal) {
- val query = new Query();
- query.addCriteria(Criteria.where("principal").is(principal));
- val results =
- this.mongoTemplate.find(query, MultifactorAuthenticationTrustRecord.class, this.collectionName);
- return new HashSet<>(results);
- }
- @Override
- protected MultifactorAuthenticationTrustRecord setInternal(final MultifactorAuthenticationTrustRecord record) {
- this.mongoTemplate.save(record, this.collectionName);
- return record;
- }
- }