-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
351 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...g/openhab/binding/folderwatcher/internal/api/auth/Azure4SignerForAuthorizationHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (c) 2010-2025 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.folderwatcher.internal.api.auth; | ||
|
||
import java.net.URL; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.folderwatcher.internal.api.exception.AuthException; | ||
import org.openhab.binding.folderwatcher.internal.api.util.HttpUtilException; | ||
|
||
/** | ||
* The {@link Azure4SignerForAuthorizationHeader} class contains methods for Azure Blob API authentication using HTTPS | ||
* headers. | ||
* | ||
* @author Alexandr Salamatov - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class Azure4SignerForAuthorizationHeader extends AzureSignerBase { | ||
|
||
public Azure4SignerForAuthorizationHeader(String httpMethod, URL endpointUrl) { | ||
super(endpointUrl, httpMethod, "serviceName", "regionName"); | ||
} | ||
|
||
public String computeSignature(Map<String, String> headers, Map<String, String> queryParameters, | ||
String AzureAccount, String AzureSecretKey, String azureContainerName) | ||
throws AuthException, HttpUtilException { | ||
String dateTimeStamp = dateTimeFormat.format(java.time.ZonedDateTime.now(java.time.ZoneOffset.UTC)); | ||
headers.put("x-ms-date", dateTimeStamp); | ||
headers.put("x-ms-version", "2020-08-04"); | ||
|
||
Map<String, String> filteredHeaders = new HashMap<>(); | ||
filteredHeaders.putAll(headers); | ||
Iterator<Map.Entry<String, String>> iterator = filteredHeaders.entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Map.Entry<String, String> entry = iterator.next(); | ||
if (!entry.getKey().startsWith(HEADER_FILTER)) { | ||
iterator.remove(); | ||
} | ||
} | ||
String canonicalizedHeaders = getCanonicalizedHeaderString(filteredHeaders); | ||
String canonicalizedQueryParameters = getCanonicalizedQueryString(queryParameters); | ||
String canonicalilezdResource = getCanonicalResource(endpointUrl, canonicalizedQueryParameters); | ||
String stringToSign = getStringToSign("GET", "", "", "", "", "", "", "", "", "", "", "", canonicalizedHeaders, | ||
canonicalilezdResource); | ||
byte[] kSecret = Base64.getDecoder().decode(AzureSecretKey); | ||
byte[] signed = sign(stringToSign, kSecret, "HmacSHA256"); | ||
return "SharedKey" + " " + AzureAccount + ":" + Base64.getEncoder().encodeToString(signed); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...er/src/main/java/org/openhab/binding/folderwatcher/internal/api/auth/AzureSignerBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2010-2025 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.folderwatcher.internal.api.auth; | ||
|
||
import java.net.URL; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.folderwatcher.internal.api.exception.AuthException; | ||
import org.openhab.binding.folderwatcher.internal.api.util.HttpUtilException; | ||
import org.openhab.binding.folderwatcher.internal.api.util.HttpUtils; | ||
|
||
/** | ||
* The {@link AzureSignerBase} class contains based methods for Azure Blob API authentication. | ||
* | ||
* @author Alexandr Salamatov - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public abstract class AzureSignerBase extends SignerBase { | ||
protected static String PAIR_SEPARATOR = "\n"; | ||
protected static String VALEU_SEPARATOR = ":"; | ||
protected static String HEADER_FILTER = "x-ms-"; | ||
protected URL endpointUrl; | ||
protected String httpMethod; | ||
protected String serviceName; | ||
protected String regionName; | ||
protected DateTimeFormatter dateTimeFormat; | ||
|
||
public AzureSignerBase(URL endpointUrl, String httpMethod, String serviceName, String regionName) { | ||
|
||
super(PAIR_SEPARATOR, VALEU_SEPARATOR); | ||
|
||
this.endpointUrl = endpointUrl; | ||
this.httpMethod = httpMethod; | ||
this.serviceName = serviceName; | ||
this.regionName = regionName; | ||
|
||
dateTimeFormat = java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; | ||
} | ||
|
||
protected static String getCanonicalResource(URL endpoint, String queryParameters) throws HttpUtilException { | ||
return getCanonicalizedResourceName(endpoint) + getCanonicalizedResourcePath(endpoint) + "\n" + queryParameters; | ||
} | ||
|
||
protected static String getCanonicalizedResourceName(URL endpoint) throws HttpUtilException { | ||
if (endpoint == null) { | ||
return "/"; | ||
} | ||
String path = endpoint.getHost().split("\\.")[0]; | ||
if (path == null || path.isEmpty()) { | ||
return "/"; | ||
} | ||
|
||
String encodedPath = HttpUtils.urlEncode(path, true); | ||
if (encodedPath.startsWith("/")) { | ||
return encodedPath; | ||
} else { | ||
return "/".concat(encodedPath); | ||
} | ||
} | ||
|
||
protected static String getStringToSign(String VERB, String ContentEncoding, String ContentLanguage, | ||
String ContentLength, String ContentMD5, String ContentType, String Date, String IfModifiedSince, | ||
String IfMatch, String IfNoneMatch, String IfUnmodifiedSince, String Range, String CanonicalizedHeaders, | ||
String CanonicalizedResource) throws AuthException { | ||
|
||
return VERB + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentLength + "\n" + ContentMD5 + "\n" | ||
+ ContentType + "\n" + Date + "\n" + IfModifiedSince + "\n" + IfMatch + "\n" + IfNoneMatch + "\n" | ||
+ IfUnmodifiedSince + "\n" + Range + "\n" + CanonicalizedHeaders + CanonicalizedResource; | ||
} | ||
} |
Oops, something went wrong.