From 196d8095bbc2e2e8fe33348b56b3aeadad22d3d2 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 13 Aug 2023 18:55:13 +0200 Subject: [PATCH] [tr064] Fix clearing of auth (results) (#15415) * [tr064] Fix clearing of auth (results) In case of multiple root things the auth store (results and digests) was cleared for all clients instead of only removing the failed auth result or old authentication. * fix TAM request Signed-off-by: Jan N. Klug --- .../binding/tr064/internal/Tr064RootHandler.java | 14 ++++++++++---- .../binding/tr064/internal/soap/SOAPConnector.java | 8 +++++++- .../tr064/internal/soap/SOAPValueConverter.java | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/Tr064RootHandler.java b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/Tr064RootHandler.java index a80dd8e6e..17c42a2f2 100644 --- a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/Tr064RootHandler.java +++ b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/Tr064RootHandler.java @@ -207,8 +207,6 @@ public class Tr064RootHandler extends BaseBridgeHandler implements PhonebookProv uninstallPolling(); stateCache.clear(); scpdUtil = null; - - super.dispose(); } /** @@ -267,8 +265,16 @@ public class Tr064RootHandler extends BaseBridgeHandler implements PhonebookProv // clear auth cache and force re-auth AuthenticationStore authStore = httpClient.getAuthenticationStore(); - authStore.clearAuthentications(); - authStore.clearAuthenticationResults(); + URI endpointUri = URI.create(endpointBaseURL); + Authentication authentication = authStore.findAuthentication("Digest", endpointUri, + Authentication.ANY_REALM); + if (authentication != null) { + authStore.removeAuthentication(authentication); + } + Authentication.Result authResult = authStore.findAuthenticationResult(endpointUri); + if (authResult != null) { + authStore.removeAuthenticationResult(authResult); + } authStore.addAuthentication(new DigestAuthentication(new URI(endpointBaseURL), Authentication.ANY_REALM, config.user, config.password)); diff --git a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPConnector.java b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPConnector.java index 3aa372d58..7c6c72b28 100644 --- a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPConnector.java +++ b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPConnector.java @@ -17,6 +17,7 @@ import static org.openhab.binding.tr064.internal.util.Util.getSOAPElement; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.URI; import java.time.Duration; import java.util.HashMap; import java.util.Map; @@ -37,6 +38,7 @@ import javax.xml.soap.SOAPPart; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.Authentication; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.util.BytesContentProvider; @@ -177,7 +179,11 @@ public class SOAPConnector { if (response.getStatus() == HttpStatus.UNAUTHORIZED_401) { // retry once if authentication expired logger.trace("Re-Auth needed."); - httpClient.getAuthenticationStore().clearAuthenticationResults(); + Authentication.Result authResult = httpClient.getAuthenticationStore() + .findAuthenticationResult(URI.create(endpointBaseURL)); + if (authResult != null) { + httpClient.getAuthenticationStore().removeAuthenticationResult(authResult); + } request = prepareSOAPRequest(soapRequest).timeout(timeout, TimeUnit.SECONDS); response = request.send(); } diff --git a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPValueConverter.java b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPValueConverter.java index 7324ccf83..5f90f4d7f 100644 --- a/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPValueConverter.java +++ b/bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/soap/SOAPValueConverter.java @@ -244,7 +244,7 @@ public class SOAPValueConverter { @SuppressWarnings("unused") private State processTamListURL(State state, Tr064ChannelConfig channelConfig) throws PostProcessingException { try { - ContentResponse response = httpClient.newRequest(state.toString()).timeout(timeout, TimeUnit.MILLISECONDS) + ContentResponse response = httpClient.newRequest(state.toString()).timeout(timeout, TimeUnit.SECONDS) .send(); String responseContent = response.getContentAsString(); int messageCount = responseContent.split("1").length - 1;