Hello,
I’d like to develop myVaillant connector. There is already a python library wrapping perfectly the myVaillant services. I have some problems just on the login, I put a test code here (it does do exactly the same of myPyllant project )
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class MyVaillantMain {
private final static String CLIENT_ID = "myvaillant";
private final static String AUTHENTICATE_URL = "https://identity.vaillant-group.com/auth/realms/vaillant-italy-b2c/protocol/openid-connect/auth";
private final static String LOGIN_URL = "https://identity.vaillant-group.com/auth/realms/vaillant-italy-b2c/login-actions/authenticate";
public static void main(String[] args) throws Exception {
String generatedCode[] = generateCode();
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultCookieStore(cookieStore)
.disableRedirectHandling()
.build();
URI uri = new URIBuilder(AUTHENTICATE_URL).addParameter("client_id", CLIENT_ID)
.addParameter("redirect_uri", "enduservaillant.page.link://login").addParameter("response_type", "code")
.addParameter("code", "code_challenge").addParameter("code_challenge_method", "S256")
.addParameter("code_challenge", generatedCode[1]).build();
HttpGet httpGet = new HttpGet(uri.toString());
try {
CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httpGet);
String login_html = EntityUtils.toString(response.getEntity());
// String login_html = new BasicResponseHandler().handleResponse(response);
response.close();
Pattern pattern = Pattern.compile(LOGIN_URL + "\\?([^\\\"]*)");
Matcher matcher = pattern.matcher(login_html);
if (matcher.find()) {
String username = "<your-username>";
String password = "<your-password>";
String loginUrl = matcher.group().replace("&", "&");
HttpPost authPost = new HttpPost(loginUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("credentialId", ""));
authPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
response = httpclient.execute(authPost);
response.getAllHeaders();
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println(response.getStatusLine().getStatusCode());
response.close();
}
} finally {
httpclient.close();
}
}
private static String[] generateCode() throws NoSuchAlgorithmException {
String code_verifier = shuffle(RandomStringUtils.randomAlphabetic(64) + RandomStringUtils.randomNumeric(64));
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(code_verifier.getBytes(StandardCharsets.UTF_8));
String b64 = Base64.getUrlEncoder().encodeToString(hash);
String code_challenge = b64.replace("=", "");
return new String[] { code_verifier, code_challenge };
}
public static String shuffle(String input) {
List<Character> characters = new ArrayList<Character>();
for (char c : input.toCharArray()) {
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while (characters.size() != 0) {
int randPicker = (int) (Math.random() * characters.size());
output.append(characters.remove(randPicker));
}
return output.toString();
}
}
I get “Your request was automatically detected as a potential threat.” In Python I don’t have any problem, can anybody help me to know what I’m missing so I can try to develop the binding?
Thanks in advance