Skip to content

Commit

Permalink
Rework auth process
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Spielmann <simon@MacBook-Pro.fritz.box>
  • Loading branch information
Simon Spielmann authored and Simon Spielmann committed Nov 10, 2022
1 parent e58beff commit b2aeeab
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.UUID;

import org.openhab.binding.icloud.internal.utilities.ListUtil;
import org.openhab.binding.icloud.internal.utilities.Pair;
import org.openhab.core.storage.Storage;
import org.slf4j.Logger;
Expand Down Expand Up @@ -72,8 +73,7 @@ public ICloudService(String appleId, String password, Storage<String> stateStora
this.clientId = "auth-" + UUID.randomUUID().toString().toLowerCase();

this.session = new ICloudSession(stateStorage);
this.session.setDefaultHeaders(Pair.of("Accept", "*/*"), Pair.of("Origin", HOME_ENDPOINT),
Pair.of("Referer", HOME_ENDPOINT + "/"));
this.session.setDefaultHeaders(Pair.of("Origin", HOME_ENDPOINT), Pair.of("Referer", HOME_ENDPOINT + "/"));
}

/**
Expand Down Expand Up @@ -221,7 +221,8 @@ public boolean validate2faCode(String code) throws IOException, InterruptedExcep

Map<String, Object> requestBody = Map.of("securityCode", Map.of("code", code));

List<Pair<String, String>> headers = getAuthHeaders();
List<Pair<String, String>> headers = ListUtil.replaceEntries(getAuthHeaders(),
List.of(Pair.of("Accept", "application/json")));

addSessionHeaders(headers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;

import org.openhab.binding.icloud.internal.utilities.CustomCookieStore;
import org.openhab.binding.icloud.internal.utilities.ListUtil;
import org.openhab.binding.icloud.internal.utilities.Pair;
import org.openhab.core.storage.Storage;
import org.slf4j.Logger;
Expand Down Expand Up @@ -100,7 +101,8 @@ public String post(String url, String body, List<Pair<String, String>> overrideH
* Invoke an HTTP GET request to the given url.
*
* @param url URL to call.
* @param overrideHeaders  If not null the given headers are used instead of the standard headers set via
* @param overrideHeaders  If not null the given headers are used to replace corresponding entries of the standard
* headers set via
* {@link #setDefaultHeaders(Pair...)}
* @return Result body as {@link String}.
* @throws IOException if I/O error occurred
Expand All @@ -117,10 +119,7 @@ private String request(String method, String url, String body, List<Pair<String,

Builder builder = HttpRequest.newBuilder().uri(URI.create(url));

List<Pair<String, String>> requestHeaders = this.headers;
if (overrideHeaders != null) {
requestHeaders = overrideHeaders;
}
List<Pair<String, String>> requestHeaders = ListUtil.replaceEntries(this.headers, overrideHeaders);

for (Pair<String, String> header : requestHeaders) {
builder.header(header.getKey(), header.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.openhab.binding.icloud.internal.json.response.ICloudDeviceInformation;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.storage.Storage;
import org.openhab.core.test.storage.VolatileStorage;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingStatus;
Expand Down Expand Up @@ -108,16 +107,12 @@ public void initialize() {
final String localAppleId = config.appleId;
final String localPassword = config.password;

if (this.iCloudService == null) {
if (localAppleId != null && localPassword != null) {
// this.iCloudService = new ICloudService(localAppleId, localPassword, this.storage);
this.iCloudService = new ICloudService(localAppleId, localPassword, new VolatileStorage<String>());
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Apple ID/Password is not set!");
return;

}
if (localAppleId != null && localPassword != null) {
this.iCloudService = new ICloudService(localAppleId, localPassword, this.storage);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Apple ID/Password is not set!");
return;
}

updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Wait for login");
Expand Down Expand Up @@ -244,8 +239,6 @@ private void checkLogin() {
}
if (iCloudService.requires2fa()) {
// New code was requested. Wait for the user to update config.
String code = "999999";
success = this.iCloudService.validate2faCode(code);
logger.warn(
"ICloud authentication requires 2-FA code. Please provide code in in thing configuration.");
validate2faCode = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.icloud.internal.utilities;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* This class implements util methods for list handling.
*
* @author Simon Spielmann - Initial contribution
*
*/
public abstract class ListUtil {

private ListUtil() {
};

/**
* Replace entries in the given originalList with entries from replacements, if the have an equal key.
*
* @param <K> Type of first pair element
* @param <V> Type of second pair element
* @param originalList List with entries to replace
* @param replacements Replacement entries
* @return New list with replaced entries
*/
public static <K, V> List<Pair<K, V>> replaceEntries(List<Pair<K, V>> originalList, List<Pair<K, V>> replacements) {
List<Pair<K, V>> result = new ArrayList<>(originalList);
if (replacements != null) {
Iterator<Pair<K, V>> it = result.iterator();
while (it.hasNext()) {
Pair<K, V> requestHeader = it.next();
for (Pair<K, V> replacementHeader : replacements) {
if (requestHeader.getKey().equals(replacementHeader.getKey())) {
it.remove();
}
}
}
result.addAll(replacements);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openhab.binding.icloud.internal.ICloudService;
import org.openhab.binding.icloud.internal.json.response.ICloudAccountDataResponse;
import org.openhab.core.storage.json.internal.JsonStorage;
import org.openhab.core.test.storage.VolatileStorage;

/**
*
Expand All @@ -49,8 +48,8 @@ public void testAuth() throws IOException, InterruptedException {
JsonStorage<String> stateStorage = new JsonStorage<String>(jsonStorageFile, TestICloud.class.getClassLoader(),
0, 1000, 1000, List.of());

// ICloudService service = new ICloudService(this.E_MAIL, this.PW, stateStorage);
ICloudService service = new ICloudService(this.E_MAIL, this.PW, new VolatileStorage<>());
ICloudService service = new ICloudService(this.E_MAIL, this.PW, stateStorage);
// ICloudService service = new ICloudService(this.E_MAIL, this.PW, new VolatileStorage<>());
service.authenticate(false);
if (service.requires2fa()) {
System.out.print("Code: ");
Expand Down

0 comments on commit b2aeeab

Please sign in to comment.