Search in sources :

Example 1 with CredentialRegistration

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

the class MongoDbWebAuthnCredentialRepository method stream.

@Override
public Stream<CredentialRegistration> stream() {
    val query = new Query().addCriteria(Criteria.where(MongoDbWebAuthnCredentialRegistration.FIELD_USERNAME).exists(true)).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);
}
Also used : lombok.val(lombok.val) Query(org.springframework.data.mongodb.core.query.Query) CredentialRegistration(com.yubico.data.CredentialRegistration) Collection(java.util.Collection) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 2 with CredentialRegistration

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

the class MongoDbWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
protected 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 query = new Query(Criteria.where(MongoDbWebAuthnCredentialRegistration.FIELD_USERNAME).is(username)).collation(Collation.of(Locale.ENGLISH).strength(Collation.ComparisonLevel.primary()));
    val collection = getProperties().getAuthn().getMfa().getWebAuthn().getMongo().getCollection();
    if (records.isEmpty()) {
        LOGGER.debug("No records are provided for [{}] so entry will be removed", username);
        mongoTemplate.remove(query, MongoDbWebAuthnCredentialRegistration.class, collection);
    } else {
        val jsonRecords = getCipherExecutor().encode(WebAuthnUtils.getObjectMapper().writeValueAsString(records));
        val entry = MongoDbWebAuthnCredentialRegistration.builder().records(jsonRecords).username(username).build();
        val update = Update.update(MongoDbWebAuthnCredentialRegistration.FIELD_RECORDS, jsonRecords);
        val result = mongoTemplate.updateFirst(query, update, collection);
        if (result.getMatchedCount() <= 0) {
            LOGGER.debug("Storing new registration record for [{}]", username);
            mongoTemplate.save(entry, collection);
        }
    }
}
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) Query(org.springframework.data.mongodb.core.query.Query) SneakyThrows(lombok.SneakyThrows)

Example 3 with CredentialRegistration

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

the class LdapWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
protected void update(final String username, final Collection<CredentialRegistration> givenRecords) {
    if (givenRecords.isEmpty()) {
        LOGGER.debug("No records are provided for [{}] so entry will be removed", username);
        executeModifyOperation(new HashSet<>(0), Optional.ofNullable(locateLdapEntryFor(username)));
    } else {
        val records = givenRecords.stream().map(record -> {
            if (record.getRegistrationTime() == null) {
                return record.withRegistrationTime(Instant.now(Clock.systemUTC()));
            }
            return record;
        }).collect(Collectors.toList());
        val results = records.stream().map(Unchecked.function(reg -> WebAuthnUtils.getObjectMapper().writeValueAsString(records))).map(reg -> getCipherExecutor().encode(reg)).collect(Collectors.toSet());
        executeModifyOperation(results, Optional.ofNullable(locateLdapEntryFor(username)));
    }
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) ConnectionFactory(org.ldaptive.ConnectionFactory) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) CredentialRegistration(com.yubico.data.CredentialRegistration) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CollectionUtils(org.apereo.cas.util.CollectionUtils) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Unchecked(org.jooq.lambda.Unchecked) Collection(java.util.Collection) lombok.val(lombok.val) Set(java.util.Set) BaseWebAuthnCredentialRepository(org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) DisposableBean(org.springframework.beans.factory.DisposableBean) LdapEntry(org.ldaptive.LdapEntry) Clock(java.time.Clock) Optional(java.util.Optional) LdapUtils(org.apereo.cas.util.LdapUtils) LdapException(org.ldaptive.LdapException) SneakyThrows(lombok.SneakyThrows)

Example 4 with CredentialRegistration

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

the class JsonResourceWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
protected void update(final String username, final Collection<CredentialRegistration> givenRecords) {
    val storage = readFromJsonRepository();
    val records = givenRecords.stream().map(record -> {
        if (record.getRegistrationTime() == null) {
            return record.withRegistrationTime(Instant.now(Clock.systemUTC()));
        }
        return record;
    }).collect(Collectors.toList());
    storage.put(username.trim().toLowerCase(), new LinkedHashSet<>(records));
    WebAuthnUtils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(location.getFile(), storage);
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) Collection(java.util.Collection) lombok.val(lombok.val) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CredentialRegistration(com.yubico.data.CredentialRegistration) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) InitializingBean(org.springframework.beans.factory.InitializingBean) HashSet(java.util.HashSet) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Map(java.util.Map) Clock(java.time.Clock) TypeReference(com.fasterxml.jackson.core.type.TypeReference) WebAuthnUtils(org.apereo.cas.webauthn.WebAuthnUtils) LinkedHashSet(java.util.LinkedHashSet) Resource(org.springframework.core.io.Resource) SneakyThrows(lombok.SneakyThrows)

Example 5 with CredentialRegistration

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

the class WebAuthnRegisteredDevicesEndpoint method importAccount.

/**
 * Import account.
 *
 * @param request the request
 * @return the http status
 * @throws Exception the exception
 */
@Operation(summary = "Import a device registration as a JSON document")
@PostMapping(path = "/import", consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpStatus importAccount(final HttpServletRequest request) throws Exception {
    val requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    LOGGER.trace("Submitted account: [{}]", requestBody);
    val account = WebAuthnUtils.getObjectMapper().readValue(requestBody, new TypeReference<CredentialRegistration>() {
    });
    LOGGER.trace("Storing account: [{}]", account);
    registrationStorage.getObject().addRegistrationByUsername(account.getUsername(), account);
    return HttpStatus.CREATED;
}
Also used : lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) PostMapping(org.springframework.web.bind.annotation.PostMapping) Operation(io.swagger.v3.oas.annotations.Operation)

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