Search in sources :

Example 1 with DialogManager

use of com.thecoderscorner.menu.mgr.DialogManager in project tcMenu by davetcc.

the class PropertiesAuthenticator method addAuthentication.

/**
 * Adds an authentication token to the store, it assumes that all appropriate permission from the user has
 * been sought.
 * @param user the user to add
 * @param uuid the uuid associated with the user
 * @return
 */
@Override
public CompletableFuture<Boolean> addAuthentication(String user, UUID uuid) {
    if (dialogManager == null)
        return CompletableFuture.completedFuture(false);
    return CompletableFuture.supplyAsync(() -> {
        try {
            logger.log(INFO, "Request for authentication with " + user);
            var shouldProceed = new AtomicBoolean(false);
            var dialogLatch = new CountDownLatch(1);
            dialogManager.withTitle("Pair with " + user, true).withMessage("Be sure you know where this connection originated", true).withDelegate(DialogViewer.DialogShowMode.REGULAR, menuButtonType -> {
                shouldProceed.set(menuButtonType == MenuButtonType.ACCEPT);
                dialogLatch.countDown();
                return true;
            }).showDialogWithButtons(MenuButtonType.ACCEPT, MenuButtonType.CANCEL);
            if (!dialogLatch.await(30, TimeUnit.SECONDS)) {
                logger.log(INFO, "Dialog Latch timed out without user operation");
            }
            if (shouldProceed.get()) {
                synchronized (properties) {
                    Path pathLocation = Path.of(location);
                    properties.setProperty(user, uuid.toString());
                    properties.store(Files.newBufferedWriter(pathLocation, CREATE, TRUNCATE_EXISTING), "TcMenu Auth properties");
                }
                logger.log(INFO, "Wrote auth properties to ", location);
                return true;
            } else {
                logger.log(INFO, "Pairing dialog was not accepted");
                return false;
            }
        } catch (Exception e) {
            logger.log(ERROR, "Failed to write auth properties to ", location);
            return false;
        }
    });
}
Also used : Properties(java.util.Properties) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) INFO(java.lang.System.Logger.Level.INFO) DialogManager(com.thecoderscorner.menu.mgr.DialogManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) DialogViewer(com.thecoderscorner.menu.mgr.DialogViewer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ERROR(java.lang.System.Logger.Level.ERROR) Path(java.nio.file.Path) MenuButtonType(com.thecoderscorner.menu.remote.commands.MenuButtonType) Path(java.nio.file.Path) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Aggregations

DialogManager (com.thecoderscorner.menu.mgr.DialogManager)1 DialogViewer (com.thecoderscorner.menu.mgr.DialogViewer)1 MenuButtonType (com.thecoderscorner.menu.remote.commands.MenuButtonType)1 IOException (java.io.IOException)1 ERROR (java.lang.System.Logger.Level.ERROR)1 INFO (java.lang.System.Logger.Level.INFO)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 Properties (java.util.Properties)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1