fix EnedisWebBridgeHandler to support last version of enedis WebSite : need new call to uri : /mon-compte-client/api/private/v1/userinfos to retrieve the personne_for_MPS172TIB cookie

Signed-off-by: Laurent ARNAL <laurent@clae.net>
This commit is contained in:
Laurent ARNAL 2024-11-08 14:16:12 +01:00
parent 2179e831c9
commit 3b44508f67
2 changed files with 33 additions and 6 deletions

View File

@ -99,8 +99,10 @@ public class EnedisHttpApi {
private static String getData(LinkyBridgeHandler linkyBridgeHandler, String url, HttpClient httpClient,
String token) throws LinkyException {
try {
Request request = httpClient.newRequest(url)
.agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
Request request = httpClient.newRequest(url);
request = request.agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
request = request.method(HttpMethod.GET);
if (!token.isEmpty()) {
request = request.header("Authorization", "" + token);

View File

@ -12,9 +12,13 @@
*/
package org.openhab.binding.linky.internal.handler;
import java.net.HttpCookie;
import java.net.URI;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
@ -66,6 +70,7 @@ public class EnedisWebBridgeHandler extends LinkyBridgeHandler {
public static final String URL_COMPTE_PART = URL_MON_COMPTE.replace("compte", "compte-particulier");
public static final URI COOKIE_URI = URI.create(URL_COMPTE_PART);
private static final String USER_INFO_CONTRACT_URL = BASE_URL + "/mon-compte-client/api/private/v1/userinfos";
private static final String USER_INFO_URL = BASE_URL + "/userinfos";
private static final String PRM_INFO_BASE_URL = BASE_URL + "/mes-mesures/api/private/v1/personnes/";
private static final String PRM_INFO_URL = PRM_INFO_BASE_URL + "null/prms";
@ -230,10 +235,6 @@ public class EnedisWebBridgeHandler extends LinkyBridgeHandler {
logger.debug("Add the tokenId cookie");
enedisApi.addCookie("enedisExt", authResult.tokenId);
enedisApi.addCook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rqM7JvqHHdPcqXmUqbgspKRDISMC4TeEzWNjM_xLqJ9YSb3JcvI1JrFzUabRf7I_MTnk-MYD3tjBkhEx6at6eYP715_HBQ_ymaK08uhTj5kl0uJUr6A1ReJjElZILQCib1b5Y0O6bgfdC5wjGvj5R5InetEzNnFxKn7Y7NR7llan_LUpZLgrj8flNj8s1zV05yNNO-hqEWdCSCN84tc2DjiiGQ9kNnZPYxyYInR2wNAocv7Kwt9Zq79HJtwyuHfUOwYZGAiu7j_4vAjKMCTJafZARdlLz0aQsXVqfTSpv2KkBtjBRw.wjzjNu2wXlDM54nPfn5djQ");
logger.debug("Step 5 : retrieve the SAMLresponse");
data = enedisApi.getData(URL_MON_COMPTE + "/" + authResult.successUrl);
@ -247,6 +248,30 @@ public class EnedisWebBridgeHandler extends LinkyBridgeHandler {
if (result.getStatus() != 302) {
throw new LinkyException("Connection failed step 6");
}
logger.debug("Step 7 : retrieve ");
result = httpClient.GET(USER_INFO_CONTRACT_URL);
HashMap hashRes = gson.fromJson(result.getContentAsString(), HashMap.class);
String cookieKey;
if (hashRes != null && hashRes.containsKey("cnAlex")) {
cookieKey = "personne_for_" + (String) hashRes.get("cnAlex");
} else {
throw new LinkyException("Connection failed step 7, missing cookieKey");
}
List<HttpCookie> lCookie = httpClient.getCookieStore().getCookies();
Optional<HttpCookie> cookie = lCookie.stream().filter(it -> it.getName().contains(cookieKey)).findFirst();
String cookieVal;
if (cookie.isPresent()) {
cookieVal = cookie.get().getValue();
} else {
throw new LinkyException("Connection failed step 7, missing cookieVal");
}
enedisApi.addCookie(cookieKey, cookieVal);
connected = true;
} catch (InterruptedException | TimeoutException | ExecutionException | JsonSyntaxException e) {
throw new LinkyException(e, "Error opening connection with Enedis webservice");