Search in sources :

Example 1 with YubiKeyAccount

use of org.apereo.cas.adaptors.yubikey.YubiKeyAccount in project cas by apereo.

the class MongoDbYubiKeyAccountRegistry method getAccount.

@Override
public Optional<YubiKeyAccount> getAccount(final String uid) {
    final Query query = new Query();
    query.addCriteria(Criteria.where("username").is(uid));
    final YubiKeyAccount account = this.mongoTemplate.findOne(query, YubiKeyAccount.class, this.collectionName);
    if (account != null) {
        return Optional.of(new YubiKeyAccount(account.getId(), getCipherExecutor().decode(account.getPublicId()), account.getUsername()));
    }
    return Optional.empty();
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount)

Example 2 with YubiKeyAccount

use of org.apereo.cas.adaptors.yubikey.YubiKeyAccount in project cas by apereo.

the class YubiKeyAccountRegistryEndpoint method export.

/**
 * Export.
 *
 * @return the response entity
 */
@GetMapping(path = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
@Operation(summary = "Export all Yubikey accounts as a zip file")
public ResponseEntity<Resource> export() {
    val accounts = registry.getObject().getAccounts();
    val resource = CompressionUtils.toZipFile(accounts.stream(), Unchecked.function(entry -> {
        val acct = (YubiKeyAccount) entry;
        val fileName = String.format("%s-%s", acct.getUsername(), acct.getId());
        val sourceFile = File.createTempFile(fileName, ".json");
        MAPPER.writeValue(sourceFile, acct);
        return sourceFile;
    }), "yubikeybaccts");
    val headers = new HttpHeaders();
    headers.setContentDisposition(ContentDisposition.attachment().filename(Objects.requireNonNull(resource.getFilename())).build());
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) PathVariable(org.springframework.web.bind.annotation.PathVariable) Operation(io.swagger.v3.oas.annotations.Operation) ObjectProvider(org.springframework.beans.factory.ObjectProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentDisposition(org.springframework.http.ContentDisposition) GetMapping(org.springframework.web.bind.annotation.GetMapping) YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Resource(org.springframework.core.io.Resource) YubiKeyAccountRegistry(org.apereo.cas.adaptors.yubikey.YubiKeyAccountRegistry) PostMapping(org.springframework.web.bind.annotation.PostMapping) Unchecked(org.jooq.lambda.Unchecked) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) CompressionUtils(org.apereo.cas.util.CompressionUtils) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BaseCasActuatorEndpoint(org.apereo.cas.web.BaseCasActuatorEndpoint) Parameter(io.swagger.v3.oas.annotations.Parameter) RestControllerEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) JacksonObjectMapperFactory(org.apereo.cas.util.serialization.JacksonObjectMapperFactory) ResponseEntity(org.springframework.http.ResponseEntity) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) Operation(io.swagger.v3.oas.annotations.Operation) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with YubiKeyAccount

use of org.apereo.cas.adaptors.yubikey.YubiKeyAccount in project cas by apereo.

the class YubiKeyAccountRegistryEndpoint method importAccount.

/**
 * Import account.
 *
 * @param request the request
 * @return the http status
 * @throws Exception the exception
 */
@Operation(summary = "Import a Yubikey account 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 = MAPPER.readValue(requestBody, new TypeReference<YubiKeyAccount>() {
    });
    LOGGER.trace("Storing account: [{}]", account);
    registry.getObject().save(account);
    return HttpStatus.CREATED;
}
Also used : lombok.val(lombok.val) YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount) PostMapping(org.springframework.web.bind.annotation.PostMapping) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with YubiKeyAccount

use of org.apereo.cas.adaptors.yubikey.YubiKeyAccount in project cas by apereo.

the class JpaYubiKeyAccountRegistry method registerAccountFor.

@Override
public boolean registerAccountFor(final String uid, final String token) {
    if (getAccountValidator().isValid(uid, token)) {
        final String yubikeyPublicId = getAccountValidator().getTokenPublicId(token);
        final YubiKeyAccount account = new YubiKeyAccount();
        account.setPublicId(getCipherExecutor().encode(yubikeyPublicId));
        account.setUsername(uid);
        return this.entityManager.merge(account) != null;
    }
    return false;
}
Also used : YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount)

Example 5 with YubiKeyAccount

use of org.apereo.cas.adaptors.yubikey.YubiKeyAccount in project cas by apereo.

the class MongoDbYubiKeyAccountRegistry method registerAccountFor.

@Override
public boolean registerAccountFor(final String uid, final String token) {
    if (getAccountValidator().isValid(uid, token)) {
        final String yubikeyPublicId = getAccountValidator().getTokenPublicId(token);
        final YubiKeyAccount account = new YubiKeyAccount();
        account.setPublicId(getCipherExecutor().encode(yubikeyPublicId));
        account.setUsername(uid);
        this.mongoTemplate.save(account, this.collectionName);
        return true;
    }
    return false;
}
Also used : YubiKeyAccount(org.apereo.cas.adaptors.yubikey.YubiKeyAccount)

Aggregations

YubiKeyAccount (org.apereo.cas.adaptors.yubikey.YubiKeyAccount)6 lombok.val (lombok.val)3 Operation (io.swagger.v3.oas.annotations.Operation)2 YubiKeyAccountRegistry (org.apereo.cas.adaptors.yubikey.YubiKeyAccountRegistry)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Parameter (io.swagger.v3.oas.annotations.Parameter)1 File (java.io.File)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Slf4j (lombok.extern.slf4j.Slf4j)1 IOUtils (org.apache.commons.io.IOUtils)1 DefaultYubiKeyAccountValidator (org.apereo.cas.adaptors.yubikey.DefaultYubiKeyAccountValidator)1 JsonYubiKeyAccountRegistry (org.apereo.cas.adaptors.yubikey.registry.JsonYubiKeyAccountRegistry)1 OpenYubiKeyAccountRegistry (org.apereo.cas.adaptors.yubikey.registry.OpenYubiKeyAccountRegistry)1 PermissiveYubiKeyAccountRegistry (org.apereo.cas.adaptors.yubikey.registry.PermissiveYubiKeyAccountRegistry)1