Search in sources :

Example 6 with CredentialRegistration

use of com.yubico.data.CredentialRegistration in project cas by apereo.

the class MongoDbWebAuthnCredentialRepository method getRegistrationsByUsername.

@Override
public Collection<CredentialRegistration> getRegistrationsByUsername(final String username) {
    val query = new Query().addCriteria(Criteria.where(MongoDbWebAuthnCredentialRegistration.FIELD_USERNAME).is(username)).collation(Collation.of(Locale.ENGLISH).strength(Collation.ComparisonLevel.primary()));
    val records = mongoTemplate.find(query, MongoDbWebAuthnCredentialRegistration.class, getProperties().getAuthn().getMfa().getWebAuthn().getMongo().getCollection());
    return records.stream().map(record -> getCipherExecutor().decode(record.getRecords())).map(Unchecked.function(record -> WebAuthnUtils.getObjectMapper().readValue(record, new TypeReference<Set<CredentialRegistration>>() {
    }))).flatMap(Collection::stream).collect(Collectors.toList());
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Unchecked(org.jooq.lambda.Unchecked) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) Collection(java.util.Collection) lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) Set(java.util.Set) BaseWebAuthnCredentialRepository(org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository) Collation(org.springframework.data.mongodb.core.query.Collation) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Locale(java.util.Locale) MongoOperations(org.springframework.data.mongodb.core.MongoOperations) Update(org.springframework.data.mongodb.core.query.Update) Clock(java.time.Clock) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Set(java.util.Set) Query(org.springframework.data.mongodb.core.query.Query)

Example 7 with CredentialRegistration

use of com.yubico.data.CredentialRegistration in project cas by apereo.

the class JpaWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
public void update(final String username, final Collection<CredentialRegistration> givenRecords) {
    val records = givenRecords.stream().map(record -> {
        if (record.getRegistrationTime() == null) {
            return record.withRegistrationTime(Instant.now(Clock.systemUTC()));
        }
        return record;
    }).collect(Collectors.toList());
    val jsonRecords = getCipherExecutor().encode(WebAuthnUtils.getObjectMapper().writeValueAsString(records));
    new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(final TransactionStatus status) {
            val count = entityManager.createQuery(UPDATE_QUERY.concat("SET r.records=:records WHERE r.username = :username")).setParameter("username", username.trim().toLowerCase()).setParameter("records", jsonRecords).executeUpdate();
            if (count == 0) {
                val record = JpaWebAuthnCredentialRegistration.builder().username(username.trim().toLowerCase()).records(jsonRecords).build();
                entityManager.merge(record);
            }
        }
    });
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Unchecked(org.jooq.lambda.Unchecked) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) Collection(java.util.Collection) lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) Set(java.util.Set) BaseWebAuthnCredentialRepository(org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Instant(java.time.Instant) EnableTransactionManagement(org.springframework.transaction.annotation.EnableTransactionManagement) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Propagation(org.springframework.transaction.annotation.Propagation) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Clock(java.time.Clock) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Transactional(org.springframework.transaction.annotation.Transactional) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) SneakyThrows(lombok.SneakyThrows)

Example 8 with CredentialRegistration

use of com.yubico.data.CredentialRegistration in project cas by apereo.

the class RedisWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
protected void update(final String username, final Collection<CredentialRegistration> givenRecords) {
    val redisKey = buildRedisKeyForRecord(username);
    if (givenRecords.isEmpty()) {
        redisTemplate.delete(redisKey);
    } else {
        val records = givenRecords.stream().map(record -> {
            if (record.getRegistrationTime() == null) {
                return record.withRegistrationTime(Instant.now(Clock.systemUTC()));
            }
            return record;
        }).collect(Collectors.toList());
        val jsonRecords = getCipherExecutor().encode(WebAuthnUtils.getObjectMapper().writeValueAsString(records));
        val entry = RedisWebAuthnCredentialRegistration.builder().records(jsonRecords).username(username.trim().toLowerCase()).build();
        redisTemplate.boundValueOps(redisKey).set(entry);
    }
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Unchecked(org.jooq.lambda.Unchecked) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) Collection(java.util.Collection) lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) Set(java.util.Set) BaseWebAuthnCredentialRepository(org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Stream(java.util.stream.Stream) Clock(java.time.Clock) CasRedisTemplate(org.apereo.cas.redis.core.CasRedisTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SneakyThrows(lombok.SneakyThrows)

Aggregations

CredentialRegistration (com.yubico.data.CredentialRegistration)8 lombok.val (lombok.val)8 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 Collection (java.util.Collection)7 Clock (java.time.Clock)6 Instant (java.time.Instant)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 SneakyThrows (lombok.SneakyThrows)6 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)6 CipherExecutor (org.apereo.cas.util.crypto.CipherExecutor)6 BaseWebAuthnCredentialRepository (org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository)5 Unchecked (org.jooq.lambda.Unchecked)5 Slf4j (lombok.extern.slf4j.Slf4j)4 Query (org.springframework.data.mongodb.core.query.Query)3 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2 Objects (java.util.Objects)2 MongoOperations (org.springframework.data.mongodb.core.MongoOperations)2