package org.apereo.cas.adaptors.u2f.web;

import org.apereo.cas.adaptors.u2f.storage.U2FDeviceRegistration;
import org.apereo.cas.adaptors.u2f.storage.U2FDeviceRepository;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.web.BaseCasActuatorEndpoint;

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.http.MediaType;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * This is {@link U2FRegisteredDevicesEndpoint}.
 *
 * @author Misagh Moayyed
 * @since 6.3.0
 */
@Endpoint(id = "u2fDevices", enableByDefault = false)
public class U2FRegisteredDevicesEndpoint extends BaseCasActuatorEndpoint {
    private final U2FDeviceRepository u2fDeviceRepository;

    public U2FRegisteredDevicesEndpoint(final CasConfigurationProperties casProperties,
                                        final U2FDeviceRepository u2fDeviceRepository) {
        super(casProperties);
        this.u2fDeviceRepository = u2fDeviceRepository;
    }

    @ReadOperation(produces = MediaType.APPLICATION_JSON_VALUE)
    public Collection<? extends U2FDeviceRegistration> fetchAll() {
        return u2fDeviceRepository.getRegisteredDevices()
            .stream()
            .map(u2fDeviceRepository::decode)
            .collect(Collectors.toList());
    }

    @ReadOperation(produces = MediaType.APPLICATION_JSON_VALUE)
    public Collection<? extends U2FDeviceRegistration> fetchBy(@Selector final String username) {
        return u2fDeviceRepository.getRegisteredDevices(username)
            .stream()
            .map(u2fDeviceRepository::decode)
            .collect(Collectors.toList());
    }

    /**
     * Delete.
     *
     * @param username the username
     */
    @DeleteOperation
    public void delete(@Selector final String username) {
        u2fDeviceRepository.getRegisteredDevices(username)
            .forEach(u2fDeviceRepository::deleteRegisteredDevice);
    }

    /**
     * Delete.
     *
     * @param username the username
     * @param id       the id
     */
    @DeleteOperation
    public void delete(@Selector final String username, @Selector final Long id) {
        u2fDeviceRepository.getRegisteredDevices(username)
            .stream()
            .filter(d -> d.getId() == id)
            .forEach(u2fDeviceRepository::deleteRegisteredDevice);
    }
}

@@ -281,16 +281,35 @@
 point;%0A%0A
+import lombok.val;%0A
 import o
@@ -636,16 +636,44 @@
 aType;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -2099,32 +2099,72 @@
 name) %7B%0A        
+val registeredDevices = new ArrayList%3C%3E(
 u2fDeviceReposit
@@ -2189,32 +2189,34 @@
 evices(username)
+);
 %0A            .fo
@@ -2200,36 +2200,49 @@
 name));%0A        
-    
+registeredDevices
 .forEach(u2fDevi
@@ -2503,16 +2503,56 @@
         
+val registeredDevices = new ArrayList%3C%3E(
 u2fDevic
@@ -2585,32 +2585,60 @@
 evices(username)
+);%0A        registeredDevices
 %0A            .st

add delete support to u2f