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);
}
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);
}
}
}
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)));
}
}
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);
}
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;
}
Aggregations