2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.folderwatcher.internal.api.auth;
16 import java.util.Date;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.folderwatcher.internal.api.util.BinaryUtils;
23 * The {@link AWS4SignerForAuthorizationHeader} class contains methods for AWS S3 API authentication using HTTP(S)
26 * Based on offical AWS example {@see https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-examples-using-sdks.html}
28 * @author Alexandr Salamatov - Initial contribution
31 public class AWS4SignerForAuthorizationHeader extends AWS4SignerBase {
33 public AWS4SignerForAuthorizationHeader(URL endpointUrl, String httpMethod, String serviceName, String regionName) {
34 super(endpointUrl, httpMethod, serviceName, regionName);
37 public String computeSignature(Map<String, String> headers, Map<String, String> queryParameters, String bodyHash,
38 String awsAccessKey, String awsSecretKey) {
39 Date now = new Date();
40 String dateTimeStamp = dateTimeFormat.format(now);
41 headers.put("x-amz-date", dateTimeStamp);
42 String hostHeader = endpointUrl.getHost();
43 int port = endpointUrl.getPort();
45 hostHeader.concat(":" + Integer.toString(port));
47 headers.put("Host", hostHeader);
49 String canonicalizedHeaderNames = getCanonicalizeHeaderNames(headers);
50 String canonicalizedHeaders = getCanonicalizedHeaderString(headers);
51 String canonicalizedQueryParameters = getCanonicalizedQueryString(queryParameters);
52 String canonicalRequest = getCanonicalRequest(endpointUrl, httpMethod, canonicalizedQueryParameters,
53 canonicalizedHeaderNames, canonicalizedHeaders, bodyHash);
54 String dateStamp = dateStampFormat.format(now);
55 String scope = dateStamp + "/" + regionName + "/" + serviceName + "/" + TERMINATOR;
56 String stringToSign = getStringToSign(SCHEME, ALGORITHM, dateTimeStamp, scope, canonicalRequest);
57 byte[] kSecret = (SCHEME + awsSecretKey).getBytes();
58 byte[] kDate = sign(dateStamp, kSecret, "HmacSHA256");
59 byte[] kRegion = sign(regionName, kDate, "HmacSHA256");
60 byte[] kService = sign(serviceName, kRegion, "HmacSHA256");
61 byte[] kSigning = sign(TERMINATOR, kService, "HmacSHA256");
62 byte[] signature = sign(stringToSign, kSigning, "HmacSHA256");
63 String credentialsAuthorizationHeader = "Credential=" + awsAccessKey + "/" + scope;
64 String signedHeadersAuthorizationHeader = "SignedHeaders=" + canonicalizedHeaderNames;
65 String signatureAuthorizationHeader = "Signature=" + BinaryUtils.toHex(signature);
66 return SCHEME + "-" + ALGORITHM + " " + credentialsAuthorizationHeader + ", " + signedHeadersAuthorizationHeader
67 + ", " + signatureAuthorizationHeader;