@Override
    public DescriptiveUrl toUrl(final Host bookmark) {
        try {
            // Run password flow
            final TokenResponse response;
            try {
                final Host target = new Host(new DAVSSLProtocol(), "oauth.freenet.de");
                final X509TrustManager trust = new KeychainX509TrustManager(new DisabledCertificateTrustCallback(),
                        new DefaultTrustManagerHostnameCallback(target), CertificateStoreFactory.get());
                final X509KeyManager key = new KeychainX509KeyManager(new DisabledCertificateIdentityCallback(), target,
                        CertificateStoreFactory.get());
                final CloseableHttpClient client = new HttpConnectionPoolBuilder(
                        target, new ThreadLocalHostnameDelegatingTrustManager(trust, target.getHostname()), key, ProxyFactory.get()
                ).build(ProxyFactory.get(), new DisabledTranscriptListener(), new DisabledLoginCallback())
                        .setUserAgent(new FreenetUserAgentProvider().get())
                        .build();
                final String username = bookmark.getCredentials().getUsername();
                final String password;
                if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) {
                    password = keychain.findLoginPassword(bookmark);
                }
                else {
                    password = bookmark.getCredentials().getPassword();
                }
                if(null == password) {
                    log.warn(String.format("No password found for %s", bookmark));
                    return DescriptiveUrl.EMPTY;
                }
                response = new PasswordTokenRequest(new ApacheHttpTransport(client),
                        new GsonFactory(), new GenericUrl("https://oauth.freenet.de/oauth/token"), username, password)
                        .setClientAuthentication(new BasicAuthentication("desktop_client", "6LIGIHuOSkznLomu5xw0EPPBJOXb2jLp"))
                        .setRequestInitializer(new UserAgentHttpRequestInitializer(new FreenetUserAgentProvider()))
                        .set("world", new HostPreferences(bookmark).getProperty("world"))
                        .set("webLogin", Boolean.TRUE)
                        .execute();
                final FreenetTemporaryLoginResponse login = this.getLoginSession(client, response.getAccessToken());
                return new DescriptiveUrl(URI.create(login.urls.login), DescriptiveUrl.Type.authenticated);
            }
            catch(IOException e) {
                throw new HttpExceptionMappingService().map(e);
            }
        }
        catch(BackgroundException e) {
            log.warn(String.format("Failure %s retrieving authenticated URL for %s", e, bookmark));
            return DescriptiveUrl.EMPTY;
        }
    }
@Test
    public void testToUrl() {
        final FreenetAuthenticatedUrlProvider provider = new FreenetAuthenticatedUrlProvider(new DefaultHostPasswordStore() {
            @Override
            public String getPassword(final String serviceName, final String accountName) throws LocalAccessDeniedException {
                return PROPERTIES.get("freenet.password");
            }

            @Override
            public void addPassword(final String serviceName, final String accountName, final String password) throws LocalAccessDeniedException {

            }

            @Override
            public String getPassword(final Scheme scheme, final int port, final String hostname, final String user) throws LocalAccessDeniedException {
                return PROPERTIES.get("freenet.password");
            }

            @Override
            public void addPassword(final Scheme scheme, final int port, final String hostname, final String user, final String password) throws LocalAccessDeniedException {

            }

            @Override
            public void deletePassword(final String serviceName, final String user) throws LocalAccessDeniedException {

            }

            @Override
            public void deletePassword(final Scheme scheme, final int port, final String hostname, final String user) throws LocalAccessDeniedException {

            }
        });
        final DescriptiveUrl url = provider.toUrl(session.getHost());
        assertNotEquals(DescriptiveUrl.EMPTY, url);
        assertEquals(DescriptiveUrl.Type.authenticated, url.getType());
    }