use of com.thecoderscorner.menu.remote.commands.MenuButtonType 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;
}
});
}
Aggregations