2

For my project I want to connect to the provided Shopware API Since I'm not that experienced with REST Services I cloned an existing HTTPClient (using the Apache Components) from Github. I have to add that Shopware offers the DigestAuth as Authentication Method.

CODE:

public RestApiHttpClient(URL apiEndpoint, String username, String password) {
    this.apiEndpoint = apiEndpoint;

    BasicHttpContext context = new BasicHttpContext();
    this.localContext = HttpClientContext.adapt(context);
    HttpHost target = new HttpHost(this.apiEndpoint.getHost(), -1, this.apiEndpoint.getProtocol());
    this.localContext.setTargetHost(target);


    TargetAuthenticationStrategy authStrat = new TargetAuthenticationStrategy();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    AuthScope aScope = new AuthScope(target.getHostName(), target.getPort());
    credsProvider.setCredentials(aScope, creds);

    BasicAuthCache authCache = new BasicAuthCache();+

    // Digest Authentication
    DigestScheme digestAuth = new DigestScheme(Charset.forName("UTF-8"));
    authCache.put(target, digestAuth);
    this.localContext.setAuthCache(authCache);
    this.localContext.setCredentialsProvider(credsProvider);


    ArrayList<Header> defHeaders = new ArrayList<>();
    defHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()));
    this.httpclient = HttpClients.custom()
            .useSystemProperties()
            .setTargetAuthenticationStrategy(authStrat)
            .disableRedirectHandling()
            .disableContentCompression()
            .setDefaultHeaders(defHeaders)
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    }

So if i test this script i get this error:

Dez 07, 2018 9:00:55 AM org.apache.http.impl.auth.HttpAuthenticator generateAuthResponse
SCHWERWIEGEND: DIGEST [complete=false, nonce=null, nc=0] authentication error: missing realm in challenge
{"success":false,"message":"Invalid or missing auth"}

I tried overwriting the paramters (first only the realm, than realm and nonce, than every parameter) but with no success:

digestAuth.overrideParamter("realm", "Shopware REST-API");
digestAuth.overrideParamter("nonce", "somerandomnumber");
// or create random number;
digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
digestAuth.overrideParamter("qop", "auth");
digestAuth.overrideParamter("nc", "0");
digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());

Im guessing the DigestAuth need's the nonce value or the whole header from the server, but I couldn't find a method to store these values. Also tried this one with no success.

Any ideas? thank you in advance!

0 Answers0