Connection with Digest Authentication lost Authentication

Hi There,

I’m starting to develop a binding that consumes a service secured with digest authentication. In general, this works good with the httpClient provided by OpenHab.
But after a while (I can not exactly say if it’s dependend to debugging or just time based) the connection loses it’s authentication and returns 401. The only solution then is to restart openhab.

I’m not sure if maybe I misunderstood a concept of openhab, there is a bug in my implementation or a problem with jetty at all. Hopefully someone here has some tips, how to debug this issue.

Here is a minimal example of my implementation. I’ve also tried adding the authentication every time the testConnection() method is called. I’ve also tried to remove all existing connections for that uri from the AuthenticationStore before sending the request.

public class MyConnection {

    private final HttpClient httpClient;

    private AuthenticationStore authenticationStore;

    public MyConnection(MyBridgeHandler bridgeHandler, HttpClient httpClient) {
        this.configuration = bridgeHandler.getConfiguration();
        this.httpClient = httpClient;

        this.authenticationStore = httpClient.getAuthenticationStore();
    }

    private void setAuthentication(URI uri) {
        String realm = "MyRealm";

        Authentication authentication = authenticationStore.findAuthentication("Digest", uri, realm);

        if (authentication == null) {
            authenticationStore.addAuthentication(
                new DigestAuthentication(uri, realm, configuration.username, configuration.password));
        }
    }

    public void testConnection() {
        String url = "http://" + configuration.hostname + "/ws";

        String request = "MyReqestBody";

        try {
            setAuthentication(new URI(url));

            ContentResponse response = httpClient.POST(url).content(new StringContentProvider(request)).send();

        } catch (Exception e) {
            logger.error(e.getMessage());
            logger.debug("Exception while connecting to endpoint '" + url + "'", e);
        }
    }
}