From b56ee7028855f2918efdb930cc5e861ebb103327 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Wed, 20 May 2020 03:58:04 +0200 Subject: [PATCH] [freebox] Hot fix for login to the freebox (#7696) * [freebox] Hot fix for login to the freebox PR #7460 broke the freebox binding * Unit test added for method hmacSha1 Signed-off-by: Laurent Garnier --- .../internal/FreeboxHandlerFactory.java | 3 +- .../internal/api/FreeboxApiManager.java | 4 +-- .../internal/api/FreeboxApiManagerTest.java | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 bundles/org.openhab.binding.freebox/src/test/java/org/openhab/binding/freebox/internal/api/FreeboxApiManagerTest.java diff --git a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/FreeboxHandlerFactory.java b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/FreeboxHandlerFactory.java index 1af9a22929ef7..ff742b211df7e 100644 --- a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/FreeboxHandlerFactory.java +++ b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/FreeboxHandlerFactory.java @@ -21,7 +21,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.core.Configuration; @@ -95,7 +94,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { } @Override - public @Nullable Thing createThing(@NonNull ThingTypeUID thingTypeUID, @NonNull Configuration configuration, + public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) { if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) { return super.createThing(thingTypeUID, configuration, thingUID, null); diff --git a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java index e16dc21043455..928b1e77d93af 100644 --- a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java +++ b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java @@ -479,7 +479,7 @@ private String encodeUrl(String url) throws FreeboxException { } } - private static String hmacSha1(String key, String value) throws FreeboxException { + public static String hmacSha1(String key, String value) throws FreeboxException { try { // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); @@ -493,7 +493,7 @@ private static String hmacSha1(String key, String value) throws FreeboxException byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to a String - return HexUtils.bytesToHex(rawHmac); + return HexUtils.bytesToHex(rawHmac).toLowerCase(); } catch (IllegalArgumentException | NoSuchAlgorithmException | InvalidKeyException | IllegalStateException e) { throw new FreeboxException("Computing the hmac-sha1 of the challenge and the app token failed", e); } diff --git a/bundles/org.openhab.binding.freebox/src/test/java/org/openhab/binding/freebox/internal/api/FreeboxApiManagerTest.java b/bundles/org.openhab.binding.freebox/src/test/java/org/openhab/binding/freebox/internal/api/FreeboxApiManagerTest.java new file mode 100644 index 0000000000000..280b2a7464179 --- /dev/null +++ b/bundles/org.openhab.binding.freebox/src/test/java/org/openhab/binding/freebox/internal/api/FreeboxApiManagerTest.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2010-2020 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.freebox.internal.api; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.Assert; +import org.junit.Test; + +/** + * + * @author Laurent Garnier - Initial contribution + */ +@NonNullByDefault +public class FreeboxApiManagerTest { + + @Test + public void hmacSha1Test() throws Exception { + String expected = "25dad1bb5604321f12b755cc9d755d1480cf7989"; + String actual = FreeboxApiManager.hmacSha1("Token1234", "Challenge"); + Assert.assertEquals(expected, actual); + } + +}