diff --git a/README.md b/README.md index ca0b9b1..0c20700 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,3 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform ## License This project is licensed under the Apache-2.0 License. - diff --git a/clients/sellingpartner-api-aa-csharp/.gitignore b/clients/sellingpartner-api-aa-csharp/.gitignore new file mode 100644 index 0000000..d4e324f --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/.gitignore @@ -0,0 +1,10 @@ +#Ignore bin directories +bin +**/bin +#Ignore obj directories +obj +**/obj +#ignore .vs directories +.vs +#misc +.DS_Store diff --git a/clients/sellingpartner-api-aa-csharp/README.md b/clients/sellingpartner-api-aa-csharp/README.md new file mode 100644 index 0000000..a58a97c --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/README.md @@ -0,0 +1,96 @@ +# Selling Partner API Authentication/Authorization Library +This library provides helper classes for use when signing HTTP requests for Amazon Selling Partner APIs. It is intended for use +with the Selling Partner API Client Libraries generated by [swagger codegen](https://swagger.io/tools/swagger-codegen/) +using the RestSharp library. It can also be integrated into custom projects. + +## LWAAuthorizationSigner +Obtains and signs a request with an access token from LWA (Login with Amazon) for the specified endpoint using the provided LWA credentials. + +*Example* +``` +using RestSharp; +using Amazon.SellingPartnerAPIAA; + +string resource = "/my/api/path"; +RestClient restClient = new RestClient("https://..."); +IRestRequest restRequest = new RestRequest(resource, Method.GET); + +// Seller APIs +LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials +{ + ClientId = "...", + ClientSecret = "", + RefreshToken = "", + Endpoint = new Uri("...") +}; + +/* Sellerless APIs +The Selling Partner API scopes can be retrieved from the ScopeConstants class and used to specify a list of scopes of an LWAAuthorizationCredentials instance. */ +LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials +{ + ClientId = "...", + ClientSecret = "", + Scopes = new List() { ScopeConstants.ScopeNotificationsAPI, ScopeConstants.ScopeMigrationAPI } + Endpoint = new Uri("...") +}; + +restRequest = new LWAAuthorizationSigner(lwaAuthorizationCredentials).Sign(restRequest); +``` +Note the IRestRequest reference is treated as **mutable** when signed. + +## AWSSigV4Signer +Signs a request with [AWS Signature Version 4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) +using the provided AWS developer account credentials. + +*Example* +``` +using RestSharp; +using Amazon.SellingPartnerAPIAA; + +string resource = "/my/api/path"; +RestClient restClient = new RestClient("https://..."); +IRestRequest restRequest = new RestRequest(resource, Method.GET); + +AWSAuthenticationCredentials awsAuthenticationCredentials = new AWSAuthenticationCredentials +{ + AccessKeyId = "..." + SecretKey = "..." + Region = "..." +}; + +restRequest = new AWSSigV4Signer(awsAuthenticationCredentials) + .Sign(restRequest, restClient.BaseUrl.Host); +``` +Note the IRestRequest reference is treated as **mutable** when signed. + +## Resources +This package features Mustache templates designed for use with [swagger codegen](https://swagger.io/tools/swagger-codegen/). +When you build Selling Partner API Swagger models with these templates, they help generate a rich SDK with functionality to invoke Selling Partner APIs built in. The templates are located in *resources/swagger-codegen*. + +## Building +This package is built as a .NET Standard Library via a Visual Studio Solution with implementation and test projects. The Visual Studio Community Edition can be obtained for free from Microsoft and used to build the solution and generate a .dll assembly which can be imported into other projects. + +## Dependencies +All dependencies can be installed via NuGet +- RestSharp - 105.1.0 +- Newtonsoft.Json 12.0.3 +- NETStandard.Library 2.0.3 (platform-specific implementation requirements are documented on the [Microsoft .NET Guide](https://docs.microsoft.com/en-us/dotnet/standard/net-standard)) + +## License +Swagger Codegen templates are subject to the [Swagger Codegen License](https://github.com/swagger-api/swagger-codegen#license). + +All other work licensed as follows: + +Copyright 2020 Amazon.com, Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this library except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/clients/sellingpartner-api-aa-csharp/SellingPartnerAPIAuthAndAuthCSharp.sln b/clients/sellingpartner-api-aa-csharp/SellingPartnerAPIAuthAndAuthCSharp.sln new file mode 100644 index 0000000..bdbc902 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/SellingPartnerAPIAuthAndAuthCSharp.sln @@ -0,0 +1,23 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Amazon.SellingPartnerAPIAA", "src\Amazon.SellingPartnerAPIAA\Amazon.SellingPartnerAPIAA.csproj", "{64339397-3AAB-49D3-8B50-7A467B16D545}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Amazon.SellingPartnerAPIAATests", "test\Amazon.SellingPartnerAPIAATests\Amazon.SellingPartnerAPIAATests.csproj", "{12B130EB-1087-4F88-BDFA-3088868C0A46}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {64339397-3AAB-49D3-8B50-7A467B16D545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64339397-3AAB-49D3-8B50-7A467B16D545}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64339397-3AAB-49D3-8B50-7A467B16D545}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64339397-3AAB-49D3-8B50-7A467B16D545}.Release|Any CPU.Build.0 = Release|Any CPU + {12B130EB-1087-4F88-BDFA-3088868C0A46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12B130EB-1087-4F88-BDFA-3088868C0A46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12B130EB-1087-4F88-BDFA-3088868C0A46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12B130EB-1087-4F88-BDFA-3088868C0A46}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSAuthenticationCredentials.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSAuthenticationCredentials.cs new file mode 100644 index 0000000..b415738 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSAuthenticationCredentials.cs @@ -0,0 +1,20 @@ +namespace Amazon.SellingPartnerAPIAA +{ + public class AWSAuthenticationCredentials + { + /** + * AWS IAM User Access Key Id + */ + public string AccessKeyId { get; set; } + + /** + * AWS IAM User Secret Key + */ + public string SecretKey { get; set; } + + /** + * AWS Region + */ + public string Region { get; set; } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSigV4Signer.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSigV4Signer.cs new file mode 100644 index 0000000..9f7e3db --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSigV4Signer.cs @@ -0,0 +1,82 @@ +using System; +using System.Text; +using RestSharp; + +namespace Amazon.SellingPartnerAPIAA +{ + public class AWSSigV4Signer + + { + public virtual AWSSignerHelper AwsSignerHelper { get; set; } + private AWSAuthenticationCredentials awsCredentials; + + /// + /// Constructor for AWSSigV4Signer + /// + /// AWS Developer Account Credentials + public AWSSigV4Signer(AWSAuthenticationCredentials awsAuthenticationCredentials) + { + awsCredentials = awsAuthenticationCredentials; + AwsSignerHelper = new AWSSignerHelper(); + } + + /// + /// Signs a Request with AWS Signature Version 4 + /// + /// RestRequest which needs to be signed + /// Request endpoint + /// RestRequest with AWS Signature + public IRestRequest Sign(IRestRequest request, string host) + { + DateTime signingDate = AwsSignerHelper.InitializeHeaders(request, host); + string signedHeaders = AwsSignerHelper.ExtractSignedHeaders(request); + + string hashedCanonicalRequest = CreateCanonicalRequest(request, signedHeaders); + + string stringToSign = AwsSignerHelper.BuildStringToSign(signingDate, + hashedCanonicalRequest, + awsCredentials.Region); + + string signature = AwsSignerHelper.CalculateSignature(stringToSign, + signingDate, + awsCredentials.SecretKey, + awsCredentials.Region); + + AwsSignerHelper.AddSignature(request, + awsCredentials.AccessKeyId, + signedHeaders, + signature, + awsCredentials.Region, + signingDate); + + return request; + } + + private string CreateCanonicalRequest(IRestRequest restRequest, string signedHeaders) + { + var canonicalizedRequest = new StringBuilder(); + //Request Method + canonicalizedRequest.AppendFormat("{0}\n", restRequest.Method); + + //CanonicalURI + canonicalizedRequest.AppendFormat("{0}\n", AwsSignerHelper.ExtractCanonicalURIParameters(restRequest.Resource)); + + //CanonicalQueryString + canonicalizedRequest.AppendFormat("{0}\n", AwsSignerHelper.ExtractCanonicalQueryString(restRequest)); + + //CanonicalHeaders + canonicalizedRequest.AppendFormat("{0}\n", AwsSignerHelper.ExtractCanonicalHeaders(restRequest)); + + //SignedHeaders + canonicalizedRequest.AppendFormat("{0}\n", signedHeaders); + + // Hash(digest) the payload in the body + canonicalizedRequest.AppendFormat(AwsSignerHelper.HashRequestBody(restRequest)); + + string canonicalRequest = canonicalizedRequest.ToString(); + + //Create a digest(hash) of the canonical request + return Utils.ToHex(Utils.Hash(canonicalRequest)); + } + } +} \ No newline at end of file diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSignerHelper.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSignerHelper.cs new file mode 100644 index 0000000..5a2f7ef --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/AWSSignerHelper.cs @@ -0,0 +1,251 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using RestSharp; +using System.Text.RegularExpressions; +using System.Globalization; + +namespace Amazon.SellingPartnerAPIAA +{ + public class AWSSignerHelper + { + public const string ISO8601BasicDateTimeFormat = "yyyyMMddTHHmmssZ"; + public const string ISO8601BasicDateFormat = "yyyyMMdd"; + + public const string XAmzDateHeaderName = "X-Amz-Date"; + public const string AuthorizationHeaderName = "Authorization"; + public const string CredentialSubHeaderName = "Credential"; + public const string SignatureSubHeaderName = "Signature"; + public const string SignedHeadersSubHeaderName = "SignedHeaders"; + public const string HostHeaderName = "host"; + + public const string Scheme = "AWS4"; + public const string Algorithm = "HMAC-SHA256"; + public const string TerminationString = "aws4_request"; + public const string ServiceName = "execute-api"; + public const string Slash = "/"; + + private readonly static Regex CompressWhitespaceRegex = new Regex("\\s+"); + + public virtual IDateHelper DateHelper { get; set; } + + public AWSSignerHelper() + { + DateHelper = new SigningDateHelper(); + } + + /// + /// Returns URI encoded version of absolute path + /// + /// Resource path(absolute path) from the request + /// URI encoded version of absolute path + public virtual string ExtractCanonicalURIParameters(string resource) + { + string canonicalUri = string.Empty; + + if (string.IsNullOrEmpty(resource)) + { + canonicalUri = Slash; + } + else + { + if (!resource.StartsWith(Slash)) + { + canonicalUri = Slash; + } + //Split path at / into segments + IEnumerable encodedSegments = resource.Split(new char[] { '/' }, StringSplitOptions.None); + + // Encode twice + encodedSegments = encodedSegments.Select(segment => Utils.UrlEncode(segment)); + encodedSegments = encodedSegments.Select(segment => Utils.UrlEncode(segment)); + + canonicalUri += string.Join(Slash, encodedSegments.ToArray()); + } + + return canonicalUri; + } + + /// + /// Returns query parameters in canonical order with URL encoding + /// + /// RestRequest + /// Query parameters in canonical order with URL encoding + public virtual string ExtractCanonicalQueryString(IRestRequest request) + { + IDictionary queryParameters = request.Parameters + .Where(parameter => ParameterType.QueryString.Equals(parameter.Type)) + .ToDictionary(header => header.Name.Trim().ToString(), header => header.Value.ToString()); + + SortedDictionary sortedqueryParameters = new SortedDictionary(queryParameters); + + StringBuilder canonicalQueryString = new StringBuilder(); + foreach (var key in sortedqueryParameters.Keys) + { + if (canonicalQueryString.Length > 0) + { + canonicalQueryString.Append("&"); + } + canonicalQueryString.AppendFormat("{0}={1}", + Utils.UrlEncode(key), + Utils.UrlEncode(sortedqueryParameters[key])); + } + + return canonicalQueryString.ToString(); + } + + /// + /// Returns Http headers in canonical order with all header names to lowercase + /// + /// RestRequest + /// Returns Http headers in canonical order + public virtual string ExtractCanonicalHeaders(IRestRequest request) + { + IDictionary headers = request.Parameters + .Where(parameter => ParameterType.HttpHeader.Equals(parameter.Type)) + .ToDictionary(header => header.Name.Trim().ToLowerInvariant(), header => header.Value.ToString()); + + SortedDictionary sortedHeaders = new SortedDictionary(headers); + + StringBuilder headerString = new StringBuilder(); + + foreach (string headerName in sortedHeaders.Keys) + { + headerString.AppendFormat("{0}:{1}\n", + headerName, + CompressWhitespaceRegex.Replace(sortedHeaders[headerName].Trim(), " ")); + } + + return headerString.ToString(); + } + + /// + /// Returns list(as string) of Http headers in canonical order + /// + /// RestRequest + /// List of Http headers in canonical order + public virtual string ExtractSignedHeaders(IRestRequest request) + { + List rawHeaders = request.Parameters.Where(parameter => ParameterType.HttpHeader.Equals(parameter.Type)) + .Select(header => header.Name.Trim().ToLowerInvariant()) + .ToList(); + rawHeaders.Sort(StringComparer.OrdinalIgnoreCase); + + return string.Join(";", rawHeaders); + } + + /// + /// Returns hexadecimal hashed value(using SHA256) of payload in the body of request + /// + /// RestRequest + /// Hexadecimal hashed value of payload in the body of request + public virtual string HashRequestBody(IRestRequest request) + { + Parameter body = request.Parameters.FirstOrDefault(parameter => ParameterType.RequestBody.Equals(parameter.Type)); + string value = body != null ? body.Value.ToString() : string.Empty; + return Utils.ToHex(Utils.Hash(value)); + } + + /// + /// Builds the string for signing using signing date, hashed canonical request and region + /// + /// Signing Date + /// Hashed Canonical Request + /// Region + /// String to be used for signing + public virtual string BuildStringToSign(DateTime signingDate, string hashedCanonicalRequest, string region) + { + string scope = BuildScope(signingDate, region); + string stringToSign = string.Format(CultureInfo.InvariantCulture, "{0}-{1}\n{2}\n{3}\n{4}", + Scheme, + Algorithm, + signingDate.ToString(ISO8601BasicDateTimeFormat, CultureInfo.InvariantCulture), + scope, + hashedCanonicalRequest); + + return stringToSign; + } + + /// + /// Sets AWS4 mandated 'x-amz-date' header, returning the date/time that will + /// be used throughout the signing process. + /// + /// RestRequest + /// Request endpoint + /// Date and time used for x-amz-date, in UTC + public virtual DateTime InitializeHeaders(IRestRequest restRequest, string host) + { + restRequest.Parameters.RemoveAll(parameter => ParameterType.HttpHeader.Equals(parameter.Type) + && parameter.Name == XAmzDateHeaderName); + restRequest.Parameters.RemoveAll(parameter => ParameterType.HttpHeader.Equals(parameter.Type) + && parameter.Name == HostHeaderName); + + DateTime signingDate = DateHelper.GetUtcNow(); + + restRequest.AddHeader(XAmzDateHeaderName, signingDate.ToString(ISO8601BasicDateTimeFormat, CultureInfo.InvariantCulture)); + restRequest.AddHeader(HostHeaderName, host); + + return signingDate; + } + + /// + /// Calculates AWS4 signature for the string, prepared for signing + /// + /// String to be signed + /// Signing Date + /// Secret Key + /// Region + /// AWS4 Signature + public virtual string CalculateSignature(string stringToSign, + DateTime signingDate, + string secretKey, + string region) + { + string date = signingDate.ToString(ISO8601BasicDateFormat, CultureInfo.InvariantCulture); + byte[] kSecret = Encoding.UTF8.GetBytes(Scheme + secretKey); + byte[] kDate = Utils.GetKeyedHash(kSecret, date); + byte[] kRegion = Utils.GetKeyedHash(kDate, region); + byte[] kService = Utils.GetKeyedHash(kRegion, ServiceName); + byte[] kSigning = Utils.GetKeyedHash(kService, TerminationString); + + // Calculate the signature + return Utils.ToHex(Utils.GetKeyedHash(kSigning, stringToSign)); + } + + /// + /// Add a signature to a request in the form of an 'Authorization' header + /// + /// Request to be signed + /// Access Key Id + /// Signed Headers + /// The signature to add + /// AWS region for the request + /// Signature date + public virtual void AddSignature(IRestRequest restRequest, + string accessKeyId, + string signedHeaders, + string signature, + string region, + DateTime signingDate) + { + string scope = BuildScope(signingDate, region); + StringBuilder authorizationHeaderValueBuilder = new StringBuilder(); + authorizationHeaderValueBuilder.AppendFormat("{0}-{1}", Scheme, Algorithm); + authorizationHeaderValueBuilder.AppendFormat(" {0}={1}/{2},", CredentialSubHeaderName, accessKeyId, scope); + authorizationHeaderValueBuilder.AppendFormat(" {0}={1},", SignedHeadersSubHeaderName, signedHeaders); + authorizationHeaderValueBuilder.AppendFormat(" {0}={1}", SignatureSubHeaderName, signature); + + restRequest.AddHeader(AuthorizationHeaderName, authorizationHeaderValueBuilder.ToString()); + } + + private static string BuildScope(DateTime signingDate, string region) + { + return string.Format("{0}/{1}/{2}/{3}", + signingDate.ToString(ISO8601BasicDateFormat, CultureInfo.InvariantCulture), + region, + ServiceName, + TerminationString); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Amazon.SellingPartnerAPIAA.csproj b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Amazon.SellingPartnerAPIAA.csproj new file mode 100644 index 0000000..b357409 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Amazon.SellingPartnerAPIAA.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + + + + + + + + + + + + diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/IDateHelper.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/IDateHelper.cs new file mode 100644 index 0000000..175076c --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/IDateHelper.cs @@ -0,0 +1,8 @@ +using System; +namespace Amazon.SellingPartnerAPIAA +{ + public interface IDateHelper + { + DateTime GetUtcNow(); + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMeta.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMeta.cs new file mode 100644 index 0000000..1188272 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMeta.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Amazon.SellingPartnerAPIAA +{ + public class LWAAccessTokenRequestMeta + { + [JsonProperty(PropertyName = "grant_type")] + public string GrantType { get; set; } + + [JsonProperty(PropertyName = "refresh_token")] + public string RefreshToken { get; set; } + + [JsonProperty(PropertyName = "client_id")] + public string ClientId { get; set; } + + [JsonProperty(PropertyName = "client_secret")] + public string ClientSecret { get; set; } + + [JsonProperty(PropertyName = "scope")] + public string Scope { get; set; } + + public override bool Equals(object obj) + { + LWAAccessTokenRequestMeta other = obj as LWAAccessTokenRequestMeta; + + return other != null && + this.GrantType == other.GrantType && + this.RefreshToken == other.RefreshToken && + this.ClientId == other.ClientId && + this.ClientSecret == other.ClientSecret && + this.Scope == other.Scope; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMetaBuilder.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMetaBuilder.cs new file mode 100644 index 0000000..d6e8bcf --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAccessTokenRequestMetaBuilder.cs @@ -0,0 +1,40 @@ +using System.Linq; + +namespace Amazon.SellingPartnerAPIAA +{ + public class LWAAccessTokenRequestMetaBuilder + { + public const string SellerAPIGrantType = "refresh_token"; + public const string SellerlessAPIGrantType = "client_credentials"; + + private const string Delimiter = " "; + + /// + /// Builds an instance of LWAAccessTokenRequestMeta modeling appropriate LWA token + /// request params based on configured LWAAuthorizationCredentials + /// + /// LWA Authorization Credentials + /// + public virtual LWAAccessTokenRequestMeta Build(LWAAuthorizationCredentials lwaAuthorizationCredentials) + { + LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta = new LWAAccessTokenRequestMeta() + { + ClientId = lwaAuthorizationCredentials.ClientId, + ClientSecret = lwaAuthorizationCredentials.ClientSecret, + RefreshToken = lwaAuthorizationCredentials.RefreshToken + }; + + if (lwaAuthorizationCredentials.Scopes == null || lwaAuthorizationCredentials.Scopes.Count == 0) + { + lwaAccessTokenRequestMeta.GrantType = SellerAPIGrantType; + } + else + { + lwaAccessTokenRequestMeta.Scope = string.Join(Delimiter, lwaAuthorizationCredentials.Scopes); + lwaAccessTokenRequestMeta.GrantType = SellerlessAPIGrantType; + } + + return lwaAccessTokenRequestMeta; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationCredentials.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationCredentials.cs new file mode 100644 index 0000000..414dcad --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationCredentials.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace Amazon.SellingPartnerAPIAA +{ + public class LWAAuthorizationCredentials + { + public LWAAuthorizationCredentials() + { + this.Scopes = new List(); + } + + /** + * LWA Client Id + */ + public string ClientId { get; set; } + + /** + * LWA Client Secret + */ + public string ClientSecret { get; set; } + + /** + * LWA Refresh Token + */ + public string RefreshToken { get; set; } + + /** + * LWA Authorization Server Endpoint + */ + public Uri Endpoint { get; set; } + + /** + * LWA Authorization Scopes + */ + public List Scopes { get; set; } + } +} + diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationSigner.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationSigner.cs new file mode 100644 index 0000000..bc64308 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAAuthorizationSigner.cs @@ -0,0 +1,34 @@ +using RestSharp; + +namespace Amazon.SellingPartnerAPIAA +{ + public class LWAAuthorizationSigner + { + public const string AccessTokenHeaderName = "x-amz-access-token"; + + public LWAClient LWAClient { get; set; } + + /// + /// Constructor for LWAAuthorizationSigner + /// + /// LWA Authorization Credentials for token exchange + public LWAAuthorizationSigner(LWAAuthorizationCredentials lwaAuthorizationCredentials) + { + LWAClient = new LWAClient(lwaAuthorizationCredentials); + } + + /// + /// Signs a request with LWA Access Token + /// + /// Request to sign + /// restRequest with LWA signature + public IRestRequest Sign(IRestRequest restRequest) + { + string accessToken = LWAClient.GetAccessToken(); + + restRequest.AddHeader(AccessTokenHeaderName, accessToken); + + return restRequest; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAClient.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAClient.cs new file mode 100644 index 0000000..dfcd4d4 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/LWAClient.cs @@ -0,0 +1,68 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using RestSharp; + +namespace Amazon.SellingPartnerAPIAA +{ + public class LWAClient + { + public const string AccessTokenKey = "access_token"; + public const string JsonMediaType = "application/json; charset=utf-8"; + + public IRestClient RestClient { get; set; } + public LWAAccessTokenRequestMetaBuilder LWAAccessTokenRequestMetaBuilder { get; set; } + public LWAAuthorizationCredentials LWAAuthorizationCredentials { get; private set; } + + + public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials) + { + LWAAuthorizationCredentials = lwaAuthorizationCredentials; + LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder(); + RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(System.UriPartial.Authority)); + } + + /// + /// Retrieves access token from LWA + /// + /// LWA AccessTokenRequest metadata + /// LWA Access Token + public virtual string GetAccessToken() + { + LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta = LWAAccessTokenRequestMetaBuilder.Build(LWAAuthorizationCredentials); + var accessTokenRequest = new RestRequest(LWAAuthorizationCredentials.Endpoint.AbsolutePath, Method.POST); + + string jsonRequestBody = JsonConvert.SerializeObject(lwaAccessTokenRequestMeta); + + accessTokenRequest.AddParameter(JsonMediaType, jsonRequestBody, ParameterType.RequestBody); + + string accessToken; + try + { + var response = RestClient.Execute(accessTokenRequest); + + if (!IsSuccessful(response)) + { + throw new IOException("Unsuccessful LWA token exchange", response.ErrorException); + } + + JObject responseJson = JObject.Parse(response.Content); + + accessToken = responseJson.GetValue(AccessTokenKey).ToString(); + } + catch (Exception e) + { + throw new SystemException("Error getting LWA Access Token", e); + } + + return accessToken; + } + + private bool IsSuccessful(IRestResponse response) + { + int statusCode = (int)response.StatusCode; + return statusCode >= 200 && statusCode <= 299 && response.ResponseStatus == ResponseStatus.Completed; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/ScopeConstants.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/ScopeConstants.cs new file mode 100644 index 0000000..c9cf798 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/ScopeConstants.cs @@ -0,0 +1,8 @@ +namespace Amazon.SellingPartnerAPIAA +{ + public class ScopeConstants + { + public const string ScopeNotificationsAPI = "sellingpartnerapi::notifications"; + public const string ScopeMigrationAPI = "sellingpartnerapi::migration"; + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/SigningDateHelper.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/SigningDateHelper.cs new file mode 100644 index 0000000..cc77d75 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/SigningDateHelper.cs @@ -0,0 +1,11 @@ +using System; +namespace Amazon.SellingPartnerAPIAA +{ + public class SigningDateHelper : IDateHelper + { + public DateTime GetUtcNow() + { + return DateTime.UtcNow; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Utils.cs b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Utils.cs new file mode 100644 index 0000000..a96f9ad --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/Utils.cs @@ -0,0 +1,73 @@ +using System.Text; +using System.Security.Cryptography; +using System.Globalization; + +namespace Amazon.SellingPartnerAPIAA +{ + public static class Utils + { + public const string ValidUrlCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; + + /// + /// Returns URL encoded version of input data according to RFC-3986 + /// + /// String to be URL-encoded + /// URL encoded version of input data + public static string UrlEncode(string data) + { + StringBuilder encoded = new StringBuilder(); + foreach (char symbol in Encoding.UTF8.GetBytes(data)) + { + if (ValidUrlCharacters.IndexOf(symbol) != -1) + { + encoded.Append(symbol); + } + else + { + encoded.Append("%").Append(string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)symbol)); + } + } + return encoded.ToString(); + } + + /// + /// Returns hashed value of input data using SHA256 + /// + /// String to be hashed + /// Hashed value of input data + public static byte[] Hash(string data) + { + return new SHA256CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(data)); + } + + /// + /// Returns lowercase hexadecimal string of input byte array + /// + /// Data to be converted + /// Lowercase hexadecimal string + public static string ToHex(byte[] data) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < data.Length; i++) + { + sb.Append(data[i].ToString("x2", CultureInfo.InvariantCulture)); + } + + return sb.ToString(); + } + + /// + /// Computes the hash of given string using the specified key with HMACSHA256 + /// + /// Key + /// String to be hashed + /// Hashed value of input data + public static byte[] GetKeyedHash(byte[] key, string value) + { + KeyedHashAlgorithm hashAlgorithm = new HMACSHA256(key); + hashAlgorithm.Initialize(); + return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(value)); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/ApiClient.mustache b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/ApiClient.mustache new file mode 100644 index 0000000..3a98a34 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/ApiClient.mustache @@ -0,0 +1,576 @@ +{{>partial_header}} +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Text.RegularExpressions; +using System.IO; +{{^netStandard}} +{{^supportsUWP}} +using System.Web; +{{/supportsUWP}} +{{/netStandard}} +using System.Linq; +using System.Net; +using System.Text; +using Newtonsoft.Json; +{{#netStandard}} +using RestSharp.Portable; +using RestSharp.Portable.HttpClient; +{{/netStandard}} +{{^netStandard}} +using RestSharp; +{{/netStandard}} +using Amazon.SellingPartnerAPIAA; + +namespace {{packageName}}.Client +{ + /// + /// API client is mainly responsible for making the HTTP call to the API backend. + /// + {{>visibility}} partial class ApiClient + { + private LWAAuthorizationSigner lwaAuthorizationSigner; + private AWSSigV4Signer awsSigV4Signer; + + private JsonSerializerSettings serializerSettings = new JsonSerializerSettings + { + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor + }; + + /// + /// Allows for extending request processing for generated code. + /// + /// The RestSharp request object + private void InterceptRequest(IRestRequest request) + { + lwaAuthorizationSigner.Sign(request); + awsSigV4Signer.Sign(request, RestClient.BaseUrl.Host); + } + + /// + /// Allows for extending response processing for generated code. + /// + /// The RestSharp request object + /// The RestSharp response object + partial void InterceptResponse(IRestRequest request, IRestResponse response); + + /// + /// Initializes a new instance of the class + /// with default base path ({{{basePath}}}). + /// + /// An instance of Configuration. + public ApiClient(Configuration config) + { + Configuration = config ?? {{packageName}}.Client.Configuration.Default; + + RestClient = new RestClient(Configuration.BasePath); + {{#netStandard}} + RestClient.IgnoreResponseStatusCode = true; + {{/netStandard}} + + lwaAuthorizationSigner = new LWAAuthorizationSigner(Configuration.AuthorizationCredentials); + awsSigV4Signer = new AWSSigV4Signer(Configuration.AuthenticationCredentials); + } + + /// + /// Gets or sets the default API client for making HTTP calls. + /// + /// The default API client. + [Obsolete("ApiClient.Default is deprecated, please use 'Configuration.Default.ApiClient' instead.")] + public static ApiClient Default; + + /// + /// Gets or sets an instance of the IReadableConfiguration. + /// + /// An instance of the IReadableConfiguration. + /// + /// helps us to avoid modifying possibly global + /// configuration values from within a given client. It does not guarantee thread-safety + /// of the instance in any way. + /// + public IReadableConfiguration Configuration { get; set; } + + /// + /// Gets or sets the RestClient. + /// + /// An instance of the RestClient + public RestClient RestClient { get; set; } + + // Creates and sets up a RestRequest prior to a call. + private RestRequest PrepareRequest( + String path, {{^netStandard}}RestSharp.{{/netStandard}}Method method, List> queryParams, Object postBody, + Dictionary headerParams, Dictionary formParams, + Dictionary fileParams, Dictionary pathParams, + String contentType) + { + var request = new RestRequest(path, method); + {{#netStandard}} + // disable ResetSharp.Portable built-in serialization + request.Serializer = null; + {{/netStandard}} + + // add path parameter, if any + foreach(var param in pathParams) + request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment); + + // add header parameter, if any + foreach(var param in headerParams) + request.AddHeader(param.Key, param.Value); + + // add query parameter, if any + foreach(var param in queryParams) + request.AddQueryParameter(param.Key, param.Value); + + // add form parameter, if any + foreach(var param in formParams) + request.AddParameter(param.Key, param.Value); + + // add file parameter, if any + foreach(var param in fileParams) + { + {{#netStandard}} + request.AddFile(param.Value); + {{/netStandard}} + {{^netStandard}} + {{^supportsUWP}} + request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType); + {{/supportsUWP}} + {{#supportsUWP}} + byte[] paramWriter = null; + param.Value.Writer = delegate (Stream stream) { paramWriter = ToByteArray(stream); }; + request.AddFile(param.Value.Name, paramWriter, param.Value.FileName, param.Value.ContentType); + {{/supportsUWP}} + {{/netStandard}} + } + + if (postBody != null) // http body (model or byte[]) parameter + { + {{#netStandard}} + request.AddParameter(new Parameter { Value = postBody, Type = ParameterType.RequestBody, ContentType = contentType }); + {{/netStandard}} + {{^netStandard}} + request.AddParameter(contentType, postBody, ParameterType.RequestBody); + {{/netStandard}} + } + + return request; + } + + /// + /// Makes the HTTP request (Sync). + /// + /// URL path. + /// HTTP method. + /// Query parameters. + /// HTTP body (POST request). + /// Header parameters. + /// Form parameters. + /// File parameters. + /// Path parameters. + /// Content Type of the request + /// Object + public Object CallApi( + String path, {{^netStandard}}RestSharp.{{/netStandard}}Method method, List> queryParams, Object postBody, + Dictionary headerParams, Dictionary formParams, + Dictionary fileParams, Dictionary pathParams, + String contentType) + { + var request = PrepareRequest( + path, method, queryParams, postBody, headerParams, formParams, fileParams, + pathParams, contentType); + + // set timeout + {{#netStandard}}RestClient.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);{{/netStandard}} + {{^netStandard}}RestClient.Timeout = Configuration.Timeout;{{/netStandard}} + // set user agent + RestClient.UserAgent = Configuration.UserAgent; + + InterceptRequest(request); + {{#netStandard}} + var response = RestClient.Execute(request).Result; + {{/netStandard}} + {{^netStandard}} + {{^supportsUWP}} + var response = RestClient.Execute(request); + {{/supportsUWP}} + {{#supportsUWP}} + // Using async method to perform sync call (uwp-only) + var response = RestClient.ExecuteTaskAsync(request).Result; + {{/supportsUWP}} + {{/netStandard}} + InterceptResponse(request, response); + + return (Object) response; + } + {{#supportsAsync}} + /// + /// Makes the asynchronous HTTP request. + /// + /// URL path. + /// HTTP method. + /// Query parameters. + /// HTTP body (POST request). + /// Header parameters. + /// Form parameters. + /// File parameters. + /// Path parameters. + /// Content type. + /// The Task instance. + public async System.Threading.Tasks.Task CallApiAsync( + String path, {{^netStandard}}RestSharp.{{/netStandard}}Method method, List> queryParams, Object postBody, + Dictionary headerParams, Dictionary formParams, + Dictionary fileParams, Dictionary pathParams, + String contentType) + { + var request = PrepareRequest( + path, method, queryParams, postBody, headerParams, formParams, fileParams, + pathParams, contentType); + InterceptRequest(request); + var response = await RestClient.Execute{{^netStandard}}TaskAsync{{/netStandard}}(request); + InterceptResponse(request, response); + return (Object)response; + }{{/supportsAsync}} + + /// + /// Escape string (url-encoded). + /// + /// String to be escaped. + /// Escaped string. + public string EscapeString(string str) + { + return UrlEncode(str); + } + + /// + /// Create FileParameter based on Stream. + /// + /// Parameter name. + /// Input stream. + /// FileParameter. + public FileParameter ParameterToFile(string name, Stream stream) + { + if (stream is FileStream) + return FileParameter.Create(name, ReadAsBytes(stream), Path.GetFileName(((FileStream)stream).Name)); + else + return FileParameter.Create(name, ReadAsBytes(stream), "no_file_name_provided"); + } + + /// + /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime. + /// If parameter is a list, join the list with ",". + /// Otherwise just return the string. + /// + /// The parameter (header, path, query, form). + /// Formatted string. + public string ParameterToString(object obj) + { + if (obj is DateTime) + // Return a formatted date string - Can be customized with Configuration.DateTimeFormat + // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") + // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 + // For example: 2009-06-15T13:45:30.0000000 + return ((DateTime)obj).ToString (Configuration.DateTimeFormat); + else if (obj is DateTimeOffset) + // Return a formatted date string - Can be customized with Configuration.DateTimeFormat + // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") + // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 + // For example: 2009-06-15T13:45:30.0000000 + return ((DateTimeOffset)obj).ToString (Configuration.DateTimeFormat); + else if (obj is IList) + { + var flattenedString = new StringBuilder(); + foreach (var param in (IList)obj) + { + if (flattenedString.Length > 0) + flattenedString.Append(","); + flattenedString.Append(param); + } + return flattenedString.ToString(); + } + else + return Convert.ToString (obj); + } + + /// + /// Deserialize the JSON string into a proper object. + /// + /// The HTTP response. + /// Object type. + /// Object representation of the JSON string. + public object Deserialize(IRestResponse response, Type type) + { + {{^netStandard}}IList{{/netStandard}}{{#netStandard}}IHttpHeaders{{/netStandard}} headers = response.Headers; + if (type == typeof(byte[])) // return byte array + { + return response.RawBytes; + } + + // TODO: ? if (type.IsAssignableFrom(typeof(Stream))) + if (type == typeof(Stream)) + { + if (headers != null) + { + var filePath = String.IsNullOrEmpty(Configuration.TempFolderPath) + ? Path.GetTempPath() + : Configuration.TempFolderPath; + var regex = new Regex(@"Content-Disposition=.*filename=['""]?([^'""\s]+)['""]?$"); + foreach (var header in headers) + { + var match = regex.Match(header.ToString()); + if (match.Success) + { + string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", "")); + File.WriteAllBytes(fileName, response.RawBytes); + return new FileStream(fileName, FileMode.Open); + } + } + } + var stream = new MemoryStream(response.RawBytes); + return stream; + } + + if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object + { + return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind); + } + + if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type + { + return ConvertType(response.Content, type); + } + + // at this point, it must be a model (json) + try + { + return JsonConvert.DeserializeObject(response.Content, type, serializerSettings); + } + catch (Exception e) + { + throw new ApiException(500, e.Message); + } + } + + /// + /// Serialize an input (model) into JSON string + /// + /// Object. + /// JSON string. + public String Serialize(object obj) + { + try + { + return obj != null ? JsonConvert.SerializeObject(obj) : null; + } + catch (Exception e) + { + throw new ApiException(500, e.Message); + } + } + + /// + ///Check if the given MIME is a JSON MIME. + ///JSON MIME examples: + /// application/json + /// application/json; charset=UTF8 + /// APPLICATION/JSON + /// application/vnd.company+json + /// + /// MIME + /// Returns True if MIME type is json. + public bool IsJsonMime(String mime) + { + var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$"); + return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json")); + } + + /// + /// Select the Content-Type header's value from the given content-type array: + /// if JSON type exists in the given array, use it; + /// otherwise use the first one defined in 'consumes' + /// + /// The Content-Type array to select from. + /// The Content-Type header to use. + public String SelectHeaderContentType(String[] contentTypes) + { + if (contentTypes.Length == 0) + return "application/json"; + + foreach (var contentType in contentTypes) + { + if (IsJsonMime(contentType.ToLower())) + return contentType; + } + + return contentTypes[0]; // use the first content type specified in 'consumes' + } + + /// + /// Select the Accept header's value from the given accepts array: + /// if JSON exists in the given array, use it; + /// otherwise use all of them (joining into a string) + /// + /// The accepts array to select from. + /// The Accept header to use. + public String SelectHeaderAccept(String[] accepts) + { + if (accepts.Length == 0) + return null; + + if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase)) + return "application/json"; + + return String.Join(",", accepts); + } + + /// + /// Encode string in base64 format. + /// + /// String to be encoded. + /// Encoded string. + public static string Base64Encode(string text) + { + return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); + } + + /// + /// Dynamically cast the object into target type. + /// + /// Object to be casted + /// Target type + /// Casted object + {{#supportsAsync}} + public static dynamic ConvertType(dynamic fromObject, Type toObject) + {{/supportsAsync}} + {{^supportsAsync}} + public static object ConvertType(T fromObject, Type toObject) where T : class + {{/supportsAsync}} + { + return Convert.ChangeType(fromObject, toObject); + } + + /// + /// Convert stream to byte array + /// + /// Input stream to be converted + /// Byte array + public static byte[] ReadAsBytes(Stream inputStream) + { + byte[] buf = new byte[16*1024]; + using (MemoryStream ms = new MemoryStream()) + { + int count; + while ((count = inputStream.Read(buf, 0, buf.Length)) > 0) + { + ms.Write(buf, 0, count); + } + return ms.ToArray(); + } + } + + /// + /// URL encode a string + /// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50 + /// + /// String to be URL encoded + /// Byte array + public static string UrlEncode(string input) + { + const int maxLength = 32766; + + if (input == null) + { + throw new ArgumentNullException("input"); + } + + if (input.Length <= maxLength) + { + return Uri.EscapeDataString(input); + } + + StringBuilder sb = new StringBuilder(input.Length * 2); + int index = 0; + + while (index < input.Length) + { + int length = Math.Min(input.Length - index, maxLength); + string subString = input.Substring(index, length); + + sb.Append(Uri.EscapeDataString(subString)); + index += subString.Length; + } + + return sb.ToString(); + } + + /// + /// Sanitize filename by removing the path + /// + /// Filename + /// Filename + public static string SanitizeFilename(string filename) + { + Match match = Regex.Match(filename, @".*[/\\](.*)$"); + + if (match.Success) + { + return match.Groups[1].Value; + } + else + { + return filename; + } + } + {{^netStandard}} + {{#supportsUWP}} + /// + /// Convert stream to byte array + /// + /// IO stream + /// Byte array + public static byte[] ToByteArray(Stream stream) + { + stream.Position = 0; + byte[] buffer = new byte[stream.Length]; + for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;) + totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied); + return buffer; + } + {{/supportsUWP}} + {{/netStandard}} + + /// + /// Convert params to key/value pairs. + /// Use collectionFormat to properly format lists and collections. + /// + /// Key name. + /// Value object. + /// A list of KeyValuePairs + public IEnumerable> ParameterToKeyValuePairs(string collectionFormat, string name, object value) + { + var parameters = new List>(); + + if (IsCollection(value) && collectionFormat == "multi") + { + var valueCollection = value as IEnumerable; + parameters.AddRange(from object item in valueCollection select new KeyValuePair(name, ParameterToString(item))); + } + else + { + parameters.Add(new KeyValuePair(name, ParameterToString(value))); + } + + return parameters; + } + + /// + /// Check if generic object is a collection. + /// + /// + /// True if object is a collection type + private static bool IsCollection(object value) + { + return value is IList || value is ICollection; + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/Configuration.mustache b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/Configuration.mustache new file mode 100644 index 0000000..77b0154 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/Configuration.mustache @@ -0,0 +1,464 @@ +{{>partial_header}} +using System; +using System.Reflection; +{{^net35}} +using System.Collections.Concurrent; +{{/net35}} +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Amazon.SellingPartnerAPIAA; + +namespace {{packageName}}.Client +{ + /// + /// Represents a set of configuration settings + /// + {{>visibility}} class Configuration : IReadableConfiguration + { + #region Constants + + /// + /// Version of the package. + /// + /// Version of the package. + public const string Version = "{{packageVersion}}"; + + /// + /// Identifier for ISO 8601 DateTime Format + /// + /// See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 for more information. + // ReSharper disable once InconsistentNaming + public const string ISO8601_DATETIME_FORMAT = "o"; + + #endregion Constants + + #region Static Members + + private static readonly object GlobalConfigSync = new { }; + private static Configuration _globalConfiguration; + + /// + /// Default creation of exceptions for a given method name and response object + /// + public static readonly ExceptionFactory DefaultExceptionFactory = (methodName, response) => + { + var status = (int)response.StatusCode; + if (status >= 400) + { + return new ApiException(status, + string.Format("Error calling {0}: {1}", methodName, response.Content), + response.Content); + } + {{^netStandard}}if (status == 0) + { + return new ApiException(status, + string.Format("Error calling {0}: {1}", methodName, response.ErrorMessage), response.ErrorMessage); + }{{/netStandard}} + return null; + }; + + /// + /// Gets or sets the default Configuration. + /// + /// Configuration. + public static Configuration Default + { + get { return _globalConfiguration; } + set + { + lock (GlobalConfigSync) + { + _globalConfiguration = value; + } + } + } + + #endregion Static Members + + #region Private Members + + /// + /// Gets or sets the API key based on the authentication name. + /// + /// The API key. + private IDictionary _apiKey = null; + + /// + /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name. + /// + /// The prefix of the API key. + private IDictionary _apiKeyPrefix = null; + + private string _dateTimeFormat = ISO8601_DATETIME_FORMAT; + private string _tempFolderPath = Path.GetTempPath(); + + #endregion Private Members + + #region Constructors + + static Configuration() + { + _globalConfiguration = new GlobalConfiguration(); + } + + /// + /// Initializes a new instance of the class + /// + public Configuration() + { + UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"; + BasePath = "{{{basePath}}}"; + DefaultHeader = new {{^net35}}Concurrent{{/net35}}Dictionary(); + ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary(); + ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary(); + } + + /// + /// Initializes a new instance of the class + /// + public Configuration( + IDictionary defaultHeader, + IDictionary apiKey, + IDictionary apiKeyPrefix, + string basePath = "{{{basePath}}}") : this() + { + if (string.{{^net35}}IsNullOrWhiteSpace{{/net35}}{{#net35}}IsNullOrEmpty{{/net35}}(basePath)) + throw new ArgumentException("The provided basePath is invalid.", "basePath"); + if (defaultHeader == null) + throw new ArgumentNullException("defaultHeader"); + if (apiKey == null) + throw new ArgumentNullException("apiKey"); + if (apiKeyPrefix == null) + throw new ArgumentNullException("apiKeyPrefix"); + + BasePath = basePath; + + foreach (var keyValuePair in defaultHeader) + { + DefaultHeader.Add(keyValuePair); + } + + foreach (var keyValuePair in apiKey) + { + ApiKey.Add(keyValuePair); + } + + foreach (var keyValuePair in apiKeyPrefix) + { + ApiKeyPrefix.Add(keyValuePair); + } + } + + /// + /// Initializes a new instance of the class with different settings + /// + /// Api client + /// Dictionary of default HTTP header + /// Username + /// Password + /// accessToken + /// Dictionary of API key + /// Dictionary of API key prefix + /// Temp folder path + /// DateTime format string + /// HTTP connection timeout (in milliseconds) + /// HTTP user agent + [Obsolete("Use explicit object construction and setting of properties.", true)] + public Configuration( + // ReSharper disable UnusedParameter.Local + ApiClient apiClient = null, + IDictionary defaultHeader = null, + string username = null, + string password = null, + string accessToken = null, + IDictionary apiKey = null, + IDictionary apiKeyPrefix = null, + string tempFolderPath = null, + string dateTimeFormat = null, + int timeout = 100000, + string userAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}" + // ReSharper restore UnusedParameter.Local + ) + { + + } + + /// + /// Initializes a new instance of the Configuration class. + /// + /// Api client. + [Obsolete("This constructor caused unexpected sharing of static data. It is no longer supported.", true)] + // ReSharper disable once UnusedParameter.Local + public Configuration(ApiClient apiClient) + { + + } + + #endregion Constructors + + + #region Properties + + private ApiClient _apiClient = null; + /// + /// Gets an instance of an ApiClient for this configuration + /// + public virtual ApiClient ApiClient + { + get + { + if (_apiClient == null) _apiClient = CreateApiClient(); + return _apiClient; + } + } + + private String _basePath = null; + /// + /// Gets or sets the base path for API access. + /// + public virtual string BasePath { + get { return _basePath; } + set { + _basePath = value; + // pass-through to ApiClient if it's set. + if(_apiClient != null) { + _apiClient.RestClient.BaseUrl = new Uri(_basePath); + } + } + } + + /// + /// Gets or sets the default header. + /// + public virtual IDictionary DefaultHeader { get; set; } + + /// + /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. + /// + public virtual int Timeout + { + {{#netStandard}}get { return (int)ApiClient.RestClient.Timeout.GetValueOrDefault(TimeSpan.FromSeconds(0)).TotalMilliseconds; } + set { ApiClient.RestClient.Timeout = TimeSpan.FromMilliseconds(value); }{{/netStandard}}{{^netStandard}} + get { return ApiClient.RestClient.Timeout; } + set { ApiClient.RestClient.Timeout = value; }{{/netStandard}} + } + + /// + /// Gets or sets the HTTP user agent. + /// + /// Http user agent. + public virtual string UserAgent { get; set; } + + /// + /// Gets or sets the username (HTTP basic authentication). + /// + /// The username. + public virtual string Username { get; set; } + + /// + /// Gets or sets the password (HTTP basic authentication). + /// + /// The password. + public virtual string Password { get; set; } + + /// + /// Gets or sets the LWAAuthorizationCredentials for Amazon Selling Partner API Authorization + /// + /// The LWAAuthorizationCredentials + public virtual LWAAuthorizationCredentials AuthorizationCredentials { get; set; } + + /// + /// Gets or sets the AWSAuthenticationCredentials for Amazon Selling Partner API Authentication + /// + /// The AWSAuthenticationCredentials + public virtual AWSAuthenticationCredentials AuthenticationCredentials { get; set; } + + /// + /// Gets the API key with prefix. + /// + /// API key identifier (authentication scheme). + /// API key with prefix. + public string GetApiKeyWithPrefix(string apiKeyIdentifier) + { + var apiKeyValue = ""; + ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue); + var apiKeyPrefix = ""; + if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix)) + return apiKeyPrefix + " " + apiKeyValue; + else + return apiKeyValue; + } + + /// + /// Gets or sets the access token for OAuth2 authentication. + /// + /// The access token. + public virtual string AccessToken { get; set; } + + /// + /// Gets or sets the temporary folder path to store the files downloaded from the server. + /// + /// Folder path. + public virtual string TempFolderPath + { + get { return _tempFolderPath; } + + set + { + if (string.IsNullOrEmpty(value)) + { + // Possible breaking change since swagger-codegen 2.2.1, enforce a valid temporary path on set. + _tempFolderPath = Path.GetTempPath(); + return; + } + + // create the directory if it does not exist + if (!Directory.Exists(value)) + { + Directory.CreateDirectory(value); + } + + // check if the path contains directory separator at the end + if (value[value.Length - 1] == Path.DirectorySeparatorChar) + { + _tempFolderPath = value; + } + else + { + _tempFolderPath = value + Path.DirectorySeparatorChar; + } + } + } + + /// + /// Gets or sets the date time format used when serializing in the ApiClient + /// By default, it's set to ISO 8601 - "o", for others see: + /// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx + /// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx + /// No validation is done to ensure that the string you're providing is valid + /// + /// The DateTimeFormat string + public virtual string DateTimeFormat + { + get { return _dateTimeFormat; } + set + { + if (string.IsNullOrEmpty(value)) + { + // Never allow a blank or null string, go back to the default + _dateTimeFormat = ISO8601_DATETIME_FORMAT; + return; + } + + // Caution, no validation when you choose date time format other than ISO 8601 + // Take a look at the above links + _dateTimeFormat = value; + } + } + + /// + /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name. + /// + /// The prefix of the API key. + public virtual IDictionary ApiKeyPrefix + { + get { return _apiKeyPrefix; } + set + { + if (value == null) + { + throw new InvalidOperationException("ApiKeyPrefix collection may not be null."); + } + _apiKeyPrefix = value; + } + } + + /// + /// Gets or sets the API key based on the authentication name. + /// + /// The API key. + public virtual IDictionary ApiKey + { + get { return _apiKey; } + set + { + if (value == null) + { + throw new InvalidOperationException("ApiKey collection may not be null."); + } + _apiKey = value; + } + } + + #endregion Properties + + #region Methods + + /// + /// Add default header. + /// + /// Header field name. + /// Header field value. + /// + public void AddDefaultHeader(string key, string value) + { + DefaultHeader[key] = value; + } + + /// + /// Creates a new based on this instance. + /// + /// + public ApiClient CreateApiClient() + { + return new ApiClient(this); + } + + + /// + /// Returns a string with essential information for debugging. + /// + public static String ToDebugReport() + { + String report = "C# SDK ({{{packageName}}}) Debug Report:\n"; + {{^netStandard}} + {{^supportsUWP}} + report += " OS: " + System.Environment.OSVersion + "\n"; + report += " .NET Framework Version: " + System.Environment.Version + "\n"; + {{/supportsUWP}} + {{/netStandard}} + {{#netStandard}} + report += " OS: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription + "\n"; + {{/netStandard}} + report += " Version of the API: {{{version}}}\n"; + report += " SDK Package Version: {{{packageVersion}}}\n"; + + return report; + } + + /// + /// Add Api Key Header. + /// + /// Api Key name. + /// Api Key value. + /// + public void AddApiKey(string key, string value) + { + ApiKey[key] = value; + } + + /// + /// Sets the API key prefix. + /// + /// Api Key name. + /// Api Key value. + public void AddApiKeyPrefix(string key, string value) + { + ApiKeyPrefix[key] = value; + } + + #endregion Methods + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/IReadableConfiguration.mustache b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/IReadableConfiguration.mustache new file mode 100644 index 0000000..525e9ce --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/IReadableConfiguration.mustache @@ -0,0 +1,98 @@ +{{>partial_header}} + +using System.Collections.Generic; +using Amazon.SellingPartnerAPIAA; + +namespace {{packageName}}.Client +{ + /// + /// Represents a readable-only configuration contract. + /// + public interface IReadableConfiguration + { + /// + /// Gets the access token. + /// + /// Access token. + string AccessToken { get; } + + /// + /// Gets the API key. + /// + /// API key. + IDictionary ApiKey { get; } + + /// + /// Gets the API key prefix. + /// + /// API key prefix. + IDictionary ApiKeyPrefix { get; } + + /// + /// Gets the base path. + /// + /// Base path. + string BasePath { get; } + + /// + /// Gets the date time format. + /// + /// Date time foramt. + string DateTimeFormat { get; } + + /// + /// Gets the default header. + /// + /// Default header. + IDictionary DefaultHeader { get; } + + /// + /// Gets the temp folder path. + /// + /// Temp folder path. + string TempFolderPath { get; } + + /// + /// Gets the HTTP connection timeout (in milliseconds) + /// + /// HTTP connection timeout. + int Timeout { get; } + + /// + /// Gets the user agent. + /// + /// User agent. + string UserAgent { get; } + + /// + /// Gets the username. + /// + /// Username. + string Username { get; } + + /// + /// Gets the password. + /// + /// Password. + string Password { get; } + + /// + /// Gets the API key with prefix. + /// + /// API key identifier (authentication scheme). + /// API key with prefix. + string GetApiKeyWithPrefix(string apiKeyIdentifier); + + /// + /// Gets the LWAAuthorizationCredentials for Amazon Selling Partner API Authorization + /// + /// AuthorizationCredentials + LWAAuthorizationCredentials AuthorizationCredentials { get; } + + /// + /// Gets the AWSAuthenticationCredentials for Amazon Selling Partner API Authentication + /// + /// AuthenticationCredentials + AWSAuthenticationCredentials AuthenticationCredentials { get; } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api.mustache b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api.mustache new file mode 100644 index 0000000..a9b1bcc --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api.mustache @@ -0,0 +1,478 @@ +{{>partial_header}} +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +{{#netStandard}} +using RestSharp.Portable; +{{/netStandard}} +{{^netStandard}} +using RestSharp; +{{/netStandard}} +using {{packageName}}.Client; +{{#hasImport}}using {{packageName}}.{{modelPackage}}; +{{/hasImport}} +using Amazon.SellingPartnerAPIAA; + +namespace {{packageName}}.{{apiPackage}} +{ + {{#operations}} + /// + /// Represents a collection of functions to interact with the API endpoints + /// + {{>visibility}} interface {{interfacePrefix}}{{classname}} : IApiAccessor + { + #region Synchronous Operations + {{#operation}} + /// + /// {{summary}} + /// + /// + /// {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// {{#returnType}}{{returnType}}{{/returnType}} + {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + + /// + /// {{summary}} + /// + /// + /// {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}} + ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + {{/operation}} + #endregion Synchronous Operations + {{#supportsAsync}} + #region Asynchronous Operations + {{#operation}} + /// + /// {{summary}} + /// + /// + /// {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} + {{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + + /// + /// {{summary}} + /// + /// + /// {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}} + System.Threading.Tasks.Task> {{operationId}}AsyncWithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + {{/operation}} + #endregion Asynchronous Operations + {{/supportsAsync}} + } + + /// + /// Represents a collection of functions to interact with the API endpoints + /// + {{>visibility}} partial class {{classname}} : {{interfacePrefix}}{{classname}} + { + private {{packageName}}.Client.ExceptionFactory _exceptionFactory = (name, response) => null; + + /// + /// Initializes a new instance of the class + /// using Configuration object + /// + /// An instance of Configuration + /// + public {{classname}}({{packageName}}.Client.Configuration configuration) + { + this.Configuration = configuration; + ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory; + } + + /// + /// Gets the base path of the API client. + /// + /// The base path + public String GetBasePath() + { + return this.Configuration.ApiClient.RestClient.BaseUrl.ToString(); + } + + /// + /// Sets the base path of the API client. + /// + /// The base path + [Obsolete("SetBasePath is deprecated, please do 'Configuration.ApiClient = new ApiClient(\"http://new-path\")' instead.")] + public void SetBasePath(String basePath) + { + // do nothing + } + + /// + /// Gets or sets the configuration object + /// + /// An instance of the Configuration + public {{packageName}}.Client.Configuration Configuration {get; set;} + + /// + /// Provides a factory method hook for the creation of exceptions. + /// + public {{packageName}}.Client.ExceptionFactory ExceptionFactory + { + get + { + if (_exceptionFactory != null && _exceptionFactory.GetInvocationList().Length > 1) + { + throw new InvalidOperationException("Multicast delegate for ExceptionFactory is unsupported."); + } + return _exceptionFactory; + } + set { _exceptionFactory = value; } + } + + /// + /// Gets the default header. + /// + /// Dictionary of HTTP header + [Obsolete("DefaultHeader is deprecated, please use Configuration.DefaultHeader instead.")] + public IDictionary DefaultHeader() + { + return new {{^net35}}ReadOnly{{/net35}}Dictionary(this.Configuration.DefaultHeader); + } + + /// + /// Add default header. + /// + /// Header field name. + /// Header field value. + /// + [Obsolete("AddDefaultHeader is deprecated, please use Configuration.AddDefaultHeader instead.")] + public void AddDefaultHeader(string key, string value) + { + this.Configuration.AddDefaultHeader(key, value); + } + + {{#operation}} + /// + /// {{summary}} {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// {{#returnType}}{{returnType}}{{/returnType}} + public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { + {{#returnType}}ApiResponse<{{{returnType}}}> localVarResponse = {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + return localVarResponse.Data;{{/returnType}}{{^returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/returnType}} + } + + /// + /// {{summary}} {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}} + public ApiResponse<{{#returnType}} {{{returnType}}} {{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { + {{#allParams}} + {{#required}} + // verify the required parameter '{{paramName}}' is set + if ({{paramName}} == null) + throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}"); + {{/required}} + {{/allParams}} + + var localVarPath = "{{#netStandard}}.{{/netStandard}}{{{path}}}"; + var localVarPathParams = new Dictionary(); + var localVarQueryParams = new List>(); + var localVarHeaderParams = new Dictionary(this.Configuration.DefaultHeader); + var localVarFormParams = new Dictionary(); + var localVarFileParams = new Dictionary(); + Object localVarPostBody = null; + + // to determine the Content-Type header + String[] localVarHttpContentTypes = new String[] { + {{#consumes}} + "{{{mediaType}}}"{{#hasMore}}, {{/hasMore}} + {{/consumes}} + }; + String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); + + // to determine the Accept header + String[] localVarHttpHeaderAccepts = new String[] { + {{#produces}} + "{{{mediaType}}}"{{#hasMore}},{{/hasMore}} + {{/produces}} + }; + String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); + if (localVarHttpHeaderAccept != null) + localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); + + {{#pathParams}} + if ({{paramName}} != null) localVarPathParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // path parameter + {{/pathParams}} + {{#queryParams}} + if ({{paramName}} != null) localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); // query parameter + {{/queryParams}} + {{#headerParams}} + if ({{paramName}} != null) localVarHeaderParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // header parameter + {{/headerParams}} + {{#formParams}} + if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // form parameter{{/isFile}} + {{/formParams}} + {{#bodyParam}} + if ({{paramName}} != null && {{paramName}}.GetType() != typeof(byte[])) + { + localVarPostBody = this.Configuration.ApiClient.Serialize({{paramName}}); // http body (model) parameter + } + else + { + localVarPostBody = {{paramName}}; // byte array + } + {{/bodyParam}} + + {{#authMethods}} + // authentication ({{name}}) required + {{#isApiKey}} + {{#isKeyInHeader}} + if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) + { + localVarHeaderParams["{{keyParamName}}"] = this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"); + } + {{/isKeyInHeader}} + {{#isKeyInQuery}} + if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) + { + localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))); + } + {{/isKeyInQuery}} + {{/isApiKey}} + {{#isBasic}} + // http basic authentication required + if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password)) + { + localVarHeaderParams["Authorization"] = "Basic " + ApiClient.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password); + } + {{/isBasic}} + {{#isOAuth}} + // oauth required + if (!String.IsNullOrEmpty(this.Configuration.AccessToken)) + { + localVarHeaderParams["Authorization"] = "Bearer " + this.Configuration.AccessToken; + } + {{/isOAuth}} + {{/authMethods}} + + // make the HTTP request + IRestResponse localVarResponse = (IRestResponse) this.Configuration.ApiClient.CallApi(localVarPath, + Method.{{httpMethod}}, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, + localVarPathParams, localVarHttpContentType); + + int localVarStatusCode = (int) localVarResponse.StatusCode; + + if (ExceptionFactory != null) + { + Exception exception = ExceptionFactory("{{operationId}}", localVarResponse); + if (exception != null) throw exception; + } + + {{#returnType}} + return new ApiResponse<{{{returnType}}}>(localVarStatusCode, + localVarResponse.Headers.ToDictionary(x => x.{{^netStandard}}Name{{/netStandard}}{{#netStandard}}Key{{/netStandard}}, x => x.Value.ToString()), + ({{{returnType}}}) this.Configuration.ApiClient.Deserialize(localVarResponse, typeof({{#returnContainer}}{{{returnContainer}}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}))); + {{/returnType}} + {{^returnType}} + return new ApiResponse(localVarStatusCode, + localVarResponse.Headers.ToDictionary(x => x.{{^netStandard}}Name{{/netStandard}}{{#netStandard}}Key{{/netStandard}}, x => x.Value.ToString()), + null); + {{/returnType}} + } + + {{#supportsAsync}} + /// + /// {{summary}} {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} + {{#returnType}}public async System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}public async System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { + {{#returnType}}ApiResponse<{{{returnType}}}> localVarResponse = await {{operationId}}AsyncWithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + return localVarResponse.Data;{{/returnType}}{{^returnType}}await {{operationId}}AsyncWithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/returnType}} + + } + + /// + /// {{summary}} {{notes}} + /// + /// Thrown when fails to make API call + {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}}/// Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}} + public async System.Threading.Tasks.Task> {{operationId}}AsyncWithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { + {{#allParams}} + {{#required}} + // verify the required parameter '{{paramName}}' is set + if ({{paramName}} == null) + throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}"); + {{/required}} + {{/allParams}} + + var localVarPath = "{{#netStandard}}.{{/netStandard}}{{{path}}}"; + var localVarPathParams = new Dictionary(); + var localVarQueryParams = new List>(); + var localVarHeaderParams = new Dictionary(this.Configuration.DefaultHeader); + var localVarFormParams = new Dictionary(); + var localVarFileParams = new Dictionary(); + Object localVarPostBody = null; + + // to determine the Content-Type header + String[] localVarHttpContentTypes = new String[] { + {{#consumes}} + "{{{mediaType}}}"{{#hasMore}}, {{/hasMore}} + {{/consumes}} + }; + String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); + + // to determine the Accept header + String[] localVarHttpHeaderAccepts = new String[] { + {{#produces}} + "{{{mediaType}}}"{{#hasMore}},{{/hasMore}} + {{/produces}} + }; + String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); + if (localVarHttpHeaderAccept != null) + localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); + + {{#pathParams}} + if ({{paramName}} != null) localVarPathParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // path parameter + {{/pathParams}} + {{#queryParams}} + if ({{paramName}} != null) localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); // query parameter + {{/queryParams}} + {{#headerParams}} + if ({{paramName}} != null) localVarHeaderParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // header parameter + {{/headerParams}} + {{#formParams}} + if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // form parameter{{/isFile}} + {{/formParams}} + {{#bodyParam}} + if ({{paramName}} != null && {{paramName}}.GetType() != typeof(byte[])) + { + localVarPostBody = this.Configuration.ApiClient.Serialize({{paramName}}); // http body (model) parameter + } + else + { + localVarPostBody = {{paramName}}; // byte array + } + {{/bodyParam}} + + {{#authMethods}} + // authentication ({{name}}) required + {{#isApiKey}} + {{#isKeyInHeader}} + if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) + { + localVarHeaderParams["{{keyParamName}}"] = this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"); + } + {{/isKeyInHeader}} + {{#isKeyInQuery}} + if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) + { + localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))); + } + {{/isKeyInQuery}} + {{/isApiKey}} + {{#isBasic}} + // http basic authentication required + if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password)) + { + localVarHeaderParams["Authorization"] = "Basic " + ApiClient.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password); + } + {{/isBasic}} + {{#isOAuth}} + // oauth required + if (!String.IsNullOrEmpty(this.Configuration.AccessToken)) + { + localVarHeaderParams["Authorization"] = "Bearer " + this.Configuration.AccessToken; + } + {{/isOAuth}} + {{/authMethods}} + + // make the HTTP request + IRestResponse localVarResponse = (IRestResponse) await this.Configuration.ApiClient.CallApiAsync(localVarPath, + Method.{{httpMethod}}, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, + localVarPathParams, localVarHttpContentType); + + int localVarStatusCode = (int) localVarResponse.StatusCode; + + if (ExceptionFactory != null) + { + Exception exception = ExceptionFactory("{{operationId}}", localVarResponse); + if (exception != null) throw exception; + } + + {{#returnType}} + return new ApiResponse<{{{returnType}}}>(localVarStatusCode, + localVarResponse.Headers.ToDictionary(x => x.{{^netStandard}}Name{{/netStandard}}{{#netStandard}}Key{{/netStandard}}, x => x.Value.ToString()), + ({{{returnType}}}) this.Configuration.ApiClient.Deserialize(localVarResponse, typeof({{#returnContainer}}{{{returnContainer}}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}))); + {{/returnType}} + {{^returnType}} + return new ApiResponse(localVarStatusCode, + localVarResponse.Headers.ToDictionary(x => x.{{^netStandard}}Name{{/netStandard}}{{#netStandard}}Key{{/netStandard}}, x => x.Value.ToString()), + null); + {{/returnType}} + } + + {{/supportsAsync}} + {{/operation}} + + public class Builder + { + private LWAAuthorizationCredentials lwaAuthorizationCredentials; + private AWSAuthenticationCredentials awsAuthenticationCredentials; + + public Builder SetLWAAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials) + { + this.lwaAuthorizationCredentials = lwaAuthorizationCredentials; + return this; + } + + public Builder SetAWSAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) + { + this.awsAuthenticationCredentials = awsAuthenticationCredentials; + return this; + } + + public {{classname}} Build() + { + if (lwaAuthorizationCredentials == null) + { + throw new NullReferenceException("LWAAuthoriztionCredentials not set"); + } + + if (awsAuthenticationCredentials == null) + { + throw new NullReferenceException("AWSAuthenticationCredentials not set"); + } + + {{packageName}}.Client.Configuration configuration = new {{packageName}}.Client.Configuration() + { + AuthorizationCredentials = lwaAuthorizationCredentials, + AuthenticationCredentials = awsAuthenticationCredentials + }; + + // default HTTP connection timeout (in milliseconds) + configuration.Timeout = 100000; + + return new {{classname}}(configuration); + } + } + } + {{/operations}} +} diff --git a/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api_test.mustache b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api_test.mustache new file mode 100644 index 0000000..2227fea --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/src/Amazon.SellingPartnerAPIAA/resources/swagger-codegen/templates/api_test.mustache @@ -0,0 +1,76 @@ +{{>partial_header}} +using System; +using System.IO; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reflection; +using RestSharp; +using NUnit.Framework; + +using {{packageName}}.Client; +using {{packageName}}.{{apiPackage}}; +{{#hasImport}}using {{packageName}}.{{modelPackage}}; +{{/hasImport}} + +namespace {{packageName}}.Test +{ + /// + /// Class for testing {{classname}} + /// + /// + /// This file is automatically generated by Swagger Codegen. + /// Please update the test case below to test the API endpoint. + /// + [TestFixture] + public class {{classname}}Tests + { + private {{classname}} instance; + + /// + /// Setup before each unit test + /// + [SetUp] + public void Init() + { + // TODO uncomment below to initialize instance for testing + //instance = new {{classname}}(); + } + + /// + /// Clean up after each unit test + /// + [TearDown] + public void Cleanup() + { + + } + + /// + /// Test an instance of {{classname}} + /// + [Test] + public void {{operationId}}InstanceTest() + { + // TODO uncomment below to test 'IsInstanceOfType' {{classname}} + //Assert.IsInstanceOfType(typeof({{classname}}), instance, "instance is a {{classname}}"); + } + + {{#operations}}{{#operation}} + /// + /// Test {{operationId}} + /// + [Test] + public void {{operationId}}Test() + { + // TODO uncomment below to test the method and replace null with proper value + {{#allParams}} + //{{{dataType}}} {{paramName}} = null; + {{/allParams}} + //{{#returnType}}var response = {{/returnType}}instance.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + {{#returnType}}//Assert.IsInstanceOf<{{{returnType}}}> (response, "response is {{{returnType}}}");{{/returnType}} + } + {{/operation}}{{/operations}} + } + +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSigV4SignerTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSigV4SignerTest.cs new file mode 100644 index 0000000..d0487a0 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSigV4SignerTest.cs @@ -0,0 +1,88 @@ +using Moq; +using Xunit; +using RestSharp; +using Amazon.SellingPartnerAPIAA; +using System; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class AWSSigV4SignerTest + { + private const string TestAccessKeyId = "aKey"; + private const string TestSecretKey = "sKey"; + private const string TestRegion = "us-east-1"; + private const string TestResourcePath = "iam/user"; + private const string TestHost = "sellingpartnerapi.amazon.com"; + + private RestRequest request; + private AWSSigV4Signer sigV4SignerUnderTest; + private Mock mockAWSSignerHelper; + + public AWSSigV4SignerTest() + { + request = new RestRequest(TestResourcePath, Method.GET); + + AWSAuthenticationCredentials authenticationCredentials = new AWSAuthenticationCredentials + { + AccessKeyId = TestAccessKeyId, + SecretKey = TestSecretKey, + Region = TestRegion + }; + mockAWSSignerHelper = new Mock(); + sigV4SignerUnderTest = new AWSSigV4Signer(authenticationCredentials); + sigV4SignerUnderTest.AwsSignerHelper = mockAWSSignerHelper.Object; + } + + [Fact] + public void TestSignRequest() + { + DateTime signingDate = DateTime.UtcNow; + string expectedHashedCanonicalRequest = "b7a5ea4c3179fcebed77f19ccd7d85795d4b7a1810709b55fa7ad3fd79ab6adc"; + string expectedSignedHeaders = "testSignedHeaders"; + string expectedSignature = "testSignature"; + string expectedStringToSign = "testStringToSign"; + mockAWSSignerHelper.Setup(signerHelper => signerHelper.InitializeHeaders(request, TestHost)) + .Returns(signingDate); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.ExtractCanonicalURIParameters(request.Resource)) + .Returns("testURIParameters"); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.ExtractCanonicalQueryString(request)) + .Returns("testCanonicalQueryString"); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.ExtractCanonicalHeaders(request)) + .Returns("testCanonicalHeaders"); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.ExtractSignedHeaders(request)) + .Returns(expectedSignedHeaders); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.HashRequestBody(request)) + .Returns("testHashRequestBody"); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.BuildStringToSign(signingDate, + expectedHashedCanonicalRequest, TestRegion)) + .Returns(expectedStringToSign); + mockAWSSignerHelper.Setup(signerHelper => signerHelper.CalculateSignature(expectedStringToSign, + signingDate, TestSecretKey, TestRegion)) + .Returns(expectedSignature); + + IRestRequest actualRestRequest = sigV4SignerUnderTest.Sign(request, TestHost); + + mockAWSSignerHelper.Verify(signerHelper => signerHelper.InitializeHeaders(request, TestHost)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.ExtractCanonicalURIParameters(request.Resource)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.ExtractCanonicalQueryString(request)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.ExtractCanonicalHeaders(request)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.ExtractSignedHeaders(request)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.HashRequestBody(request)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.BuildStringToSign(signingDate, + expectedHashedCanonicalRequest, + TestRegion)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.CalculateSignature(expectedStringToSign, + signingDate, + TestSecretKey, + TestRegion)); + mockAWSSignerHelper.Verify(signerHelper => signerHelper.AddSignature(request, + TestAccessKeyId, + expectedSignedHeaders, + expectedSignature, + TestRegion, + signingDate)); + + Assert.Equal(request, actualRestRequest); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSignerHelperTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSignerHelperTest.cs new file mode 100644 index 0000000..4a10c8c --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/AWSSignerHelperTest.cs @@ -0,0 +1,269 @@ +using System; +using Xunit; +using RestSharp; +using Amazon.SellingPartnerAPIAA; +using System.Text; +using Moq; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class AWSSignerHelperTest + { + private const string Slash = "/"; + private const string ISOSigningDateTime = "20200504T121212Z"; + private const string ISOSigningDate = "20200504"; + private const string TestAccessKeyId = "aKey"; + private const string TestSecretKey = "sKey"; + private const string TestRegion = "us-east-1"; + private const string TestResourcePath = "iam/user"; + private const string TestHost = "sellingpartnerapi.amazon.com"; + private const string JsonMediaType = "application/json; charset=utf-8"; + + private static readonly DateTime SigningDate = DateTime.Parse("2020-05-04 12:12:12"); + + private AWSSignerHelper awsSignerHelperUnderTest; + + public AWSSignerHelperTest() + { + var mockDateHelper = new Mock(); + mockDateHelper.Setup(dateHelper => dateHelper.GetUtcNow()).Returns(SigningDate); + awsSignerHelperUnderTest = new AWSSignerHelper() { DateHelper = mockDateHelper.Object }; + } + + [Fact] + public void TestExtractCanonicalURIParameters() + { + IRestRequest request = new RestRequest(TestResourcePath, Method.GET); + string result = awsSignerHelperUnderTest.ExtractCanonicalURIParameters(request.Resource); + Assert.Equal("/iam/user", result); + } + + [Fact] + public void TestExtractCanonicalURIParameters_ResourcePathWithSpace() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalURIParameters("iam/ user"); + Assert.Equal("/iam/%2520user", result); + } + + [Fact] + public void TestExtractCanonicalURIParameters_EmptyResourcePath() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalURIParameters(string.Empty); + Assert.Equal(Slash, result); + } + + [Fact] + public void TestExtractCanonicalURIParameters_NullResourcePath() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalURIParameters(null); + Assert.Equal(Slash, result); + } + + [Fact] + public void TestExtractCanonicalURIParameters_SlashPath() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalURIParameters(Slash); + Assert.Equal(Slash, result); + } + + [Fact] + public void TestExtractCanonicalQueryString() + { + IRestRequest request = new RestRequest(); + request.AddQueryParameter("Version", "2010-05-08"); + request.AddQueryParameter("Action", "ListUsers"); + request.AddQueryParameter("RequestId", "1"); + + string result = awsSignerHelperUnderTest.ExtractCanonicalQueryString(request); + //Query parameters in canonical order + Assert.Equal("Action=ListUsers&RequestId=1&Version=2010-05-08", result); + } + + [Fact] + public void TestExtractCanonicalQueryString_EmptyQueryParameters() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalQueryString(new RestRequest()); + Assert.Empty(result); + } + + [Fact] + public void TestExtractCanonicalQueryString_WithUrlEncoding() + { + IRestRequest request = new RestRequest(); + request.AddQueryParameter("Action^", "ListUsers$Roles"); + string result = awsSignerHelperUnderTest.ExtractCanonicalQueryString(request); + Assert.Equal("Action%5E=ListUsers%24Roles", result); + } + + [Fact] + public void TestExtractCanonicalHeaders() + { + IRestRequest request = new RestRequest(); + request.AddHeader("X-Amz-Date", "20150830T123600Z"); + request.AddHeader("Host", "iam.amazonaws.com"); + request.AddHeader("Content-Type", JsonMediaType); + + string result = awsSignerHelperUnderTest.ExtractCanonicalHeaders(request); + Assert.Equal("content-type:application/json; charset=utf-8\nhost:iam.amazonaws.com\n" + + "x-amz-date:20150830T123600Z\n", result); + } + + [Fact] + public void TestExtractCanonicalHeaders_NoHeader() + { + string result = awsSignerHelperUnderTest.ExtractCanonicalHeaders(new RestRequest()); + Assert.Empty(result); + } + + [Fact] + public void TestExtractSignedHeaders() + { + IRestRequest request = new RestRequest(); + request.AddHeader("X-Amz-Date", "20150830T123600Z"); + request.AddHeader("Host", "iam.amazonaws.com"); + request.AddHeader("Content-Type", JsonMediaType); + + string result = awsSignerHelperUnderTest.ExtractSignedHeaders(request); + Assert.Equal("content-type;host;x-amz-date", result); + } + + [Fact] + public void TestExtractSignedHeaders_NoHeader() + { + string result = awsSignerHelperUnderTest.ExtractSignedHeaders(new RestRequest()); + Assert.Empty(result); + } + + [Fact] + public void TestHashRequestBody() + { + IRestRequest request = new RestRequest(TestResourcePath, Method.POST); + request.AddJsonBody("{\"test\":\"payload\"}"); + + string result = awsSignerHelperUnderTest.HashRequestBody(request); + Assert.NotEmpty(result); + } + + [Fact] + public void TestHashRequestBody_NoBody() + { + string result = awsSignerHelperUnderTest.HashRequestBody(new RestRequest()); + Assert.NotEmpty(result); + } + + [Fact] + public void TestBuildStringToSign() + { + string expectedCanonicalHash = "foo"; + StringBuilder expectedStringBuilder = new StringBuilder(); + expectedStringBuilder.AppendLine("AWS4-HMAC-SHA256"); + expectedStringBuilder.AppendLine(ISOSigningDateTime); + expectedStringBuilder.AppendFormat("{0}/{1}/execute-api/aws4_request\n", ISOSigningDate, TestRegion); + expectedStringBuilder.Append(expectedCanonicalHash); + + string result = awsSignerHelperUnderTest.BuildStringToSign(SigningDate, expectedCanonicalHash, TestRegion); + + Assert.Equal(expectedStringBuilder.ToString(), result); + } + + [Fact] + public void TestInitializeHeadersReturnsUtcNow() + { + Assert.Equal(SigningDate, awsSignerHelperUnderTest.InitializeHeaders(new RestRequest(), TestHost)); + } + + [Fact] + public void TestInitializeHeadersSetsUtcNowXAmzDateHeader() + { + IRestRequest request = new RestRequest(); + awsSignerHelperUnderTest.InitializeHeaders(request, TestHost); + + Parameter actualParameter = request.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == AWSSignerHelper.XAmzDateHeaderName); + + Assert.Equal(ISOSigningDateTime, actualParameter.Value); + } + + [Fact] + public void TestInitializeHeadersOverwritesXAmzDateHeader() + { + IRestRequest request = new RestRequest(); + request.AddHeader(AWSSignerHelper.XAmzDateHeaderName, "foobar"); + + awsSignerHelperUnderTest.InitializeHeaders(request, TestHost); + + Parameter actualParameter = request.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == AWSSignerHelper.XAmzDateHeaderName); + + Assert.Equal(ISOSigningDateTime, actualParameter.Value); + } + + [Fact] + public void TestAddSignatureToRequest() + { + IRestRequest restRequest = new RestRequest(); + string expectedAccessKeyId = TestAccessKeyId; + string expectedRegion = TestRegion; + string expectedSignature = "testCalculatedSignature"; + string expectedSignedHeaders = "header1;header2"; + + string expectedAuthorizationHeaderValue = string.Format("AWS4-HMAC-SHA256 " + + "Credential={0}/{1}/{2}/execute-api/aws4_request, SignedHeaders={3}, Signature={4}", + expectedAccessKeyId, + ISOSigningDate, + expectedRegion, + expectedSignedHeaders, + expectedSignature); + + awsSignerHelperUnderTest.AddSignature(restRequest, + expectedAccessKeyId, + expectedSignedHeaders, + expectedSignature, + expectedRegion, + SigningDate); + + Parameter actualParameter = restRequest.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == AWSSignerHelper.AuthorizationHeaderName); + + Assert.Equal(expectedAuthorizationHeaderValue, actualParameter.Value); + } + + [Fact] + public void TestCalculateSignature() + { + string signature = awsSignerHelperUnderTest.CalculateSignature("testString", + SigningDate, + TestSecretKey, + TestRegion); + Assert.Equal("7e2c7c2e330123ef7468b41d8ddaf3841e6ef56959b9116b44ded5466cf96405", signature); + } + + [Fact] + public void TestInitializeHeadersSetsHostHeader() + { + IRestRequest restRequest = new RestRequest(); + + awsSignerHelperUnderTest.InitializeHeaders(restRequest, TestHost); + + Parameter actualParamter = restRequest.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == AWSSignerHelper.HostHeaderName); + + Assert.Equal(TestHost, actualParamter.Value); + } + + [Fact] + public void TestInitializeHeadersOverwritesHostHeader() + { + IRestRequest restRequest = new RestRequest(); + + restRequest.AddHeader(AWSSignerHelper.HostHeaderName, "foobar"); + + awsSignerHelperUnderTest.InitializeHeaders(restRequest, TestHost); + + Parameter actualParamter = restRequest.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == AWSSignerHelper.HostHeaderName); + + Assert.Equal(TestHost, actualParamter.Value); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/Amazon.SellingPartnerAPIAATests.csproj b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/Amazon.SellingPartnerAPIAATests.csproj new file mode 100644 index 0000000..4a0bd06 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/Amazon.SellingPartnerAPIAATests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAccessTokenRequestMetaBuilderTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAccessTokenRequestMetaBuilderTest.cs new file mode 100644 index 0000000..1c96f64 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAccessTokenRequestMetaBuilderTest.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using Amazon.SellingPartnerAPIAA; +using Xunit; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class LWAAccessTokenRequestMetaBuilderTest + { + private const string TestClientId = "cid"; + private const string TestClientSecret = "csecret"; + private const string TestRefreshToken = "rtoken"; + private static readonly Uri TestUri = new Uri("https://www.amazon.com"); + private LWAAccessTokenRequestMetaBuilder lwaAccessTokenRequestMetaBuilderUnderTest; + + public LWAAccessTokenRequestMetaBuilderTest() + { + lwaAccessTokenRequestMetaBuilderUnderTest = new LWAAccessTokenRequestMetaBuilder(); + } + + [Fact] + public void LWAAuthorizationCredentialsWithoutScopesBuildsSellerTokenRequestMeta() + { + LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials() + { + ClientId = TestClientId, + ClientSecret = TestClientSecret, + Endpoint = TestUri, + RefreshToken = TestRefreshToken + }; + + LWAAccessTokenRequestMeta expected = new LWAAccessTokenRequestMeta() + { + ClientId = TestClientId, + ClientSecret = TestClientSecret, + GrantType = LWAAccessTokenRequestMetaBuilder.SellerAPIGrantType, + RefreshToken = TestRefreshToken, + Scope = null + }; + + LWAAccessTokenRequestMeta actual = lwaAccessTokenRequestMetaBuilderUnderTest.Build(lwaAuthorizationCredentials); + + Assert.Equal(expected, actual); + } + + [Fact] + public void LWAAuthorizationCredentialsWithScopesBuildsSellerlessTokenRequestMeta() + { + LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials() + { + ClientId = TestClientId, + ClientSecret = TestClientSecret, + Endpoint = TestUri, + Scopes = new List() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI } + }; + + LWAAccessTokenRequestMeta expected = new LWAAccessTokenRequestMeta() + { + ClientId = TestClientId, + ClientSecret = TestClientSecret, + GrantType = LWAAccessTokenRequestMetaBuilder.SellerlessAPIGrantType, + Scope = string.Format("{0} {1}", ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI), + RefreshToken = null + }; + + LWAAccessTokenRequestMeta actual = lwaAccessTokenRequestMetaBuilderUnderTest.Build(lwaAuthorizationCredentials); + + Assert.Equal(expected, actual); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAuthorizationSignerTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAuthorizationSignerTest.cs new file mode 100644 index 0000000..9a71b19 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAAuthorizationSignerTest.cs @@ -0,0 +1,49 @@ +using System; +using Xunit; +using Moq; +using RestSharp; +using Amazon.SellingPartnerAPIAA; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class LWAAuthorizationSignerTest + { + private static readonly LWAAuthorizationCredentials LWAAuthorizationCredentials = new LWAAuthorizationCredentials() + { + ClientId = "cid", + ClientSecret = "csecret", + Endpoint = new Uri("https://www.amazon.com") + }; + + private LWAAuthorizationSigner lwaAuthorizationSignerUnderTest; + + public LWAAuthorizationSignerTest() + { + lwaAuthorizationSignerUnderTest = new LWAAuthorizationSigner(LWAAuthorizationCredentials); + } + + [Fact] + public void ConstructorInitializesLWAClientWithCredentials() + { + Assert.Equal(LWAAuthorizationCredentials, lwaAuthorizationSignerUnderTest.LWAClient.LWAAuthorizationCredentials); + } + + [Fact] + public void RequestIsSignedFromLWAClientProvidedToken() + { + string expectedAccessToken = "foo"; + + var mockLWAClient = new Mock(LWAAuthorizationCredentials); + mockLWAClient.Setup(lwaClient => lwaClient.GetAccessToken()).Returns(expectedAccessToken); + lwaAuthorizationSignerUnderTest.LWAClient = mockLWAClient.Object; + + IRestRequest restRequest = new RestRequest(); + restRequest = lwaAuthorizationSignerUnderTest.Sign(restRequest); + + Parameter actualAccessTokenHeader = restRequest.Parameters.Find(parameter => + ParameterType.HttpHeader.Equals(parameter.Type) && parameter.Name == LWAAuthorizationSigner.AccessTokenHeaderName); + + Assert.Equal(expectedAccessToken, actualAccessTokenHeader.Value); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAClientTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAClientTest.cs new file mode 100644 index 0000000..cf4fbc2 --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/LWAClientTest.cs @@ -0,0 +1,148 @@ +using System; +using System.IO; +using System.Linq; +using System.Net; +using Moq; +using Newtonsoft.Json.Linq; +using RestSharp; +using Amazon.SellingPartnerAPIAA; +using Xunit; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class LWAClientTest + { + private const string TestClientSecret = "cSecret"; + private const string TestClientId = "cId"; + private const string TestRefreshGrantType = "rToken"; + private Mock mockRestClient; + private Mock mockLWAAccessTokenRequestMetaBuilder; + + private static readonly Uri TestEndpoint = new Uri("https://www.amazon.com/lwa"); + private static readonly LWAAuthorizationCredentials LWAAuthorizationCredentials = new LWAAuthorizationCredentials + { + ClientId = TestClientId, + ClientSecret = TestClientSecret, + RefreshToken = TestRefreshGrantType, + Endpoint = TestEndpoint + }; + private static readonly IRestResponse Response = new RestResponse + { + StatusCode = HttpStatusCode.OK, + ResponseStatus = ResponseStatus.Completed, + Content = @"{access_token:'Azta|foo'}" + }; + + public LWAClientTest() + { + mockRestClient = new Mock(); + mockLWAAccessTokenRequestMetaBuilder = new Mock(); + } + + [Fact] + public void InitializeLWAAuthorizationCredentials() + { + LWAClient lwaClientUnderTest = new LWAClient(LWAAuthorizationCredentials); + Assert.Equal(LWAAuthorizationCredentials, lwaClientUnderTest.LWAAuthorizationCredentials); + } + + [Fact] + public void MakeRequestFromMeta() + { + IRestRequest request = new RestRequest(); + LWAAccessTokenRequestMeta expectedLWAAccessTokenRequestMeta = new LWAAccessTokenRequestMeta() + { + ClientSecret = "expectedSecret", + ClientId = "expectedClientId", + RefreshToken = "expectedRefreshToken", + GrantType = "expectedGrantType" + }; + + mockRestClient.Setup(client => client.Execute(It.IsAny())) + .Callback((IRestRequest req) => { request = req; }) + .Returns(Response); + + mockLWAAccessTokenRequestMetaBuilder.Setup(builder => builder.Build(LWAAuthorizationCredentials)) + .Returns(expectedLWAAccessTokenRequestMeta); + + LWAClient lwaClientUnderTest = new LWAClient(LWAAuthorizationCredentials); + lwaClientUnderTest.RestClient = mockRestClient.Object; + lwaClientUnderTest.LWAAccessTokenRequestMetaBuilder = mockLWAAccessTokenRequestMetaBuilder.Object; + lwaClientUnderTest.GetAccessToken(); + + Parameter requestBody = request.Parameters + .FirstOrDefault(parameter => parameter.Type.Equals(ParameterType.RequestBody)); + + JObject jsonRequestBody = JObject.Parse(requestBody.Value.ToString()); + + Assert.Equal(Method.POST, request.Method); + Assert.Equal(TestEndpoint.AbsolutePath, request.Resource); + Assert.Equal(expectedLWAAccessTokenRequestMeta.RefreshToken, jsonRequestBody.GetValue("refresh_token")); + Assert.Equal(expectedLWAAccessTokenRequestMeta.GrantType, jsonRequestBody.GetValue("grant_type")); + Assert.Equal(expectedLWAAccessTokenRequestMeta.ClientId, jsonRequestBody.GetValue("client_id")); + Assert.Equal(expectedLWAAccessTokenRequestMeta.ClientSecret, jsonRequestBody.GetValue("client_secret")); + } + + [Fact] + public void ReturnAccessTokenFromResponse() + { + IRestRequest request = new RestRequest(); + + mockRestClient.Setup(client => client.Execute(It.IsAny())) + .Callback((IRestRequest req) => { request = req; }) + .Returns(Response); + + LWAClient lwaClientUnderTest = new LWAClient(LWAAuthorizationCredentials); + lwaClientUnderTest.RestClient = mockRestClient.Object; + + string accessToken = lwaClientUnderTest.GetAccessToken(); + + Assert.Equal("Azta|foo", accessToken); + } + + [Fact] + public void UnsuccessfulPostThrowsException() + { + IRestResponse response = new RestResponse + { + StatusCode = HttpStatusCode.BadRequest, + ResponseStatus = ResponseStatus.Completed, + Content = string.Empty + }; + + IRestRequest request = new RestRequest(); + + mockRestClient.Setup(client => client.Execute(It.IsAny())) + .Callback((IRestRequest req) => { request = req; }) + .Returns(response); + + LWAClient lwaClientUnderTest = new LWAClient(LWAAuthorizationCredentials); + lwaClientUnderTest.RestClient = mockRestClient.Object; + + SystemException systemException = Assert.Throws(() => lwaClientUnderTest.GetAccessToken()); + Assert.IsType(systemException.GetBaseException()); + } + + [Fact] + public void MissingAccessTokenInResponseThrowsException() + { + IRestResponse response = new RestResponse + { + StatusCode = HttpStatusCode.OK, + ResponseStatus = ResponseStatus.Completed, + Content = string.Empty + }; + + IRestRequest request = new RestRequest(); + + mockRestClient.Setup(client => client.Execute(It.IsAny())) + .Callback((IRestRequest req) => { request = (RestRequest)req; }) + .Returns(response); + + LWAClient lwaClientUnderTest = new LWAClient(LWAAuthorizationCredentials); + lwaClientUnderTest.RestClient = mockRestClient.Object; + + Assert.Throws(() => lwaClientUnderTest.GetAccessToken()); + } + } +} diff --git a/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/UtilsTest.cs b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/UtilsTest.cs new file mode 100644 index 0000000..448092a --- /dev/null +++ b/clients/sellingpartner-api-aa-csharp/test/Amazon.SellingPartnerAPIAATests/UtilsTest.cs @@ -0,0 +1,55 @@ +using System; +using System.Linq; +using System.Text; +using Xunit; +using Amazon.SellingPartnerAPIAA; + +namespace Amazon.SellingPartnerAPIAATests +{ + public class UtilsTest + { + private const string TestString = "test"; + + [Fact] + public void TestUrlEncode_WithoutEncoding() + { + string result = Utils.UrlEncode("Test-_.~"); + Assert.Equal("Test-_.~", result); + } + + [Fact] + public void TestUrlEncode_WithEncoding() + { + string result = Utils.UrlEncode("Test$%*^"); + Assert.Equal("Test%24%25%2A%5E", result); + } + + [Fact] + public void TestUrlEncode_Empty() + { + Assert.Empty(Utils.UrlEncode(string.Empty)); + } + + [Fact] + public void TestHash() + { + Assert.NotEmpty(Utils.Hash(TestString)); + } + + [Fact] + public void TestToHex() + { + string result = Utils.ToHex(Encoding.UTF8.GetBytes(TestString)); + Assert.Equal("74657374", result); + } + + [Fact] + public void TestGetKeyedHash() + { + byte[] expectedHash = new byte[] { 106, 120, 238, 51, 86, 30, 87, 173, 232, 197, 95, 132,155, + 183, 80, 81, 25, 213, 212, 241, 218, 201, 168, 17, 253, 143, 54, 226, 42, 118, 61, 54 }; + byte[] keyedHash = Utils.GetKeyedHash(Encoding.UTF8.GetBytes("testKey"), TestString); + Assert.True(expectedHash.SequenceEqual(keyedHash)); + } + } +} diff --git a/clients/sellingpartner-api-aa-java/.gitignore b/clients/sellingpartner-api-aa-java/.gitignore new file mode 100644 index 0000000..8b06d03 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/.gitignore @@ -0,0 +1,9 @@ +*.iml +.idea/ +target/ +out/ +.settings/ +.DS_Store +.classpath +.project + diff --git a/clients/sellingpartner-api-aa-java/README.md b/clients/sellingpartner-api-aa-java/README.md new file mode 100644 index 0000000..0f851be --- /dev/null +++ b/clients/sellingpartner-api-aa-java/README.md @@ -0,0 +1,90 @@ +# Selling Partner API Authentication/Authorization Library +This library provides helper classes for use when signing HTTP requests for Amazon Selling Partner APIs. It is intended for use +with the Selling Partner API Client Libraries generated by [swagger codegen](https://swagger.io/tools/swagger-codegen/) +using the OkHttp library. It can also be integrated into custom projects. + +## LWAAuthorizationSigner +Obtains and signs a request with an access token from LWA (Login with Amazon) for the specified endpoint using the provided LWA credentials. + +*Example* +``` +com.squareup.okhttp.Request request = new Request.Builder() + .url(...) + ... + .build(); + +// Seller APIs + +LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() + .clientId("...") + .clientSecret("...") + .refreshToken("...") + .endpoint("...") + .build(); + +/* Sellerless APIs +The Selling Partner API scopes can be retrieved from the ScopeConstants class and passed as argument(s) to either the withScope(String scope) or withScopes(String... scopes) method during lwaAuthorizationCredentials object instantiation. */ + +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API; + +LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() + .clientId("...") + .clientSecret("...") + .withScopes("...") + .endpoint("...") + .build(); + +com.squareup.okhttp.Request signedRequest = new LWAAuthorizationSigner(lwaAuthorizationCredentials) + .sign(request); +``` + +## AWSSigV4Signer +Signs a request with [AWS Signature Version 4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) +using the provided AWS developer account credentials. + +*Example* +``` +com.squareup.okhttp.Request request = new Request.Builder() + .url(...) + ... + .build(); + +AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder() + .accessKeyId("...") + .secretKey("...") + .region("...") + .build(); + +com.squareup.okhttp.Request signedRequest = new AWSSigV4Signer(awsAuthenticationCredentials) + .sign(request); +``` + +## Resources +This package features Mustache templates designed for use with [swagger codegen](https://swagger.io/tools/swagger-codegen/). +When you build Selling Partner API Swagger models with these templates, they help generate a rich SDK with functionality to invoke Selling Partner APIs built in. The templates are located in *resources/swagger-codegen*. + +## Building +This library can be built using Maven by running this command in the package root: + ``` +mvn clean package +``` +Dependencies are declared in the pom.xml file. + +## License +Swagger Codegen templates are subject to the [Swagger Codegen License](https://github.com/swagger-api/swagger-codegen#license). + +All other work licensed as follows: + +Copyright 2019 Amazon.com, Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this library except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/clients/sellingpartner-api-aa-java/pom.xml b/clients/sellingpartner-api-aa-java/pom.xml new file mode 100644 index 0000000..5ce406c --- /dev/null +++ b/clients/sellingpartner-api-aa-java/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + com.amazon.sellingpartnerapi + sellingpartnerapi-aa-java + jar + 1.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.1 + + + build-assembly + package + + single + + + + + + jar-with-dependencies + + + + + src + target + ${project.artifactId}-${project.version} + + + + + com.amazonaws + aws-java-sdk-signer + 1.11.610 + + + + org.projectlombok + lombok + 1.18.8 + provided + + + + com.google.code.gson + gson + 2.8.5 + + + + com.squareup.okhttp + okhttp + 2.7.5 + + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0 + test + + + + org.junit.jupiter + junit-jupiter-params + 5.3.2 + test + + + + + org.junit.jupiter + junit-jupiter-migrationsupport + 5.5.1 + test + + + + org.mockito + mockito-core + 3.0.0 + test + + + + org.apache.commons + commons-lang3 + 3.9 + + + + org.apache.httpcomponents + httpclient + 4.5.9 + + + diff --git a/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/ApiClient.mustache b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/ApiClient.mustache new file mode 100644 index 0000000..2ea2c54 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/ApiClient.mustache @@ -0,0 +1,1258 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import com.squareup.okhttp.*; +import com.squareup.okhttp.internal.http.HttpMethod; +import com.squareup.okhttp.logging.HttpLoggingInterceptor; +import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level; +import okio.BufferedSink; +import okio.Okio; +{{#joda}} +import org.joda.time.DateTime; +import org.joda.time.LocalDate; +import org.joda.time.format.DateTimeFormatter; +{{/joda}} +{{#threetenbp}} +import org.threeten.bp.LocalDate; +import org.threeten.bp.OffsetDateTime; +import org.threeten.bp.format.DateTimeFormatter; +{{/threetenbp}} + +import javax.net.ssl.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.SecureRandom; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.text.DateFormat; +{{#java8}} +import java.time.LocalDate; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +{{/java8}} +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import {{invokerPackage}}.auth.Authentication; +import {{invokerPackage}}.auth.HttpBasicAuth; +import {{invokerPackage}}.auth.ApiKeyAuth; +import {{invokerPackage}}.auth.OAuth; + +import com.amazon.SellingPartnerAPIAA.AWSSigV4Signer; +import com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner; + +public class ApiClient { + + private String basePath = "{{{basePath}}}"; + private boolean debugging = false; + private Map defaultHeaderMap = new HashMap(); + private String tempFolderPath = null; + + private Map authentications; + + private DateFormat dateFormat; + private DateFormat datetimeFormat; + private boolean lenientDatetimeFormat; + private int dateLength; + + private InputStream sslCaCert; + private boolean verifyingSsl; + private KeyManager[] keyManagers; + + private OkHttpClient httpClient; + private JSON json; + + private HttpLoggingInterceptor loggingInterceptor; + + private LWAAuthorizationSigner lwaAuthorizationSigner; + private AWSSigV4Signer awsSigV4Signer; + + /* + * Constructor for ApiClient + */ + public ApiClient() { + httpClient = new OkHttpClient(); + + {{#useGzipFeature}} + // Enable gzip request compression + httpClient.interceptors().add(new GzipRequestInterceptor()); + {{/useGzipFeature}} + + verifyingSsl = true; + + json = new JSON(); + + // Set default User-Agent. + setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{artifactVersion}}}/java{{/httpUserAgent}}"); + + // Setup authentications (key: authentication name, value: authentication). + authentications = new HashMap();{{#authMethods}}{{#isBasic}} + authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} + authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} + authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} + // Prevent the authentications from being modified. + authentications = Collections.unmodifiableMap(authentications); + } + + /** + * Get base path + * + * @return Baes path + */ + public String getBasePath() { + return basePath; + } + + /** + * Set base path + * + * @param basePath Base path of the URL (e.g {{{basePath}}} + * @return An instance of OkHttpClient + */ + public ApiClient setBasePath(String basePath) { + this.basePath = basePath; + return this; + } + + /** + * Get HTTP client + * + * @return An instance of OkHttpClient + */ + public OkHttpClient getHttpClient() { + return httpClient; + } + + /** + * Set HTTP client + * + * @param httpClient An instance of OkHttpClient + * @return Api Client + */ + public ApiClient setHttpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Get JSON + * + * @return JSON object + */ + public JSON getJSON() { + return json; + } + + /** + * Set JSON + * + * @param json JSON object + * @return Api client + */ + public ApiClient setJSON(JSON json) { + this.json = json; + return this; + } + + /** + * True if isVerifyingSsl flag is on + * + * @return True if isVerifySsl flag is on + */ + public boolean isVerifyingSsl() { + return verifyingSsl; + } + + /** + * Configure whether to verify certificate and hostname when making https requests. + * Default to true. + * NOTE: Do NOT set to false in production code, otherwise you would face multiple types of cryptographic attacks. + * + * @param verifyingSsl True to verify TLS/SSL connection + * @return ApiClient + */ + public ApiClient setVerifyingSsl(boolean verifyingSsl) { + this.verifyingSsl = verifyingSsl; + applySslSettings(); + return this; + } + + /** + * Get SSL CA cert. + * + * @return Input stream to the SSL CA cert + */ + public InputStream getSslCaCert() { + return sslCaCert; + } + + /** + * Configure the CA certificate to be trusted when making https requests. + * Use null to reset to default. + * + * @param sslCaCert input stream for SSL CA cert + * @return ApiClient + */ + public ApiClient setSslCaCert(InputStream sslCaCert) { + this.sslCaCert = sslCaCert; + applySslSettings(); + return this; + } + + public KeyManager[] getKeyManagers() { + return keyManagers; + } + + /** + * Configure client keys to use for authorization in an SSL session. + * Use null to reset to default. + * + * @param managers The KeyManagers to use + * @return ApiClient + */ + public ApiClient setKeyManagers(KeyManager[] managers) { + this.keyManagers = managers; + applySslSettings(); + return this; + } + + public DateFormat getDateFormat() { + return dateFormat; + } + + public ApiClient setDateFormat(DateFormat dateFormat) { + this.json.setDateFormat(dateFormat); + return this; + } + + public ApiClient setSqlDateFormat(DateFormat dateFormat) { + this.json.setSqlDateFormat(dateFormat); + return this; + } + + {{#joda}} + public ApiClient setDateTimeFormat(DateTimeFormatter dateFormat) { + this.json.setDateTimeFormat(dateFormat); + return this; + } + + public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { + this.json.setLocalDateFormat(dateFormat); + return this; + } + + {{/joda}} + {{#jsr310}} + public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { + this.json.setOffsetDateTimeFormat(dateFormat); + return this; + } + + public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { + this.json.setLocalDateFormat(dateFormat); + return this; + } + + {{/jsr310}} + public ApiClient setLenientOnJson(boolean lenientOnJson) { + this.json.setLenientOnJson(lenientOnJson); + return this; + } + + /** + * Get authentications (key: authentication name, value: authentication). + * + * @return Map of authentication objects + */ + public Map getAuthentications() { + return authentications; + } + + /** + * Get authentication for the given name. + * + * @param authName The authentication name + * @return The authentication, null if not found + */ + public Authentication getAuthentication(String authName) { + return authentications.get(authName); + } + + /** + * Helper method to set username for the first HTTP basic authentication. + * + * @param username Username + */ + public void setUsername(String username) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setUsername(username); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** + * Helper method to set password for the first HTTP basic authentication. + * + * @param password Password + */ + public void setPassword(String password) { + for (Authentication auth : authentications.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setPassword(password); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** + * Helper method to set API key value for the first API key authentication. + * + * @param apiKey API key + */ + public void setApiKey(String apiKey) { + for (Authentication auth : authentications.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKey(apiKey); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** + * Helper method to set API key prefix for the first API key authentication. + * + * @param apiKeyPrefix API key prefix + */ + public void setApiKeyPrefix(String apiKeyPrefix) { + for (Authentication auth : authentications.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** + * Helper method to set access token for the first OAuth2 authentication. + * + * @param accessToken Access token + */ + public void setAccessToken(String accessToken) { + for (Authentication auth : authentications.values()) { + if (auth instanceof OAuth) { + ((OAuth) auth).setAccessToken(accessToken); + return; + } + } + throw new RuntimeException("No OAuth2 authentication configured!"); + } + + /** + * Set the User-Agent header's value (by adding to the default header map). + * + * @param userAgent HTTP request's user agent + * @return ApiClient + */ + public ApiClient setUserAgent(String userAgent) { + addDefaultHeader("User-Agent", userAgent); + return this; + } + + /** + * Add a default header. + * + * @param key The header's key + * @param value The header's value + * @return ApiClient + */ + public ApiClient addDefaultHeader(String key, String value) { + defaultHeaderMap.put(key, value); + return this; + } + + /** + * Check that whether debugging is enabled for this API client. + * + * @return True if debugging is enabled, false otherwise. + */ + public boolean isDebugging() { + return debugging; + } + + /** + * Enable/disable debugging for this API client. + * + * @param debugging To enable (true) or disable (false) debugging + * @return ApiClient + */ + public ApiClient setDebugging(boolean debugging) { + if (debugging != this.debugging) { + if (debugging) { + loggingInterceptor = new HttpLoggingInterceptor(); + loggingInterceptor.setLevel(Level.BODY); + httpClient.interceptors().add(loggingInterceptor); + } else { + httpClient.interceptors().remove(loggingInterceptor); + loggingInterceptor = null; + } + } + this.debugging = debugging; + return this; + } + + /** + * The path of temporary folder used to store downloaded files from endpoints + * with file response. The default value is null, i.e. using + * the system's default tempopary folder. + * + * @see createTempFile + * @return Temporary folder path + */ + public String getTempFolderPath() { + return tempFolderPath; + } + + /** + * Set the temporary folder path (for downloading files) + * + * @param tempFolderPath Temporary folder path + * @return ApiClient + */ + public ApiClient setTempFolderPath(String tempFolderPath) { + this.tempFolderPath = tempFolderPath; + return this; + } + + /** + * Get connection timeout (in milliseconds). + * + * @return Timeout in milliseconds + */ + public int getConnectTimeout() { + return httpClient.getConnectTimeout(); + } + + /** + * Sets the connect timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + * + * @param connectionTimeout connection timeout in milliseconds + * @return Api client + */ + public ApiClient setConnectTimeout(int connectionTimeout) { + httpClient.setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS); + return this; + } + + /** + * Get read timeout (in milliseconds). + * + * @return Timeout in milliseconds + */ + public int getReadTimeout() { + return httpClient.getReadTimeout(); + } + + /** + * Sets the read timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + * + * @param readTimeout read timeout in milliseconds + * @return Api client + */ + public ApiClient setReadTimeout(int readTimeout) { + httpClient.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS); + return this; + } + + /** + * Get write timeout (in milliseconds). + * + * @return Timeout in milliseconds + */ + public int getWriteTimeout() { + return httpClient.getWriteTimeout(); + } + + /** + * Sets the write timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + * + * @param writeTimeout connection timeout in milliseconds + * @return Api client + */ + public ApiClient setWriteTimeout(int writeTimeout) { + httpClient.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS); + return this; + } + + /** + * Sets the LWAAuthorizationSigner + * + * @param lwaAuthorizationSigner LWAAuthorizationSigner instance + * @return Api client + */ + public ApiClient setLWAAuthorizationSigner(LWAAuthorizationSigner lwaAuthorizationSigner) { + this.lwaAuthorizationSigner = lwaAuthorizationSigner; + return this; + } + + /** + * Sets the AWSSigV4Signer + * + * @param awsSigV4Signer AWSSigV4Signer instance + * @return Api client + */ + public ApiClient setAWSSigV4Signer(AWSSigV4Signer awsSigV4Signer) { + this.awsSigV4Signer = awsSigV4Signer; + return this; + } + + /** + * Format the given parameter object into string. + * + * @param param Parameter + * @return String representation of the parameter + */ + public String parameterToString(Object param) { + if (param == null) { + return ""; + } else if (param instanceof Date {{#joda}}|| param instanceof DateTime || param instanceof LocalDate{{/joda}}{{#jsr310}}|| param instanceof OffsetDateTime || param instanceof LocalDate{{/jsr310}}) { + //Serialize to json string and remove the " enclosing characters + String jsonStr = json.serialize(param); + return jsonStr.substring(1, jsonStr.length() - 1); + } else if (param instanceof Collection) { + StringBuilder b = new StringBuilder(); + for (Object o : (Collection)param) { + if (b.length() > 0) { + b.append(","); + } + b.append(String.valueOf(o)); + } + return b.toString(); + } else { + return String.valueOf(param); + } + } + + /** + * Formats the specified query parameter to a list containing a single {@code Pair} object. + * + * Note that {@code value} must not be a collection. + * + * @param name The name of the parameter. + * @param value The value of the parameter. + * @return A list containing a single {@code Pair} object. + */ + public List parameterToPair(String name, Object value) { + List params = new ArrayList(); + + // preconditions + if (name == null || name.isEmpty() || value == null || value instanceof Collection) return params; + + params.add(new Pair(name, parameterToString(value))); + return params; + } + + /** + * Formats the specified collection query parameters to a list of {@code Pair} objects. + * + * Note that the values of each of the returned Pair objects are percent-encoded. + * + * @param collectionFormat The collection format of the parameter. + * @param name The name of the parameter. + * @param value The value of the parameter. + * @return A list of {@code Pair} objects. + */ + public List parameterToPairs(String collectionFormat, String name, Collection value) { + List params = new ArrayList(); + + // preconditions + if (name == null || name.isEmpty() || value == null || value.isEmpty()) { + return params; + } + + // create the params based on the collection format + if ("multi".equals(collectionFormat)) { + for (Object item : value) { + params.add(new Pair(name, escapeString(parameterToString(item)))); + } + return params; + } + + // collectionFormat is assumed to be "csv" by default + String delimiter = ","; + + // escape all delimiters except commas, which are URI reserved + // characters + if ("ssv".equals(collectionFormat)) { + delimiter = escapeString(" "); + } else if ("tsv".equals(collectionFormat)) { + delimiter = escapeString("\t"); + } else if ("pipes".equals(collectionFormat)) { + delimiter = escapeString("|"); + } + + StringBuilder sb = new StringBuilder() ; + for (Object item : value) { + sb.append(delimiter); + sb.append(escapeString(parameterToString(item))); + } + + params.add(new Pair(name, sb.substring(delimiter.length()))); + + return params; + } + + /** + * Sanitize filename by removing path. + * e.g. ../../sun.gif becomes sun.gif + * + * @param filename The filename to be sanitized + * @return The sanitized filename + */ + public String sanitizeFilename(String filename) { + return filename.replaceAll(".*[/\\\\]", ""); + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * "* / *" is also default to JSON + * @param mime MIME (Multipurpose Internet Mail Extensions) + * @return True if the given MIME is JSON, false otherwise. + */ + public boolean isJsonMime(String mime) { + String jsonMime = "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$"; + return mime != null && (mime.matches(jsonMime) || mime.equals("*/*")); + } + + /** + * Select the Accept header's value from the given accepts array: + * if JSON exists in the given array, use it; + * otherwise use all of them (joining into a string) + * + * @param accepts The accepts array to select from + * @return The Accept header to use. If the given array is empty, + * null will be returned (not to set the Accept header explicitly). + */ + public String selectHeaderAccept(String[] accepts) { + if (accepts.length == 0) { + return null; + } + for (String accept : accepts) { + if (isJsonMime(accept)) { + return accept; + } + } + return StringUtil.join(accepts, ","); + } + + /** + * Select the Content-Type header's value from the given array: + * if JSON exists in the given array, use it; + * otherwise use the first one of the array. + * + * @param contentTypes The Content-Type array to select from + * @return The Content-Type header to use. If the given array is empty, + * or matches "any", JSON will be used. + */ + public String selectHeaderContentType(String[] contentTypes) { + if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) { + return "application/json"; + } + for (String contentType : contentTypes) { + if (isJsonMime(contentType)) { + return contentType; + } + } + return contentTypes[0]; + } + + /** + * Escape the given string to be used as URL query value. + * + * @param str String to be escaped + * @return Escaped string + */ + public String escapeString(String str) { + try { + return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20"); + } catch (UnsupportedEncodingException e) { + return str; + } + } + + /** + * Deserialize response body to Java object, according to the return type and + * the Content-Type response header. + * + * @param Type + * @param response HTTP response + * @param returnType The type of the Java object + * @return The deserialized Java object + * @throws ApiException If fail to deserialize response body, i.e. cannot read response body + * or the Content-Type of the response is not supported. + */ + @SuppressWarnings("unchecked") + public T deserialize(Response response, Type returnType) throws ApiException { + if (response == null || returnType == null) { + return null; + } + + if ("byte[]".equals(returnType.toString())) { + // Handle binary response (byte array). + try { + return (T) response.body().bytes(); + } catch (IOException e) { + throw new ApiException(e); + } + } else if (returnType.equals(File.class)) { + // Handle file downloading. + return (T) downloadFileFromResponse(response); + } + + String respBody; + try { + if (response.body() != null) + respBody = response.body().string(); + else + respBody = null; + } catch (IOException e) { + throw new ApiException(e); + } + + if (respBody == null || "".equals(respBody)) { + return null; + } + + String contentType = response.headers().get("Content-Type"); + if (contentType == null) { + // ensuring a default content type + contentType = "application/json"; + } + if (isJsonMime(contentType)) { + return json.deserialize(respBody, returnType); + } else if (returnType.equals(String.class)) { + // Expecting string, return the raw response body. + return (T) respBody; + } else { + throw new ApiException( + "Content type \"" + contentType + "\" is not supported for type: " + returnType, + response.code(), + response.headers().toMultimap(), + respBody); + } + } + + /** + * Serialize the given Java object into request body according to the object's + * class and the request Content-Type. + * + * @param obj The Java object + * @param contentType The request Content-Type + * @return The serialized request body + * @throws ApiException If fail to serialize the given object + */ + public RequestBody serialize(Object obj, String contentType) throws ApiException { + if (obj instanceof byte[]) { + // Binary (byte array) body parameter support. + return RequestBody.create(MediaType.parse(contentType), (byte[]) obj); + } else if (obj instanceof File) { + // File body parameter support. + return RequestBody.create(MediaType.parse(contentType), (File) obj); + } else if (isJsonMime(contentType)) { + String content; + if (obj != null) { + content = json.serialize(obj); + } else { + content = null; + } + return RequestBody.create(MediaType.parse(contentType), content); + } else { + throw new ApiException("Content type \"" + contentType + "\" is not supported"); + } + } + + /** + * Download file from the given response. + * + * @param response An instance of the Response object + * @throws ApiException If fail to read file content from response and write to disk + * @return Downloaded file + */ + public File downloadFileFromResponse(Response response) throws ApiException { + try { + File file = prepareDownloadFile(response); + BufferedSink sink = Okio.buffer(Okio.sink(file)); + sink.writeAll(response.body().source()); + sink.close(); + return file; + } catch (IOException e) { + throw new ApiException(e); + } + } + + /** + * Prepare file for download + * + * @param response An instance of the Response object + * @throws IOException If fail to prepare file for download + * @return Prepared file for the download + */ + public File prepareDownloadFile(Response response) throws IOException { + String filename = null; + String contentDisposition = response.header("Content-Disposition"); + if (contentDisposition != null && !"".equals(contentDisposition)) { + // Get filename from the Content-Disposition header. + Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?"); + Matcher matcher = pattern.matcher(contentDisposition); + if (matcher.find()) { + filename = sanitizeFilename(matcher.group(1)); + } + } + + String prefix = null; + String suffix = null; + if (filename == null) { + prefix = "download-"; + suffix = ""; + } else { + int pos = filename.lastIndexOf("."); + if (pos == -1) { + prefix = filename + "-"; + } else { + prefix = filename.substring(0, pos) + "-"; + suffix = filename.substring(pos); + } + // File.createTempFile requires the prefix to be at least three characters long + if (prefix.length() < 3) + prefix = "download-"; + } + + if (tempFolderPath == null) + return File.createTempFile(prefix, suffix); + else + return File.createTempFile(prefix, suffix, new File(tempFolderPath)); + } + + /** + * {@link #execute(Call, Type)} + * + * @param Type + * @param call An instance of the Call object + * @throws ApiException If fail to execute the call + * @return ApiResponse<T> + */ + public ApiResponse execute(Call call) throws ApiException { + return execute(call, null); + } + + /** + * Execute HTTP call and deserialize the HTTP response body into the given return type. + * + * @param returnType The return type used to deserialize HTTP response body + * @param The return type corresponding to (same with) returnType + * @param call Call + * @return ApiResponse object containing response status, headers and + * data, which is a Java object deserialized from response body and would be null + * when returnType is null. + * @throws ApiException If fail to execute the call + */ + public ApiResponse execute(Call call, Type returnType) throws ApiException { + try { + Response response = call.execute(); + T data = handleResponse(response, returnType); + return new ApiResponse(response.code(), response.headers().toMultimap(), data); + } catch (IOException e) { + throw new ApiException(e); + } + } + + /** + * {@link #executeAsync(Call, Type, ApiCallback)} + * + * @param Type + * @param call An instance of the Call object + * @param callback ApiCallback<T> + */ + public void executeAsync(Call call, ApiCallback callback) { + executeAsync(call, null, callback); + } + + /** + * Execute HTTP call asynchronously. + * + * @see #execute(Call, Type) + * @param Type + * @param call The callback to be executed when the API call finishes + * @param returnType Return type + * @param callback ApiCallback + */ + @SuppressWarnings("unchecked") + public void executeAsync(Call call, final Type returnType, final ApiCallback callback) { + call.enqueue(new Callback() { + @Override + public void onFailure(Request request, IOException e) { + callback.onFailure(new ApiException(e), 0, null); + } + + @Override + public void onResponse(Response response) throws IOException { + T result; + try { + result = (T) handleResponse(response, returnType); + } catch (ApiException e) { + callback.onFailure(e, response.code(), response.headers().toMultimap()); + return; + } + callback.onSuccess(result, response.code(), response.headers().toMultimap()); + } + }); + } + + /** + * Handle the given response, return the deserialized object when the response is successful. + * + * @param Type + * @param response Response + * @param returnType Return type + * @throws ApiException If the response has a unsuccessful status code or + * fail to deserialize the response body + * @return Type + */ + public T handleResponse(Response response, Type returnType) throws ApiException { + if (response.isSuccessful()) { + if (returnType == null || response.code() == 204) { + // returning null if the returnType is not defined, + // or the status code is 204 (No Content) + if (response.body() != null) { + try { + response.body().close(); + } catch (IOException e) { + throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); + } + } + return null; + } else { + return deserialize(response, returnType); + } + } else { + String respBody = null; + if (response.body() != null) { + try { + respBody = response.body().string(); + } catch (IOException e) { + throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); + } + } + throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody); + } + } + + /** + * Build HTTP call with the given options. + * + * @param path The sub-path of the HTTP URL + * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" + * @param queryParams The query parameters + * @param collectionQueryParams The collection query parameters + * @param body The request body object + * @param headerParams The header parameters + * @param formParams The form parameters + * @param authNames The authentications to apply + * @param progressRequestListener Progress request listener + * @return The HTTP call + * @throws ApiException If fail to serialize the request body object + */ + public Call buildCall(String path, String method, List queryParams, List collectionQueryParams, Object body, Map headerParams, Map formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener); + + return httpClient.newCall(request); + } + + /** + * Build an HTTP request with the given options. + * + * @param path The sub-path of the HTTP URL + * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" + * @param queryParams The query parameters + * @param collectionQueryParams The collection query parameters + * @param body The request body object + * @param headerParams The header parameters + * @param formParams The form parameters + * @param authNames The authentications to apply + * @param progressRequestListener Progress request listener + * @return The HTTP request + * @throws ApiException If fail to serialize the request body object + */ + public Request buildRequest(String path, String method, List queryParams, List collectionQueryParams, Object body, Map headerParams, Map formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + updateParamsForAuth(authNames, queryParams, headerParams); + + final String url = buildUrl(path, queryParams, collectionQueryParams); + final Request.Builder reqBuilder = new Request.Builder().url(url); + processHeaderParams(headerParams, reqBuilder); + + String contentType = (String) headerParams.get("Content-Type"); + // ensuring a default content type + if (contentType == null) { + contentType = "application/json"; + } + + RequestBody reqBody; + if (!HttpMethod.permitsRequestBody(method)) { + reqBody = null; + } else if ("application/x-www-form-urlencoded".equals(contentType)) { + reqBody = buildRequestBodyFormEncoding(formParams); + } else if ("multipart/form-data".equals(contentType)) { + reqBody = buildRequestBodyMultipart(formParams); + } else if (body == null) { + if ("DELETE".equals(method)) { + // allow calling DELETE without sending a request body + reqBody = null; + } else { + // use an empty request body (for POST, PUT and PATCH) + reqBody = RequestBody.create(MediaType.parse(contentType), ""); + } + } else { + reqBody = serialize(body, contentType); + } + + Request request = null; + + if(progressRequestListener != null && reqBody != null) { + ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener); + request = reqBuilder.method(method, progressRequestBody).build(); + } else { + request = reqBuilder.method(method, reqBody).build(); + } + + request = lwaAuthorizationSigner.sign(request); + request = awsSigV4Signer.sign(request); + + return request; + } + + /** + * Build full URL by concatenating base path, the given sub path and query parameters. + * + * @param path The sub path + * @param queryParams The query parameters + * @param collectionQueryParams The collection query parameters + * @return The full URL + */ + public String buildUrl(String path, List queryParams, List collectionQueryParams) { + final StringBuilder url = new StringBuilder(); + url.append(basePath).append(path); + + if (queryParams != null && !queryParams.isEmpty()) { + // support (constant) query string in `path`, e.g. "/posts?draft=1" + String prefix = path.contains("?") ? "&" : "?"; + for (Pair param : queryParams) { + if (param.getValue() != null) { + if (prefix != null) { + url.append(prefix); + prefix = null; + } else { + url.append("&"); + } + String value = parameterToString(param.getValue()); + url.append(escapeString(param.getName())).append("=").append(escapeString(value)); + } + } + } + + if (collectionQueryParams != null && !collectionQueryParams.isEmpty()) { + String prefix = url.toString().contains("?") ? "&" : "?"; + for (Pair param : collectionQueryParams) { + if (param.getValue() != null) { + if (prefix != null) { + url.append(prefix); + prefix = null; + } else { + url.append("&"); + } + String value = parameterToString(param.getValue()); + // collection query parameter value already escaped as part of parameterToPairs + url.append(escapeString(param.getName())).append("=").append(value); + } + } + } + + return url.toString(); + } + + /** + * Set header parameters to the request builder, including default headers. + * + * @param headerParams Header parameters in the ofrm of Map + * @param reqBuilder Reqeust.Builder + */ + public void processHeaderParams(Map headerParams, Request.Builder reqBuilder) { + for (Entry param : headerParams.entrySet()) { + reqBuilder.header(param.getKey(), parameterToString(param.getValue())); + } + for (Entry header : defaultHeaderMap.entrySet()) { + if (!headerParams.containsKey(header.getKey())) { + reqBuilder.header(header.getKey(), parameterToString(header.getValue())); + } + } + } + + /** + * Update query and header parameters based on authentication settings. + * + * @param authNames The authentications to apply + * @param queryParams List of query parameters + * @param headerParams Map of header parameters + */ + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + for (String authName : authNames) { + Authentication auth = authentications.get(authName); + if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); + auth.applyToParams(queryParams, headerParams); + } + } + + /** + * Build a form-encoding request body with the given form parameters. + * + * @param formParams Form parameters in the form of Map + * @return RequestBody + */ + public RequestBody buildRequestBodyFormEncoding(Map formParams) { + FormEncodingBuilder formBuilder = new FormEncodingBuilder(); + for (Entry param : formParams.entrySet()) { + formBuilder.add(param.getKey(), parameterToString(param.getValue())); + } + return formBuilder.build(); + } + + /** + * Build a multipart (file uploading) request body with the given form parameters, + * which could contain text fields and file fields. + * + * @param formParams Form parameters in the form of Map + * @return RequestBody + */ + public RequestBody buildRequestBodyMultipart(Map formParams) { + MultipartBuilder mpBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); + for (Entry param : formParams.entrySet()) { + if (param.getValue() instanceof File) { + File file = (File) param.getValue(); + Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); + MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); + mpBuilder.addPart(partHeaders, RequestBody.create(mediaType, file)); + } else { + Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); + mpBuilder.addPart(partHeaders, RequestBody.create(null, parameterToString(param.getValue()))); + } + } + return mpBuilder.build(); + } + + /** + * Guess Content-Type header from the given file (defaults to "application/octet-stream"). + * + * @param file The given file + * @return The guessed Content-Type + */ + public String guessContentTypeFromFile(File file) { + String contentType = URLConnection.guessContentTypeFromName(file.getName()); + if (contentType == null) { + return "application/octet-stream"; + } else { + return contentType; + } + } + + /** + * Apply SSL related settings to httpClient according to the current values of + * verifyingSsl and sslCaCert. + */ + private void applySslSettings() { + try { + TrustManager[] trustManagers = null; + HostnameVerifier hostnameVerifier = null; + if (!verifyingSsl) { + TrustManager trustAll = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} + @Override + public X509Certificate[] getAcceptedIssuers() { return null; } + }; + SSLContext sslContext = SSLContext.getInstance("TLS"); + trustManagers = new TrustManager[]{ trustAll }; + hostnameVerifier = new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { return true; } + }; + } else if (sslCaCert != null) { + char[] password = null; // Any password will work. + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); + Collection certificates = certificateFactory.generateCertificates(sslCaCert); + if (certificates.isEmpty()) { + throw new IllegalArgumentException("expected non-empty set of trusted certificates"); + } + KeyStore caKeyStore = newEmptyKeyStore(password); + int index = 0; + for (Certificate certificate : certificates) { + String certificateAlias = "ca" + Integer.toString(index++); + caKeyStore.setCertificateEntry(certificateAlias, certificate); + } + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init(caKeyStore); + trustManagers = trustManagerFactory.getTrustManagers(); + } + + if (keyManagers != null || trustManagers != null) { + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(keyManagers, trustManagers, new SecureRandom()); + httpClient.setSslSocketFactory(sslContext.getSocketFactory()); + } else { + httpClient.setSslSocketFactory(null); + } + httpClient.setHostnameVerifier(hostnameVerifier); + } catch (GeneralSecurityException e) { + throw new RuntimeException(e); + } + } + + private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException { + try { + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, password); + return keyStore; + } catch (IOException e) { + throw new AssertionError(e); + } + } +} diff --git a/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/StringUtil.mustache b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/StringUtil.mustache new file mode 100644 index 0000000..a9343a9 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/StringUtil.mustache @@ -0,0 +1,54 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +{{>generatedAnnotation}} +public class StringUtil { + /** + * Check if the given array contains the given value (with case-insensitive comparison). + * + * @param array The array + * @param value The value to search + * @return true if the array contains the value + */ + public static boolean containsIgnoreCase(String[] array, String value) { + for (String str : array) { + if (value == null && str == null) return true; + if (value != null && value.equalsIgnoreCase(str)) return true; + } + return false; + } + + /** + * Join an array of strings with the given separator. + *

+ * Note: This might be replaced by utility method from commons-lang or guava someday + * if one of those libraries is added as dependency. + *

+ * + * @param array The array of strings + * @param separator The separator + * @return the resulting string + */ + public static String join(String[] array, String separator) { + int len = array.length; + if (len == 0) return ""; + + StringBuilder out = new StringBuilder(); + out.append(array[0]); + for (int i = 1; i < len; i++) { + out.append(separator).append(array[i]); + } + return out.toString(); + } + + /** + * Check if the given value is null or an empty string + * + * @param value The value to check + * @return true if the value is null or empty + */ + public static boolean isEmpty(String value) { + return value == null || value.isEmpty(); + } +} diff --git a/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/api.mustache b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/api.mustache new file mode 100644 index 0000000..6d13e68 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/resources/swagger-codegen/templates/api.mustache @@ -0,0 +1,317 @@ +{{>licenseInfo}} + +package {{package}}; + +import {{invokerPackage}}.ApiCallback; +import {{invokerPackage}}.ApiClient; +import {{invokerPackage}}.ApiException; +import {{invokerPackage}}.ApiResponse; +import {{invokerPackage}}.Configuration; +import {{invokerPackage}}.Pair; +import {{invokerPackage}}.ProgressRequestBody; +import {{invokerPackage}}.ProgressResponseBody; +import {{invokerPackage}}.StringUtil; +{{#performBeanValidation}} +import {{invokerPackage}}.BeanValidationException; +{{/performBeanValidation}} + +import com.google.gson.reflect.TypeToken; + +import java.io.IOException; + +{{#useBeanValidation}} +import javax.validation.constraints.*; +{{/useBeanValidation}} +{{#performBeanValidation}} +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.executable.ExecutableValidator; +import java.util.Set; +import java.lang.reflect.Method; +import java.lang.reflect.Type; +{{/performBeanValidation}} + +{{#imports}}import {{import}}; +{{/imports}} + +import java.lang.reflect.Type; +{{^fullJavaUtil}} +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +{{/fullJavaUtil}} + +import com.amazon.SellingPartnerAPIAA.AWSSigV4Signer; +import com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner; +import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials; +import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials; + +{{#operations}} +public class {{classname}} { + private ApiClient {{localVariablePrefix}}apiClient; + + {{classname}}() { + this(Configuration.getDefaultApiClient()); + } + + public {{classname}}(ApiClient apiClient) { + this.{{localVariablePrefix}}apiClient = apiClient; + } + + public ApiClient getApiClient() { + return {{localVariablePrefix}}apiClient; + } + + public void setApiClient(ApiClient apiClient) { + this.{{localVariablePrefix}}apiClient = apiClient; + } + + {{#operation}} + /** + * Build call for {{operationId}}{{#allParams}} + * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}} + * @param progressListener Progress listener + * @param progressRequestListener Progress request listener + * @return Call to execute + * @throws ApiException If fail to serialize the request body object + {{#isDeprecated}} + * @deprecated + {{/isDeprecated}} + {{#externalDocs}} + * {{description}} + * @see {{summary}} Documentation + {{/externalDocs}} + */ + {{#isDeprecated}} + @Deprecated + {{/isDeprecated}} + public com.squareup.okhttp.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + Object {{localVariablePrefix}}localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; + + // create path and map variables + String {{localVariablePrefix}}localVarPath = "{{{path}}}"{{#pathParams}} + .replaceAll("\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}}; + + {{javaUtilPrefix}}List {{localVariablePrefix}}localVarQueryParams = new {{javaUtilPrefix}}ArrayList(); + {{javaUtilPrefix}}List {{localVariablePrefix}}localVarCollectionQueryParams = new {{javaUtilPrefix}}ArrayList();{{#queryParams}} + if ({{paramName}} != null) + {{localVariablePrefix}}{{#collectionFormat}}localVarCollectionQueryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll({{localVariablePrefix}}apiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));{{/queryParams}} + + {{javaUtilPrefix}}Map {{localVariablePrefix}}localVarHeaderParams = new {{javaUtilPrefix}}HashMap();{{#headerParams}} + if ({{paramName}} != null) + {{localVariablePrefix}}localVarHeaderParams.put("{{baseName}}", {{localVariablePrefix}}apiClient.parameterToString({{paramName}}));{{/headerParams}} + + {{javaUtilPrefix}}Map {{localVariablePrefix}}localVarFormParams = new {{javaUtilPrefix}}HashMap();{{#formParams}} + if ({{paramName}} != null) + {{localVariablePrefix}}localVarFormParams.put("{{baseName}}", {{paramName}});{{/formParams}} + + final String[] {{localVariablePrefix}}localVarAccepts = { + {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} + }; + final String {{localVariablePrefix}}localVarAccept = {{localVariablePrefix}}apiClient.selectHeaderAccept({{localVariablePrefix}}localVarAccepts); + if ({{localVariablePrefix}}localVarAccept != null) {{localVariablePrefix}}localVarHeaderParams.put("Accept", {{localVariablePrefix}}localVarAccept); + + final String[] {{localVariablePrefix}}localVarContentTypes = { + {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} + }; + final String {{localVariablePrefix}}localVarContentType = {{localVariablePrefix}}apiClient.selectHeaderContentType({{localVariablePrefix}}localVarContentTypes); + {{localVariablePrefix}}localVarHeaderParams.put("Content-Type", {{localVariablePrefix}}localVarContentType); + + if(progressListener != null) { + apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() { + @Override + public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException { + com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request()); + return originalResponse.newBuilder() + .body(new ProgressResponseBody(originalResponse.body(), progressListener)) + .build(); + } + }); + } + + String[] {{localVariablePrefix}}localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; + return {{localVariablePrefix}}apiClient.buildCall({{localVariablePrefix}}localVarPath, "{{httpMethod}}", {{localVariablePrefix}}localVarQueryParams, {{localVariablePrefix}}localVarCollectionQueryParams, {{localVariablePrefix}}localVarPostBody, {{localVariablePrefix}}localVarHeaderParams, {{localVariablePrefix}}localVarFormParams, {{localVariablePrefix}}localVarAuthNames, progressRequestListener); + } + + {{#isDeprecated}} + @Deprecated + {{/isDeprecated}} + @SuppressWarnings("rawtypes") + private com.squareup.okhttp.Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + {{^performBeanValidation}} + {{#allParams}}{{#required}} + // verify the required parameter '{{paramName}}' is set + if ({{paramName}} == null) { + throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)"); + } + {{/required}}{{/allParams}} + + com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); + return {{localVariablePrefix}}call; + + {{/performBeanValidation}} + {{#performBeanValidation}} + try { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + ExecutableValidator executableValidator = factory.getValidator().forExecutables(); + + Object[] parameterValues = { {{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}} }; + Method method = this.getClass().getMethod("{{operationId}}WithHttpInfo"{{#allParams}}, {{#isListContainer}}java.util.List{{/isListContainer}}{{#isMapContainer}}java.util.Map{{/isMapContainer}}{{^isListContainer}}{{^isMapContainer}}{{{dataType}}}{{/isMapContainer}}{{/isListContainer}}.class{{/allParams}}); + Set> violations = executableValidator.validateParameters(this, method, + parameterValues); + + if (violations.size() == 0) { + com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); + return {{localVariablePrefix}}call; + + } else { + throw new BeanValidationException((Set) violations); + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new ApiException(e.getMessage()); + } catch (SecurityException e) { + e.printStackTrace(); + throw new ApiException(e.getMessage()); + } + + {{/performBeanValidation}} + } + + /** + * {{summary}} + * {{notes}}{{#allParams}} + * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}{{#returnType}} + * @return {{returnType}}{{/returnType}} + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + {{#isDeprecated}} + * @deprecated + {{/isDeprecated}} + {{#externalDocs}} + * {{description}} + * @see {{summary}} Documentation + {{/externalDocs}} + */ + {{#isDeprecated}} + @Deprecated + {{/isDeprecated}} + public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + {{#returnType}}ApiResponse<{{{returnType}}}> {{localVariablePrefix}}resp = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}} + return {{localVariablePrefix}}resp.getData();{{/returnType}} + } + + /** + * {{summary}} + * {{notes}}{{#allParams}} + * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}} + * @return ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}> + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + {{#isDeprecated}} + * @deprecated + {{/isDeprecated}} + {{#externalDocs}} + * {{description}} + * @see {{summary}} Documentation + {{/externalDocs}} + */ + {{#isDeprecated}} + @Deprecated + {{/isDeprecated}} + public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}null, null); + {{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); + return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType);{{/returnType}}{{^returnType}}return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}} + } + + /** + * {{summary}} (asynchronously) + * {{notes}}{{#allParams}} + * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}} + * @param callback The callback to be executed when the API call finishes + * @return The request call + * @throws ApiException If fail to process the API call, e.g. serializing the request body object + {{#isDeprecated}} + * @deprecated + {{/isDeprecated}} + {{#externalDocs}} + * {{description}} + * @see {{summary}} Documentation + {{/externalDocs}} + */ + {{#isDeprecated}} + @Deprecated + {{/isDeprecated}} + public com.squareup.okhttp.Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException { + + ProgressResponseBody.ProgressListener progressListener = null; + ProgressRequestBody.ProgressRequestListener progressRequestListener = null; + + if (callback != null) { + progressListener = new ProgressResponseBody.ProgressListener() { + @Override + public void update(long bytesRead, long contentLength, boolean done) { + callback.onDownloadProgress(bytesRead, contentLength, done); + } + }; + + progressRequestListener = new ProgressRequestBody.ProgressRequestListener() { + @Override + public void onRequestProgress(long bytesWritten, long contentLength, boolean done) { + callback.onUploadProgress(bytesWritten, contentLength, done); + } + }; + } + + com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); + {{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); + {{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType, {{localVariablePrefix}}callback);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}callback);{{/returnType}} + return {{localVariablePrefix}}call; + } + {{/operation}} + + public static class Builder { + private AWSAuthenticationCredentials awsAuthenticationCredentials; + private LWAAuthorizationCredentials lwaAuthorizationCredentials; + private String endpoint; + + public Builder awsAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) { + this.awsAuthenticationCredentials = awsAuthenticationCredentials; + return this; + } + + public Builder lwaAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials) { + this.lwaAuthorizationCredentials = lwaAuthorizationCredentials; + return this; + } + + public Builder endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + public {{classname}} build() { + if (awsAuthenticationCredentials == null) { + throw new RuntimeException("AWSAuthenticationCredentials not set"); + } + + if (lwaAuthorizationCredentials == null) { + throw new RuntimeException("LWAAuthorizationCredentials not set"); + } + + if (StringUtil.isEmpty(endpoint)) { + throw new RuntimeException("Endpoint not set"); + } + + AWSSigV4Signer awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials); + LWAAuthorizationSigner lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials); + + return new {{classname}}(new ApiClient() + .setAWSSigV4Signer(awsSigV4Signer) + .setLWAAuthorizationSigner(lwaAuthorizationSigner) + .setBasePath(endpoint)); + } + } +} +{{/operations}} \ No newline at end of file diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSAuthenticationCredentials.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSAuthenticationCredentials.java new file mode 100644 index 0000000..6a7026f --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSAuthenticationCredentials.java @@ -0,0 +1,30 @@ +package com.amazon.SellingPartnerAPIAA; + +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; + +/** + * AWSAuthenticationCredentials + */ +@Data +@Builder +public class AWSAuthenticationCredentials { + /** + * AWS IAM User Access Key Id + */ + @NonNull + private String accessKeyId; + + /** + * AWS IAM User Secret Key + */ + @NonNull + private String secretKey; + + /** + * AWS Region + */ + @NonNull + private String region; +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSSigV4Signer.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSSigV4Signer.java new file mode 100644 index 0000000..ba4e9f0 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/AWSSigV4Signer.java @@ -0,0 +1,48 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.amazonaws.SignableRequest; +import com.amazonaws.auth.AWS4Signer; +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.squareup.okhttp.Request; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +/** + * AWS Signature Version 4 Signer + */ +public class AWSSigV4Signer { + private static final String SERVICE_NAME = "execute-api"; + + @Setter(AccessLevel.PACKAGE) + @Getter(AccessLevel.PACKAGE) + private AWS4Signer aws4Signer; + + private AWSCredentials awsCredentials; + + /** + * + * @param awsAuthenticationCredentials AWS Developer Account Credentials + */ + public AWSSigV4Signer(AWSAuthenticationCredentials awsAuthenticationCredentials) { + aws4Signer = new AWS4Signer(); + aws4Signer.setServiceName(SERVICE_NAME); + aws4Signer.setRegionName(awsAuthenticationCredentials.getRegion()); + awsCredentials = new BasicAWSCredentials(awsAuthenticationCredentials.getAccessKeyId(), + awsAuthenticationCredentials.getSecretKey()); + } + + /** + * Signs a Request with AWS Signature Version 4 + * + * @param originalRequest Request to sign (treated as immutable) + * @return Copy of originalRequest with AWS Signature + */ + public Request sign(Request originalRequest) { + SignableRequest signableRequest = new SignableRequestImpl(originalRequest); + aws4Signer.sign(signableRequest, awsCredentials); + + return (Request) signableRequest.getOriginalRequestObject(); + } +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAccessTokenRequestMeta.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAccessTokenRequestMeta.java new file mode 100644 index 0000000..2313397 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAccessTokenRequestMeta.java @@ -0,0 +1,25 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.google.gson.annotations.SerializedName; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +class LWAAccessTokenRequestMeta { + @SerializedName("grant_type") + private String grantType; + + @SerializedName("refresh_token") + private String refreshToken; + + @SerializedName("client_id") + private String clientId; + + @SerializedName("client_secret") + private String clientSecret; + + @SerializedName("scope") + private LWAClientScopes scopes; + +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationCredentials.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationCredentials.java new file mode 100644 index 0000000..c9dee81 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationCredentials.java @@ -0,0 +1,65 @@ +package com.amazon.SellingPartnerAPIAA; + +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; + +import java.util.Arrays; +import java.util.HashSet; + +/** + * LWAAuthorizationCredentials + */ +@Data +@Builder +public class LWAAuthorizationCredentials { + /** + * LWA Client Id + */ + @NonNull + private String clientId; + + /** + * LWA Client Secret + */ + @NonNull + private String clientSecret; + + /** + * LWA Refresh Token + */ + private String refreshToken; + + /** + * LWA Authorization Server Endpoint + */ + @NonNull + private String endpoint; + + /** + * LWA Client Scopes + */ + private LWAClientScopes scopes; + + public static class LWAAuthorizationCredentialsBuilder { + + { + scopes = new LWAClientScopes(new HashSet<>()); + } + + public LWAAuthorizationCredentialsBuilder withScope(String scope) { + return withScopes(scope); + } + + public LWAAuthorizationCredentialsBuilder withScopes(String... scopes) { + if (scopes != null) { + Arrays.stream(scopes) + .forEach(this.scopes::addScope); + } + return this; + } + + + } + +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSigner.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSigner.java new file mode 100644 index 0000000..f429983 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSigner.java @@ -0,0 +1,55 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.squareup.okhttp.Request; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +/** + * LWA Authorization Signer + */ +public class LWAAuthorizationSigner { + private static final String SIGNED_ACCESS_TOKEN_HEADER_NAME = "x-amz-access-token"; + private final String tokenRequestGrantType; + + @Getter(AccessLevel.PACKAGE) + @Setter(AccessLevel.PACKAGE) + private LWAClient lwaClient; + + private LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta; + + /** + * + * @param lwaAuthorizationCredentials LWA Authorization Credentials for token exchange + */ + public LWAAuthorizationSigner(LWAAuthorizationCredentials lwaAuthorizationCredentials) { + + lwaClient = new LWAClient(lwaAuthorizationCredentials.getEndpoint()); + + if (!lwaAuthorizationCredentials.getScopes().isEmpty()) { + tokenRequestGrantType = "client_credentials"; + } else { + tokenRequestGrantType = "refresh_token"; + } + + lwaAccessTokenRequestMeta = LWAAccessTokenRequestMeta.builder() + .clientId(lwaAuthorizationCredentials.getClientId()) + .clientSecret(lwaAuthorizationCredentials.getClientSecret()) + .refreshToken(lwaAuthorizationCredentials.getRefreshToken()) + .grantType(tokenRequestGrantType).scopes(lwaAuthorizationCredentials.getScopes()) + .build(); + } + + /** + * Signs a Request with an LWA Access Token + * @param originalRequest Request to sign (treated as immutable) + * @return Copy of originalRequest with LWA signature + */ + public Request sign(Request originalRequest) { + String accessToken = lwaClient.getAccessToken(lwaAccessTokenRequestMeta); + + return originalRequest.newBuilder() + .addHeader(SIGNED_ACCESS_TOKEN_HEADER_NAME, accessToken) + .build(); + } +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClient.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClient.java new file mode 100644 index 0000000..5068400 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClient.java @@ -0,0 +1,58 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.OkHttpClient; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.RequestBody; +import com.squareup.okhttp.Response; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +import java.io.IOException; + +class LWAClient { + private static final String ACCESS_TOKEN_KEY = "access_token"; + private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8"); + + @Getter + private String endpoint; + + @Setter(AccessLevel.PACKAGE) + private OkHttpClient okHttpClient; + + LWAClient(String endpoint) { + okHttpClient = new OkHttpClient(); + this.endpoint = endpoint; + } + + String getAccessToken(LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta) { + RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE, + new Gson().toJson(lwaAccessTokenRequestMeta)); + Request accessTokenRequest = new Request.Builder() + .url(endpoint) + .post(requestBody) + .build(); + + String accessToken; + try { + Response response = okHttpClient.newCall(accessTokenRequest).execute(); + if (!response.isSuccessful()) { + throw new IOException("Unsuccessful LWA token exchange"); + } + + JsonObject responseJson = new JsonParser() + .parse(response.body().string()) + .getAsJsonObject(); + + accessToken = responseJson.get(ACCESS_TOKEN_KEY).getAsString(); + } catch (Exception e) { + throw new RuntimeException("Error getting LWA Access Token", e); + } + + return accessToken; + } +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopes.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopes.java new file mode 100644 index 0000000..b2de370 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopes.java @@ -0,0 +1,27 @@ +package com.amazon.SellingPartnerAPIAA; + + +import com.google.gson.annotations.JsonAdapter; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Set; + +@AllArgsConstructor +@Getter +@JsonAdapter(LWAClientScopesSerializerDeserializer.class) +public class LWAClientScopes { + + private final Set scopes; + + protected void addScope(String scope) { + scopes.add(scope); + + } + + protected boolean isEmpty() { + return scopes.isEmpty(); + } + + +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializer.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializer.java new file mode 100644 index 0000000..b821658 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializer.java @@ -0,0 +1,34 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class LWAClientScopesSerializerDeserializer implements JsonDeserializer, + JsonSerializer { + + @Override + public JsonElement serialize(LWAClientScopes src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(String.join(" ", src.getScopes())); + } + + @Override + public LWAClientScopes deserialize(JsonElement jsonElement, Type type, + JsonDeserializationContext jsonDeserializationContext) + throws JsonParseException { + JsonObject jsonObj = jsonElement.getAsJsonObject(); + Set scopeSet = new HashSet<>(Arrays.asList(jsonObj.get("scope").getAsString().split(" "))); + return new LWAClientScopes(scopeSet); + + } +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/ScopeConstants.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/ScopeConstants.java new file mode 100644 index 0000000..8d71dd1 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/ScopeConstants.java @@ -0,0 +1,11 @@ +package com.amazon.SellingPartnerAPIAA; + +public final class ScopeConstants { + + private ScopeConstants(){ + } + + public static final String SCOPE_NOTIFICATIONS_API = "sellingpartnerapi::notifications"; + public static final String SCOPE_MIGRATION_API = "sellingpartnerapi::migration"; + +} diff --git a/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/SignableRequestImpl.java b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/SignableRequestImpl.java new file mode 100644 index 0000000..9e0d196 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/src/com/amazon/SellingPartnerAPIAA/SignableRequestImpl.java @@ -0,0 +1,153 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.amazonaws.ReadLimitInfo; +import com.amazonaws.SignableRequest; +import com.amazonaws.http.HttpMethodName; +import com.squareup.okhttp.HttpUrl; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.Request; +import okio.Buffer; +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class SignableRequestImpl implements SignableRequest { + private static final String CONTENT_TYPE_HEADER_NAME = "Content-Type"; + private Request originalRequest; + private Request.Builder signableRequestBuilder; + + SignableRequestImpl(Request originalRequest) { + this.originalRequest = originalRequest; + signableRequestBuilder = originalRequest.newBuilder(); + } + + @Override + public void addHeader(String name, String value) { + signableRequestBuilder.addHeader(name, value); + } + + @Override + public void addParameter(String name, String value) { + HttpUrl newUrl = signableRequestBuilder.build() + .httpUrl() + .newBuilder() + .addEncodedQueryParameter(name, value) + .build(); + + signableRequestBuilder.url(newUrl); + } + + @Override + public void setContent(InputStream inputStream) { + throw new UnsupportedOperationException(); + } + + @Override + public Map getHeaders() { + Map headers = new HashMap<>(); + + Request requestSnapshot = signableRequestBuilder.build(); + requestSnapshot.headers() + .names() + .forEach(headerName -> headers.put(headerName, requestSnapshot.header(headerName))); + + if (requestSnapshot.body() != null) { + MediaType contentType = requestSnapshot.body().contentType(); + if (contentType != null) { + headers.put(CONTENT_TYPE_HEADER_NAME, contentType.toString()); + } + } + + return headers; + } + + @Override + public String getResourcePath() { + try { + return originalRequest.url() + .toURI() + .getPath(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + + @Override + public Map> getParameters() { + Map> parameters = new HashMap<>(); + try { + List nameValuePairs = URLEncodedUtils.parse(originalRequest.url().toURI(), + StandardCharsets.UTF_8); + nameValuePairs.forEach(nameValuePair -> parameters.put(nameValuePair.getName(), + Collections.singletonList(nameValuePair.getValue()))); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + return parameters; + } + + @Override + public URI getEndpoint() { + URI uri = null; + try { + uri = originalRequest.url().toURI(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + return URI.create(String.format("%s://%s", uri.getScheme(), uri.getHost())); + } + + @Override + public HttpMethodName getHttpMethod() { + return HttpMethodName.fromValue(originalRequest.method().toUpperCase()); + } + + @Override + public int getTimeOffset() { + return 0; + } + + @Override + public InputStream getContent() { + ByteArrayInputStream inputStream = null; + + if (originalRequest.body() != null) { + try { + Buffer buffer = new Buffer(); + originalRequest.body().writeTo(buffer); + inputStream = new ByteArrayInputStream(buffer.readByteArray()); + } catch (IOException e) { + throw new RuntimeException("Unable to buffer request body", e); + } + } + + return inputStream; + } + + @Override + public InputStream getContentUnwrapped() { + return getContent(); + } + + @Override + public ReadLimitInfo getReadLimitInfo() { + return null; + } + + @Override + public Object getOriginalRequestObject() { + return signableRequestBuilder.build(); + } +} diff --git a/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/AWSSigV4SignerTest.java b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/AWSSigV4SignerTest.java new file mode 100644 index 0000000..3cd9c6a --- /dev/null +++ b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/AWSSigV4SignerTest.java @@ -0,0 +1,83 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.amazonaws.SignableRequest; +import com.amazonaws.auth.AWS4Signer; +import com.amazonaws.auth.AWSCredentials; +import com.squareup.okhttp.Request; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class AWSSigV4SignerTest { + private static final String TEST_ACCESS_KEY_ID = "aKey"; + private static final String TEST_SECRET_KEY = "sKey"; + private static final String TEST_REGION = "us-east"; + + @Mock + private AWS4Signer mockAWS4Signer; + + private AWSSigV4Signer underTest; + + @Before + public void init() { + underTest = new AWSSigV4Signer(AWSAuthenticationCredentials.builder() + .accessKeyId(TEST_ACCESS_KEY_ID) + .secretKey(TEST_SECRET_KEY) + .region(TEST_REGION) + .build() + ); + } + + @Test + public void signRequestUsingProvidedCredentials() { + ArgumentCaptor awsCredentialsArgumentCaptor = ArgumentCaptor.forClass(AWSCredentials.class); + underTest.setAws4Signer(mockAWS4Signer); + + doNothing() + .when(mockAWS4Signer) + .sign(any(SignableRequest.class), awsCredentialsArgumentCaptor.capture()); + + underTest.sign(new Request.Builder().url("https://api.amazon.com").build()); + + AWSCredentials actualAWSCredentials = awsCredentialsArgumentCaptor.getValue(); + + assertEquals(TEST_ACCESS_KEY_ID, actualAWSCredentials.getAWSAccessKeyId()); + assertEquals(TEST_SECRET_KEY, actualAWSCredentials.getAWSSecretKey()); + } + + @Test + public void setSignerRegion() { + assertEquals(TEST_REGION, underTest.getAws4Signer().getRegionName()); + } + + @Test + public void setSignerServiceName() { + assertEquals("execute-api", underTest.getAws4Signer().getServiceName()); + } + + @Test + public void returnSignedRequest() { + ArgumentCaptor signableRequestArgumentCaptor = ArgumentCaptor.forClass(SignableRequest.class); + underTest.setAws4Signer(mockAWS4Signer); + + Request actualSignedRequest = underTest.sign(new Request.Builder() + .url("http://api.amazon.com") + .build()); + + verify(mockAWS4Signer) + .sign(signableRequestArgumentCaptor.capture(), any(AWSCredentials.class)); + + SignableRequest actualSignableRequest = signableRequestArgumentCaptor.getValue(); + + assertEquals(((Request)actualSignableRequest.getOriginalRequestObject()).url(), actualSignedRequest.url()); + } +} diff --git a/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSignerTest.java b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSignerTest.java new file mode 100644 index 0000000..10b5982 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAAuthorizationSignerTest.java @@ -0,0 +1,131 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.squareup.okhttp.Request; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.ArgumentCaptor; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.stream.Stream; + +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_MIGRATION_API; +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class LWAAuthorizationSignerTest { + private static final String TEST_REFRESH_TOKEN = "rToken"; + private static final String TEST_CLIENT_SECRET = "cSecret"; + private static final String TEST_CLIENT_ID = "cId"; + private static final String TEST_ENDPOINT = "https://www.amazon.com/lwa"; + private static final String TEST_SCOPE_1 = SCOPE_NOTIFICATIONS_API; + private static final String TEST_SCOPE_2 = SCOPE_MIGRATION_API; + private static final String SELLER_TYPE_SELLER = "seller"; + private static final String SELLER_TYPE_SELLERLESS = "sellerless"; + + + private Request request; + private static LWAAuthorizationSigner underTestSeller; + private static LWAAuthorizationSigner underTestSellerless; + + static { + + underTestSeller = new LWAAuthorizationSigner(LWAAuthorizationCredentials.builder() + .clientId(TEST_CLIENT_ID) + .clientSecret(TEST_CLIENT_SECRET) + .refreshToken(TEST_REFRESH_TOKEN) + .endpoint(TEST_ENDPOINT) + .build()); + + underTestSellerless = new LWAAuthorizationSigner(LWAAuthorizationCredentials.builder() + .clientId(TEST_CLIENT_ID) + .clientSecret(TEST_CLIENT_SECRET) + .withScopes(TEST_SCOPE_1, TEST_SCOPE_2) + .endpoint(TEST_ENDPOINT) + .build()); + } + + @BeforeEach + public void init() { + request = new Request.Builder() + .url("https://www.amazon.com/api") + .build(); + + } + + public static Stream lwaAuthSigner(){ + + return Stream.of( + Arguments.of(SELLER_TYPE_SELLER, underTestSeller), + Arguments.of(SELLER_TYPE_SELLERLESS, underTestSellerless) + ); + } + + @ParameterizedTest + @MethodSource("lwaAuthSigner") + public void initializeLWAClientWithConfiguredEndpoint(String sellerType, LWAAuthorizationSigner testAuthSigner) { + assertEquals(TEST_ENDPOINT, testAuthSigner.getLwaClient().getEndpoint()); + } + + @ParameterizedTest + @MethodSource("lwaAuthSigner") + public void requestLWAAccessTokenFromConfiguration(String sellerType, LWAAuthorizationSigner testAuthSigner) { + LWAClient mockLWAClient = mock(LWAClient.class); + ArgumentCaptor lwaAccessTokenRequestMetaArgumentCaptor = ArgumentCaptor.forClass( + LWAAccessTokenRequestMeta.class); + + when(mockLWAClient.getAccessToken(lwaAccessTokenRequestMetaArgumentCaptor.capture())) + .thenReturn("foo"); + + testAuthSigner.setLwaClient(mockLWAClient); + testAuthSigner.sign(request); + + LWAAccessTokenRequestMeta actualLWAAccessTokenRequestMeta = lwaAccessTokenRequestMetaArgumentCaptor.getValue(); + assertEquals(TEST_REFRESH_TOKEN, actualLWAAccessTokenRequestMeta.getRefreshToken()); + assertEquals(TEST_CLIENT_SECRET, actualLWAAccessTokenRequestMeta.getClientSecret()); + assertEquals(TEST_CLIENT_ID, actualLWAAccessTokenRequestMeta.getClientId()); + + if(sellerType.equals(SELLER_TYPE_SELLER)){ + Assert.assertTrue(actualLWAAccessTokenRequestMeta.getScopes().getScopes().isEmpty()); + assertEquals("refresh_token", actualLWAAccessTokenRequestMeta.getGrantType()); + } + else if (sellerType.equals(SELLER_TYPE_SELLERLESS)){ + assertEquals(new HashSet(Arrays.asList(TEST_SCOPE_1, TEST_SCOPE_2)), actualLWAAccessTokenRequestMeta.getScopes().getScopes()); + assertEquals("client_credentials", actualLWAAccessTokenRequestMeta.getGrantType()); + } + + } + + @ParameterizedTest + @MethodSource("lwaAuthSigner") + public void returnSignedRequestWithAccessTokenFromLWAClient(String sellerType, LWAAuthorizationSigner testAuthSigner) { + LWAClient mockLWAClient = mock(LWAClient.class); + + when(mockLWAClient.getAccessToken(any(LWAAccessTokenRequestMeta.class))) + .thenReturn("Azta|Foo"); + + testAuthSigner.setLwaClient(mockLWAClient); + Request actualSignedRequest = testAuthSigner.sign(request); + + assertEquals("Azta|Foo", actualSignedRequest.header("x-amz-access-token")); + } + + @ParameterizedTest + @MethodSource("lwaAuthSigner") + public void originalRequestIsImmutable(String sellerType, LWAAuthorizationSigner testAuthSigner) { + LWAClient mockLWAClient = mock(LWAClient.class); + + when(mockLWAClient.getAccessToken(any(LWAAccessTokenRequestMeta.class))) + .thenReturn("Azta|Foo"); + + testAuthSigner.setLwaClient(mockLWAClient); + assertNotSame(request, testAuthSigner.sign(request)); + } +} diff --git a/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializerTest.java b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializerTest.java new file mode 100644 index 0000000..089f72e --- /dev/null +++ b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientScopesSerializerDeserializerTest.java @@ -0,0 +1,78 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.google.gson.Gson; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Stream; + +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_MIGRATION_API; +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API; + +public class LWAClientScopesSerializerDeserializerTest { + private static final String TEST_SCOPE_1 = SCOPE_NOTIFICATIONS_API; + private static final String TEST_SCOPE_2 = SCOPE_MIGRATION_API; + + private static final Set scopesTestSellerless = new HashSet(Arrays.asList(TEST_SCOPE_1, + TEST_SCOPE_2)); + + private static final String SELLER_TYPE_SELLER = "seller"; + private static final String SELLER_TYPE_SELLERLESS = "sellerless"; + + private Gson gson; + + @BeforeEach + public void setup() { + gson = new Gson(); + } + + public static Stream scopeSerialization(){ + + return Stream.of( + Arguments.of(SELLER_TYPE_SELLER, null), + Arguments.of(SELLER_TYPE_SELLERLESS, new LWAClientScopes(scopesTestSellerless)) + ); + } + + public static Stream scopeDeserialization(){ + + return Stream.of( + Arguments.of(SELLER_TYPE_SELLER, null), + Arguments.of(SELLER_TYPE_SELLERLESS, "{\"scope\":\"sellingpartnerapi::migration sellingpartnerapi::notifications\"}") + ); + } + + @ParameterizedTest + @MethodSource("scopeSerialization") + public void testSerializeScope(String sellerType, LWAClientScopes testScope){ + + String scopeJSON = gson.toJson(testScope); + + if (sellerType.equals(SELLER_TYPE_SELLER)) { + Assert.assertEquals("null", scopeJSON); + } + else if (sellerType.equals(SELLER_TYPE_SELLERLESS)){ + Assert.assertTrue(!scopeJSON.isEmpty()); + } + } + + @ParameterizedTest + @MethodSource("scopeDeserialization") + public void testDeserializeScope(String sellerType, String serializedValue){ + + LWAClientScopes deserializedValue = gson.fromJson(serializedValue, LWAClientScopes.class); + if (sellerType.equals(SELLER_TYPE_SELLER)) { + Assert.assertNull(deserializedValue); + } + else if (sellerType.equals(SELLER_TYPE_SELLERLESS)){ + Assert.assertEquals(deserializedValue.getScopes(),scopesTestSellerless); + } + } + +} diff --git a/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientTest.java b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientTest.java new file mode 100644 index 0000000..4606f14 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/LWAClientTest.java @@ -0,0 +1,185 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.squareup.okhttp.Call; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.OkHttpClient; +import com.squareup.okhttp.Protocol; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; +import com.squareup.okhttp.ResponseBody; +import okio.Buffer; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Stream; + +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_MIGRATION_API; +import static com.amazon.SellingPartnerAPIAA.ScopeConstants.SCOPE_NOTIFICATIONS_API; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class LWAClientTest { + private static final String TEST_ENDPOINT = "https://www.amazon.com/api"; + private static final MediaType EXPECTED_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8"); + private static LWAAccessTokenRequestMeta lwaAccessTokenRequestMetaSeller; + private static LWAAccessTokenRequestMeta lwaAccessTokenRequestMetaSellerless; + + private static final String TEST_SCOPE_1 = SCOPE_NOTIFICATIONS_API; + private static final String TEST_SCOPE_2 = SCOPE_MIGRATION_API; + + private static final Set scopesTestSellerless = new HashSet(Arrays.asList(TEST_SCOPE_1, + TEST_SCOPE_2)); + + private static final String SELLER_TYPE_SELLER = "seller"; + private static final String SELLER_TYPE_SELLERLESS = "sellerless"; + + @Mock + private OkHttpClient mockOkHttpClient; + + @Mock + private Call mockCall; + + private LWAClient underTest; + + static { + lwaAccessTokenRequestMetaSeller = LWAAccessTokenRequestMeta.builder() + .refreshToken("rToken") + .clientId("cId") + .clientSecret("cSecret") + .grantType("rToken") + .build(); + + lwaAccessTokenRequestMetaSellerless = LWAAccessTokenRequestMeta.builder() + .clientId("cId") + .clientSecret("cSecret") + .grantType("cCredentials") + .scopes(new LWAClientScopes(scopesTestSellerless)) + .build(); + } + + @Before + @BeforeEach + public void init() { + MockitoAnnotations.initMocks(this); + + underTest = new LWAClient(TEST_ENDPOINT); + underTest.setOkHttpClient(mockOkHttpClient); + + } + + public static Stream lwaClient(){ + + return Stream.of( + Arguments.of(SELLER_TYPE_SELLER, lwaAccessTokenRequestMetaSeller), + Arguments.of(SELLER_TYPE_SELLERLESS, lwaAccessTokenRequestMetaSellerless) + ); + } + + @Test + public void initializeEndpoint() { + assertEquals(TEST_ENDPOINT, underTest.getEndpoint()); + } + + @ParameterizedTest + @MethodSource("lwaClient") + public void makeRequestFromMeta (String sellerType, LWAAccessTokenRequestMeta testLwaAccessTokenRequestMeta) throws IOException { + ArgumentCaptor requestArgumentCaptor = ArgumentCaptor.forClass(Request.class); + when(mockOkHttpClient.newCall(requestArgumentCaptor.capture())) + .thenReturn(mockCall); + when(mockCall.execute()) + .thenReturn(buildResponse(200, "foo")); + + underTest.getAccessToken(testLwaAccessTokenRequestMeta); + + Request actualRequest = requestArgumentCaptor.getValue(); + assertEquals(TEST_ENDPOINT, actualRequest.url().toString()); + assertEquals("POST", actualRequest.method().toUpperCase()); + + Buffer bodyBuffer = new Buffer(); + actualRequest.body().writeTo(bodyBuffer); + JsonObject bodyJson = new JsonParser() + .parse(new InputStreamReader(bodyBuffer.inputStream())) + .getAsJsonObject(); + + if (sellerType == SELLER_TYPE_SELLER) { + assertEquals("rToken", bodyJson.get("refresh_token") + .getAsString()); + } + else if (sellerType == SELLER_TYPE_SELLERLESS){ + assertNotNull(bodyJson.get("scope")); + } + assertEquals("cId", bodyJson.get("client_id").getAsString()); + assertEquals("cSecret", bodyJson.get("client_secret").getAsString()); + assertEquals(EXPECTED_MEDIA_TYPE, actualRequest.body().contentType()); + } + + @ParameterizedTest + @MethodSource("lwaClient") + public void returnAccessTokenFromResponse(String sellerType, LWAAccessTokenRequestMeta testLwaAccessTokenRequestMeta) throws IOException { + when(mockOkHttpClient.newCall(any(Request.class))) + .thenReturn(mockCall); + when(mockCall.execute()) + .thenReturn(buildResponse(200, "Azta|foo")); + + assertEquals("Azta|foo", underTest.getAccessToken(testLwaAccessTokenRequestMeta)); + } + + @ParameterizedTest + @MethodSource("lwaClient") + public void unsuccessfulPostThrowsException(String sellerType, LWAAccessTokenRequestMeta testLwaAccessTokenRequestMeta) throws IOException { + when(mockOkHttpClient.newCall(any(Request.class))) + .thenReturn(mockCall); + when(mockCall.execute()) + .thenReturn(buildResponse(400, "Azta|foo")); + + Assertions.assertThrows(RuntimeException.class, () -> { + underTest.getAccessToken(testLwaAccessTokenRequestMeta); + }); + } + + @ParameterizedTest + @MethodSource("lwaClient") + public void missingAccessTokenInResponseThrowsException(String sellerType, LWAAccessTokenRequestMeta testLwaAccessTokenRequestMeta) throws IOException { + when(mockOkHttpClient.newCall(any(Request.class))) + .thenReturn(mockCall); + when(mockCall.execute()) + .thenReturn(buildResponse(200, "")); + + Assertions.assertThrows(RuntimeException.class, () -> { + underTest.getAccessToken(testLwaAccessTokenRequestMeta); + }); + } + + private static Response buildResponse(int code, String accessToken) { + ResponseBody responseBody = ResponseBody.create(EXPECTED_MEDIA_TYPE, + String.format("{%s:%s}", "access_token", accessToken)); + + return new Response.Builder() + .request(new Request.Builder().url(TEST_ENDPOINT).build()) + .code(code) + .body(responseBody) + .protocol(Protocol.HTTP_1_1) + .message("OK") + .build(); + } +} diff --git a/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/SignableRequestImplTest.java b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/SignableRequestImplTest.java new file mode 100644 index 0000000..f370944 --- /dev/null +++ b/clients/sellingpartner-api-aa-java/tst/com/amazon/SellingPartnerAPIAA/SignableRequestImplTest.java @@ -0,0 +1,236 @@ +package com.amazon.SellingPartnerAPIAA; + +import com.amazonaws.http.HttpMethodName; +import com.squareup.okhttp.HttpUrl; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.RequestBody; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.net.URI; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class SignableRequestImplTest { + private Request testRequest; + private SignableRequestImpl underTest; + + @Before + public void init() { + testRequest = new Request.Builder() + .url("http://www.amazon.com/request/library?test=true&sky=blue&right=右") + .get() + .build(); + + underTest = new SignableRequestImpl(testRequest); + } + + @Test + public void getHttpMethod() { + assertEquals(HttpMethodName.GET, underTest.getHttpMethod()); + + underTest = new SignableRequestImpl(new Request.Builder() + .url("https://www.amazon.com") + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"foo\": \"bar\"}")) + .build()); + + assertEquals(HttpMethodName.POST, underTest.getHttpMethod()); + } + + @Test + public void getOriginalRequestObject() { + Request actualRequest = (Request)underTest.getOriginalRequestObject(); + + assertNotSame(testRequest, actualRequest); + assertEquals(testRequest.method(), actualRequest.method()); + assertEquals(testRequest.url(), actualRequest.url()); + assertEquals(testRequest.headers().toMultimap(), actualRequest.headers().toMultimap()); + assertEquals(testRequest.body(), actualRequest.body()); + } + + @Test + public void getReadLimitInfo() { + assertNull(underTest.getReadLimitInfo()); + } + + @Test + public void getResourcePath() { + assertEquals("/request/library", underTest.getResourcePath()); + } + + @Test + public void noTimeOffset() { + assertEquals(0, underTest.getTimeOffset()); + } + + @Test + public void getEndpoint() { + assertEquals(URI.create("http://www.amazon.com"), underTest.getEndpoint()); + } + + @Test + public void headers() { + Map expectedHeaders = new HashMap<>(); + + assertTrue(underTest.getHeaders().isEmpty()); + + underTest.addHeader("foo", "bar"); + expectedHeaders.put("foo", "bar"); + assertEquals(expectedHeaders, underTest.getHeaders()); + + underTest.addHeader("ban", "bop"); + expectedHeaders.put("ban", "bop"); + assertEquals(expectedHeaders, underTest.getHeaders()); + } + + @Test + public void getParameters() { + Map> expectedParamters = new HashMap<>(); + expectedParamters.put("test", Collections.singletonList("true")); + expectedParamters.put("sky", Collections.singletonList("blue")); + expectedParamters.put("right", Collections.singletonList("右")); + + assertEquals(expectedParamters, underTest.getParameters()); + } + + @Test + public void getContent() { + String expectedContent = "{\"foo\":\"bar\"}"; + StringBuilder actualContent = new StringBuilder(); + + underTest = new SignableRequestImpl(new Request.Builder() + .url("https://www.amazon.com") + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), expectedContent)) + .build()); + + try(Scanner scanner = new Scanner(underTest.getContent())){ + while(scanner.hasNext()) { + actualContent.append(scanner.next()); + } + } + + assertEquals(expectedContent, actualContent.toString()); + } + + @Test + public void getUnwrappedContent() { + String expectedContent = "{\"ban\":\"bop\"}"; + StringBuilder actualContent = new StringBuilder(); + + underTest = new SignableRequestImpl(new Request.Builder() + .url("https://www.amazon.com") + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), expectedContent)) + .build()); + + try(Scanner scanner = new Scanner(underTest.getContentUnwrapped())){ + while(scanner.hasNext()) { + actualContent.append(scanner.next()); + } + } + + assertEquals(expectedContent, actualContent.toString()); + } + + @Test(expected = UnsupportedOperationException.class) + public void setContentNotSupported() { + underTest.setContent(new ByteArrayInputStream("abc".getBytes())); + } + + @Test + public void addParameter() { + underTest.addParameter("left", "左"); + + HttpUrl actualHttpUrl = ((Request) underTest.getOriginalRequestObject()) + .httpUrl(); + + assertEquals(Collections.singletonList("true"), actualHttpUrl.queryParameterValues("test")); + assertEquals(Collections.singletonList("blue"), actualHttpUrl.queryParameterValues("sky")); + assertEquals(Collections.singletonList("右"), actualHttpUrl.queryParameterValues("right")); + assertEquals(Collections.singletonList("左"), actualHttpUrl.queryParameterValues("left")); + } + + @Test + public void gracefulBlankParametersParse() { + testRequest = new Request.Builder() + .url("http://www.amazon.com/request/library? ") + .get() + .build(); + + underTest = new SignableRequestImpl(testRequest); + + assertTrue(underTest.getParameters().isEmpty()); + } + + @Test + public void gracefulIncompleteParameterPairsParse() { + testRequest = new Request.Builder() + .url("http://www.amazon.com/request/library?isSigned& =false") + .get() + .build(); + + Map> expected = new HashMap<>(); + expected.put("isSigned", Collections.singletonList(null)); + expected.put(" ", Collections.singletonList("false")); + + underTest = new SignableRequestImpl(testRequest); + + assertEquals(expected, underTest.getParameters()); + } + + @Test + public void getHeadersIncludesContentTypeFromRequestBody() { + String expected = "application/json; charset=utf-8"; + RequestBody requestBody = RequestBody.create(MediaType.parse(expected), + "{\"foo\":\"bar\"}"); + + testRequest = new Request.Builder() + .url("http://www.amazon.com") + .post(requestBody) + .header("Content-Type", "THIS SHOULD BE OVERRIDDEN WITH REQUEST BODY CONTENT TYPE") + .build(); + + underTest = new SignableRequestImpl(testRequest); + + assertEquals(expected, underTest.getHeaders().get("Content-Type")); + } + + @Test + public void missingRequestBodyDoesNotOverwriteExistingContentTypeHeader() { + String expected = "testContentType"; + + testRequest = new Request.Builder() + .url("http://www.amazon.com") + .get() + .header("Content-Type", expected) + .build(); + + underTest = new SignableRequestImpl(testRequest); + + assertEquals(expected, underTest.getHeaders().get("Content-Type")); + } + + @Test + public void missingRequestBodyContentTypeDoesNotOverwriteExistingContentTypeHeader() { + String expected = "testContentType"; + + testRequest = new Request.Builder() + .url("http://www.amazon.com") + .post(RequestBody.create(null, "foo")) + .header("Content-Type", expected) + .build(); + + underTest = new SignableRequestImpl(testRequest); + + assertEquals(expected, underTest.getHeaders().get("Content-Type")); + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/.gitignore b/clients/sellingpartner-api-frp-helper-java/.gitignore new file mode 100644 index 0000000..3a576f3 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/.gitignore @@ -0,0 +1,11 @@ +.gradle/ +.idea/ +/build/ +/buildSrc +/gradle/ +/gradlew +/gradlew.bat +/wrapper/ +/target +build.gradle.backup +settings.gradle.backup \ No newline at end of file diff --git a/clients/sellingpartner-api-frp-helper-java/README.md b/clients/sellingpartner-api-frp-helper-java/README.md new file mode 100644 index 0000000..0a1e9fe --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/README.md @@ -0,0 +1,116 @@ +#Selling Partner API for Feeds, Uploads and Reports Java Helper + +This library provides helper classes for use with the Selling Partner APIs for Feeds, Uploads and Reports. It is intended for use with the Selling Partner API Client Libraries generated by [Swagger Codegen](https://swagger.io/tools/swagger-codegen/) using the OkHttp library. + +Use this library to encrypt files to be uploaded (Feeds and Uploads), or decrypt files after downloading (Reports) + +## Resources + +To use this library you will need to integrate it with one or more of the following packages: + +##### Selling Partner API Authentication/Authorization Library + +This package is required because it handles the Selling Partner API credentials required by the APIs. You can find it on our GitHub Repository [here](https://github.com/amzn/amazon-marketplace-api-sdk/tree/master/clients/sellingpartner-api-aa-java). + +##### Selling Partner API for Feeds + +The JSON file can be obtained [here](https://github.com/amzn/amazon-marketplace-api-sdk/tree/master/Feeds%20API). + +##### Selling Partner API for Uploads + +The JSON file can be obtained [here](https://github.com/amzn/amazon-marketplace-api-sdk/tree/master/Uploads%20API). + +##### Selling Partner API for Reports + +The JSON file can be obtained [here](https://github.com/amzn/amazon-marketplace-api-sdk/tree/master/Reports%20API). + +## Building + +#### Building the Client Libraries + +- Build the client library for each of the API model JSON files using Swagger Codegen. For each file: + +*For example:* + + java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\Feeds.json -l java -t [path to clients\sellingpartner-apiaa-java directory]/resources/swagger-codegen/templates/ -o C:\SwaggerToCL\Feeds_JavaCL + +#### Integrating the AA Library to the Helper via Maven + +1. Build the AA Library and add it as a dependency of the SDKs: + + Navigate to the **clients/sellingpartner-api-aa-java** folder and run: + + mvn package + + This generates a folder named "target". In this folder is a JAR file named **sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar** (or something similar) and all of the required dependencies. + +2. Install the JAR file in your local Maven repository: + + *For example:* + + mvn install:install-file -Dfile=[path to JAR file in "target" folder] -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aajava -Dversion=1.0 -Dpackaging=jar + + Keep track of the *groupId*, *artifactId*, and *version* values (which can also be found near the top of the **pom.xml** file in the **clients/sellingpartner-api-aa-java** folder) because you will use them in the steps to follow. + +3. Add a dependency on the AA library in the **pom.xml** of each client library. + + *For example:* + + ``` + + com.amazon.sellingpartnerapi + sellingpartnerapi-aa-java + 1.0 + + ``` +4. Build each client library with: + + ``` + mvn package + ``` + +5. Install the Feeds, Uploads and Reports client libraries in your local Maven repository. + + *For example:* + + mvn install:install-file -Dfile=[path to JAR file in "target" folder] -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-feeds-java -Dversion=1.0 -Dpackaging=jar + +6. Confirm that the *artifactId* of the Feeds, Uploads and Reports libraries in the **pom.xml** of the Helper matches the ones used when installing each library. + + *For example:* + + ``` + + com.amazon.sellingpartnerapi + sellingpartnerapi-feeds-java + 1.0 + + ``` + +7. Navigate to the **SellingPartnerApiFeedsUploadsReportsJavaHelper** folder and run: + + ``` + mvn package + ``` + *The Feeds, Uploads and Reports Java Helper SDK comes with two building options: Maven and Gradle, however, we only detail how to complete the Maven build above.* + +## License +Swagger Codegen templates are subject to the [Swagger Codegen License](https://github.com/swagger-api/swagger-codegen#license). + +All other work licensed as follows: + +Copyright Amazon.com Inc. or its affiliates. + +All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this library except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/clients/sellingpartner-api-frp-helper-java/build.gradle b/clients/sellingpartner-api-frp-helper-java/build.gradle new file mode 100644 index 0000000..b38dabc --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/build.gradle @@ -0,0 +1,107 @@ + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.3.+' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' + } +} + +plugins { + id 'idea' + id 'eclipse' + id 'java' + id 'maven' +} + +group = 'io.swagger' +version = '1.0.0' + + +repositories { + jcenter() +} + +sourceCompatibility = JavaVersion.VERSION_1_8 +targetCompatibility = JavaVersion.VERSION_1_8 + + + +/*sourceSets { + main { + java { + srcDirs = ['src/main/java'] + } + resources { + srcDirs = ['src/main/resources'] + } + } + + test { + java { + srcDirs = ['src/test/java'] + } + resources { + srcDirs = ['src/test/resources'] + } + } +}*/ + + +install { + repositories.mavenInstaller { + pom.artifactId = 'feeds-uploads-reports-java-helper' + } +} + +task execute(type: JavaExec) { + main = System.getProperty('mainClass') + classpath = sourceSets.main.runtimeClasspath +} + +test { + jvmArgs('-Djavax.net.ssl.trustStore=resources/main/certs/InternalAndExternalTrustStore.jks', + '-Djavax.net.ssl.trustStorePassword=amazon') +} + +test.dependsOn processTestResources + +// TO get this to work, +// You must add the sellingpartner-api-aa-java.jar (with dependencies) to your +// /libs/ folder!!!!! +dependencies { + compileOnly 'org.projectlombok:lombok:1.18.8' + annotationProcessor 'org.projectlombok:lombok:1.18.8' + implementation 'io.swagger:swagger-annotations:1.5.17' + implementation 'com.squareup.okhttp:okhttp:2.7.5' + implementation 'com.squareup.okhttp:logging-interceptor:2.7.5' + implementation 'com.google.code.gson:gson:2.8.1' + implementation 'io.gsonfire:gson-fire:1.8.0' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.2' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.2' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.10.2' + implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12' + implementation group: 'org.apache.directory.studio', name: 'org.apache.commons.io', version: '2.4' + implementation group: 'com.google.guava', name: 'guava', version: '28.2-jre' + implementation group: 'joda-time', name: 'joda-time', version: '2.10.5' + + // https://mvnrepository.com/artifact/org.threeten/threetenbp + implementation group: 'org.threeten', name: 'threetenbp', version: '1.3.5' + // https://mvnrepository.com/artifact/junit/junit + testCompile group: 'junit', name: 'junit', version: '4.12' + + implementation 'com.amazonaws:aws-java-sdk-signer:1.11.610' + implementation 'com.google.code.gson:gson:2.8.5' + implementation 'com.squareup.okhttp:okhttp:2.7.5' + implementation 'org.junit.jupiter:junit-jupiter-engine:5.0.0' + implementation 'org.junit.jupiter:junit-jupiter-params:5.3.2' + implementation 'org.junit.jupiter:junit-jupiter-migrationsupport:5.5.1' + implementation 'org.mockito:mockito-core:3.0.0' + implementation 'org.apache.commons:commons-lang3:3.9' + implementation 'org.apache.httpcomponents:httpclient:4.5.9' + + implementation fileTree(dir: 'libs', include: '*.jar') + +} diff --git a/clients/sellingpartner-api-frp-helper-java/libs/sellingpartner-api-aa-java-1.0.jar b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartner-api-aa-java-1.0.jar new file mode 100644 index 0000000..31fd121 Binary files /dev/null and b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartner-api-aa-java-1.0.jar differ diff --git a/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-feeds-java-1.0.jar b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-feeds-java-1.0.jar new file mode 100644 index 0000000..36eee17 Binary files /dev/null and b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-feeds-java-1.0.jar differ diff --git a/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-reports-java-1.0.jar b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-reports-java-1.0.jar new file mode 100644 index 0000000..b24866c Binary files /dev/null and b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-reports-java-1.0.jar differ diff --git a/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-uploads-java-1.0.jar b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-uploads-java-1.0.jar new file mode 100644 index 0000000..965d38a Binary files /dev/null and b/clients/sellingpartner-api-frp-helper-java/libs/sellingpartnerapi-uploads-java-1.0.jar differ diff --git a/clients/sellingpartner-api-frp-helper-java/pom.xml b/clients/sellingpartner-api-frp-helper-java/pom.xml new file mode 100644 index 0000000..6115587 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/pom.xml @@ -0,0 +1,164 @@ + + + 4.0.0 + io.swagger + feeds-uploads-reports-java-helper + 1.0.0 + + + org.projectlombok + lombok + 1.18.12 + provided + + + io.swagger + swagger-annotations + 1.5.17 + + + com.squareup.okhttp + okhttp + 2.7.5 + + + com.squareup.okhttp + logging-interceptor + 2.7.5 + + + io.gsonfire + gson-fire + 1.8.0 + + + com.fasterxml.jackson.core + jackson-core + 2.10.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.10.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.10.2 + + + org.apache.httpcomponents + httpclient + 4.5.12 + + + org.apache.directory.studio + org.apache.commons.io + 2.4 + + + com.google.guava + guava + 28.2-jre + + + joda-time + joda-time + 2.10.5 + + + org.threeten + threetenbp + 1.3.5 + + + com.amazonaws + aws-java-sdk-signer + 1.11.610 + + + com.google.code.gson + gson + 2.8.5 + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0 + + + org.junit.jupiter + junit-jupiter-params + 5.3.2 + + + org.junit.jupiter + junit-jupiter-migrationsupport + 5.5.1 + + + org.mockito + mockito-core + 3.0.0 + + + org.apache.commons + commons-lang3 + 3.9 + + + junit + junit + 4.12 + test + + + com.amazon.sellingpartnerapi + sellingpartnerapi-aa-java + 1.0 + + + com.amazon.sellingpartnerapi + sellingpartnerapi-feeds-java + 1.0 + + + com.amazon.sellingpartnerapi + sellingpartnerapi-reports-java + 1.0 + + + com.amazon.sellingpartnerapi + sellingpartnerapi-uploads-java + 1.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + + + org.projectlombok + lombok + 1.18.12 + + + + + + + + + 1.8 + ${java.version} + ${java.version} + 1.0.0 + UTF-8 + + diff --git a/clients/sellingpartner-api-frp-helper-java/settings.gradle b/clients/sellingpartner-api-frp-helper-java/settings.gradle new file mode 100644 index 0000000..5b80a16 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/settings.gradle @@ -0,0 +1 @@ +rootProject.name = "feeds-uploads-reports-java-helper" \ No newline at end of file diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DocumentValidatorInputStream.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DocumentValidatorInputStream.java new file mode 100644 index 0000000..ba6caeb --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DocumentValidatorInputStream.java @@ -0,0 +1,64 @@ +package com.amazon.spapi.sequencing.client; + +import com.google.common.base.Preconditions; +import lombok.extern.apachecommons.CommonsLog; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +/** + * An input stream that validates the integrity of the document when you close the stream. + */ +@CommonsLog +public class DocumentValidatorInputStream extends FilterInputStream { + + private final byte[] expectedSha256sum; + private final MessageDigest sha256digest; + + /** + * Creates an InputStream that will calculate the sha256digest and validate it upon closing the stream. + * @param inputStream + * the inputStream to calculate the sha256sum of. + * @param expectedSha256sum + * the expected sha256sum of the document being streamed if one exists. Leave null if document does not + * have a sha256sum to validate against. + * @throws NoSuchAlgorithmException + * when unable to create a SHA-256 instance of MessageDigest + */ + public DocumentValidatorInputStream(InputStream inputStream, byte[] expectedSha256sum) + throws NoSuchAlgorithmException { + super(null); + Preconditions.checkArgument(inputStream != null, "inputStream must not be null"); + Preconditions.checkArgument(expectedSha256sum != null, "expectedSha256sum must not be null"); + + this.expectedSha256sum = Arrays.copyOf(expectedSha256sum, expectedSha256sum.length); + sha256digest = MessageDigest.getInstance("SHA-256"); + this.in = new DigestInputStream(inputStream, sha256digest); + } + + /** + * Validates that the streamed document was not tampered with. + * @throws IOException + * if the streamed document was completely read and is not valid or an IOException occurs while closing + * the stream. + */ + @Override + public void close() throws IOException { + try { + if (!MessageDigest.isEqual(expectedSha256sum, sha256digest.digest()) && super.read() == -1) { + throw new ValidationFailureException("The streamed document was not valid."); + } + } finally { + try { + super.close(); + } catch (IOException e) { + log.warn(String.format("Exception while closing the validation string: %s", e.getMessage()), e); + } + } + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DownloadsSequencer.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DownloadsSequencer.java new file mode 100644 index 0000000..84967ca --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/DownloadsSequencer.java @@ -0,0 +1,23 @@ +package com.amazon.spapi.sequencing.client; + +import com.amazon.spapi.sequencing.crypto.EncryptionException; +import io.swagger.client.model.EncryptionDetails; +import io.swagger.client.model.UploadDestination; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +public interface DownloadsSequencer { + + InputStream downloadAndDecryptReportContent(UploadDestination destination, boolean isGzipped) throws IOException; + + InputStream decryptReportContent(File reportFile, EncryptionDetails details, boolean isGzipped) + throws IOException, EncryptionException; + + void downloadDecryptThenWriteFile(UploadDestination destination, boolean isGzipped, File fileToWriteTo) + throws IOException; + + void decryptThenWriteFile(File reportFile, EncryptionDetails details, boolean isGzipped, File fileToWriteTo) + throws IOException, EncryptionException; +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/SpoolingLimitExceededException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/SpoolingLimitExceededException.java new file mode 100644 index 0000000..f3522fc --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/SpoolingLimitExceededException.java @@ -0,0 +1,19 @@ +package com.amazon.spapi.sequencing.client; + +import java.io.IOException; + +/** + * Exception thrown when spooling size limits exceeded. + */ +public class SpoolingLimitExceededException extends IOException { + + /** + * Constructor that takes in a message. + * @param message + * the message describing the exception + */ + public SpoolingLimitExceededException(String message) { + super(message); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadDetails.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadDetails.java new file mode 100644 index 0000000..2fa10cb --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadDetails.java @@ -0,0 +1,19 @@ +package com.amazon.spapi.sequencing.client; + + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; +import lombok.experimental.FieldDefaults; + +@Builder +@Getter +@ToString +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +@EqualsAndHashCode +public class UploadDetails { + String sha256Sum; + String uploadDestinationId; +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadsSequencer.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadsSequencer.java new file mode 100644 index 0000000..8ed6b52 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/UploadsSequencer.java @@ -0,0 +1,46 @@ +package com.amazon.spapi.sequencing.client; + +import io.swagger.client.ApiException; +import io.swagger.client.model.UploadDestination; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; + +/** + * A thick client used to simplify the use of SP-API Uploads. + */ +public interface UploadsSequencer { + /** + * Retrieve a document and related metadata from SP-API Uploads. + * @param obfuscatedCustomerId the obfuscated customer ID of the document owner. + * @param documentId the document ID of the document being retrieved. + * @return A DownloadBundle to be used to retrieve the requested document. + * @throws RuntimeException if an exceptional case was encountered but the action should be retried. + * @throws RuntimeException if an exceptional case was encountered and the action should not be retried. + * @throws RuntimeException if an exceptional case was encountered and the cause was unexpected. + */ + // DownloadBundle get(String obfuscatedCustomerId, String documentId) throws RuntimeException; + + UploadDestination createDestination(String feedType, String contentType, long documentLength) + throws ApiException; + /** + * Delivers a document and related metadata to SP-API Uploads. + * @param uploadDestination metadata for the document being uploaded. + * @param contentType the content type + * @param documentLength the length in bytes of the document. + * @param inputStream the InputStream from which the document should be read for upload. + * @return A String document ID. + * @throws RuntimeException if an exceptional case was encountered but the action should be retried. + * @throws RuntimeException if an exceptional case was encountered and the action should not be retried. + * @throws RuntimeException if an exceptional case was encountered and the cause was unexpected. + */ + UploadDetails uploadToDestination(UploadDestination uploadDestination, String contentType, long documentLength, + InputStream inputStream) throws RuntimeException; + + UploadDetails createDestinationAndUpload(String feedType, String contentType, File fileName) + throws ApiException, FileNotFoundException; + + UploadDetails createDestinationAndUpload(String feedType, String contentType, long documentLength, + InputStream stream) throws ApiException; +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/ValidationFailureException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/ValidationFailureException.java new file mode 100644 index 0000000..4957841 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/ValidationFailureException.java @@ -0,0 +1,38 @@ +package com.amazon.spapi.sequencing.client; + + +/** + * Exception thrown if document integrity has been compromised during handling and is not considered safe to consume. + */ +public class ValidationFailureException extends RuntimeException { + + /** + * Constructor taking both a message and cause. + * @param message + * the message + * @param cause + * the cause + */ + public ValidationFailureException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message + * the message + */ + public ValidationFailureException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause + * the cause + */ + public ValidationFailureException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/AccessMechanism.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/AccessMechanism.java new file mode 100644 index 0000000..167743d --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/AccessMechanism.java @@ -0,0 +1,15 @@ +package com.amazon.spapi.sequencing.client.impl; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.experimental.FieldDefaults; + +@AllArgsConstructor +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +@Getter +public enum AccessMechanism { + UPLOAD("upload"), DOWNLOAD("download"); + + private String description; +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/DownloadsSequencerImpl.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/DownloadsSequencerImpl.java new file mode 100644 index 0000000..6bc524a --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/DownloadsSequencerImpl.java @@ -0,0 +1,307 @@ +package com.amazon.spapi.sequencing.client.impl; + +import com.amazon.spapi.sequencing.client.DownloadsSequencer; +import com.amazon.spapi.sequencing.crypto.EncryptionException; +import com.google.common.base.Strings; + +import io.swagger.client.model.EncryptionDetails; +import io.swagger.client.model.UploadDestination; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.experimental.FieldDefaults; +import lombok.extern.apachecommons.CommonsLog; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.util.EntityUtils; + +import javax.crypto.Cipher; +import java.io.*; +import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; + +import static com.amazon.spapi.sequencing.client.impl.SequencerHelper.S3_IAD_ENDPOINT; +import static com.amazon.spapi.sequencing.client.impl.SequencerHelper.S3_SEA_ENDPOINT; + +@Builder +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +@CommonsLog +public class DownloadsSequencerImpl implements DownloadsSequencer { + @Builder.Default + private final SequencerHelper helper = SequencerHelper.defaultSequencerHelper(); + + @Override + public InputStream downloadAndDecryptReportContent(UploadDestination destination, boolean isGzipped) { + boolean hasError = false; + InputStream result = null; + String truncatedUrl = null; + try { + + String url = destination.getUrl(); + + if (Strings.isNullOrEmpty(url)) { + throw new RuntimeException("The returned download URL is null or empty."); + } + + // Acquire the file + HttpClient httpClient = helper.getHttpClientFactory().newGetClient(); + HttpGet httpGet = new HttpGet(url); + + truncatedUrl = helper.removeQueryFromUrl(url); + log.info(String.format("Downloading document with destination ID %s using URL %s (signing info redacted)", + destination.getUploadDestinationId(), truncatedUrl + )); + HttpResponse httpResponse = httpClient.execute(httpGet); + if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { + boolean isS3UsStandardRegion = false; + if (url.contains(S3_IAD_ENDPOINT)) { + isS3UsStandardRegion = true; + url = url.replaceFirst(Pattern.quote(S3_IAD_ENDPOINT), S3_SEA_ENDPOINT); + } else if (url.contains(S3_SEA_ENDPOINT)) { + isS3UsStandardRegion = true; + url = url.replaceFirst(Pattern.quote(S3_SEA_ENDPOINT), S3_IAD_ENDPOINT); + } + + if (isS3UsStandardRegion) { + log.info("S3 Returned a 404 in the US Standard Region." + + " This likely indicates issues with eventual consistency and should be rare." + + " Attempting to find object in peer region." + + " See: https://w.amazon.com/index.php/S3#What_About_Eventual_Consistency.3F"); + + // Consume the previous response so that the HTTP client connection can be reused + EntityUtils.consumeQuietly(httpResponse.getEntity()); + + httpGet = new HttpGet(url); + httpResponse = httpClient.execute(httpGet); + } + } + + // Handle error responses + helper.handleErrorResponseCodes(httpResponse, AccessMechanism.DOWNLOAD); + + HttpEntity entity = httpResponse.getEntity(); + if (entity == null) { + throw new RuntimeException("The HTTP store returned success but no document."); + } + result = entity.getContent(); + result = decryptReportContent(entity.getContent(), destination.getEncryptionDetails(), isGzipped); + return result; + } catch (ClientProtocolException e) { + // Indicates an error in the HTTP protocol + hasError = true; + throw new RuntimeException( + "Unable to download the file.", e); + } catch (EncryptionException e) { + // Unable to perform cipher operations + hasError = true; + throw new RuntimeException( + "Unable to perform cipher operations.", e); + } catch (IOException e) { + hasError = true; + // Unable to download the file or operate on the stream + throw new RuntimeException( + String.format("Unable to download the file using URL %s (signing info redacted) " + + "or manipulate the InputStream.", truncatedUrl), + e + ); + } finally { + // kill the stream if we are throwing an exception + if (hasError) { + IOUtils.closeQuietly(result); + } + } + } + + @Override + public InputStream decryptReportContent(File reportFile, EncryptionDetails details, boolean isGzipped) + throws IOException, EncryptionException { + return decryptReportContent(new FileInputStream(reportFile), details, isGzipped); + } + + private InputStream decryptReportContent(InputStream input, EncryptionDetails details, boolean isGzipped) + throws EncryptionException, IOException { + InputStream resultStream = input; + // If encrypted, decipher the stream + if (details != null && EncryptionDetails.StandardEnum.AES.equals(details.getStandard())) { + try { + resultStream = helper.buildCipherInputStream(details, resultStream, Cipher.DECRYPT_MODE); + + } catch (IllegalArgumentException | IllegalStateException e) { + throw new EncryptionException(e); + } + } + + // Determine if the stream should be unzipped as well + if (isGzipped) { + resultStream = new GZIPInputStream(resultStream); + } + + return resultStream; + } + + @Override + public void downloadDecryptThenWriteFile(UploadDestination destination, boolean isGzipped, File fileToWriteTo) + throws IOException { + try ( + InputStream result = downloadAndDecryptReportContent(destination, isGzipped); + FileOutputStream output = new FileOutputStream(fileToWriteTo); + ) { + IOUtils.copyLarge(result, output); + } + } + + @Override + public void decryptThenWriteFile(File reportFile, EncryptionDetails details, boolean isGzipped, + File fileToWriteTo) throws IOException, EncryptionException { + try ( + InputStream result = decryptReportContent(reportFile,details, isGzipped); + FileOutputStream output = new FileOutputStream(fileToWriteTo); + ) { + IOUtils.copyLarge(result, output); + } + + } + + /* + + + + */ + /** {@inheritDoc} *//* + + @Override + public DownloadBundle get(String obfuscatedCustomerId, String documentId) + throws RuntimeException, RuntimeException, RuntimeException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(obfuscatedCustomerId), + "obfuscatedMerchantCustomerId is null or empty."); + Preconditions.checkArgument(!Strings.isNullOrEmpty(documentId), "documentId is null or empty."); + Preconditions.checkState(httpClientFactory != null, "The httpClientFactory is null."); + + // We need the expanded scope so that we can close the stream in the case of an exception. + InputStream resultStream = null; + DownloadBundle result = null; + String truncatedUrl = null; + try { + // Acquire the document metadata, document receipt URL and encryption details from Tortuga. + GetDownloadUrlInput getUrlInput = new GetDownloadUrlInput(); + getUrlInput.setDocumentId(documentId); + getUrlInput.setObfuscatedCustomerId(obfuscatedCustomerId); + GetDownloadUrlOutput getUrlOutput = + GetDownloadUrlSyncJobBuilder.build(getUrlInput, metricsStrategy, client).run(); + DocumentMetadata metadata = getUrlOutput.getDocumentMetadata(); + if (metadata == null) { + throw new RuntimeException("The returned document metadata is null."); + } + String url = getUrlOutput.getUrl(); + if (Strings.isNullOrEmpty(url)) { + throw new RuntimeException("The returned download URL is null or empty."); + } + Date creationTimestamp = getUrlOutput.getCreationTimestamp(); + if (creationTimestamp == null) { + throw new RuntimeException("The returned document creationDate is null."); + } + + getUrlOutput.setDocumentMetadata(null); + + // Acquire the file + HttpClient httpClient = httpClientFactory.newGetClient(); + HttpGet httpGet = new HttpGet(url); + + truncatedUrl = removeQueryFromUrl(url); + log.info(String.format("Downloading document with ID %s using URL %s (signing info redacted)", + documentId, truncatedUrl)); + long startTimeMillis = System.nanoTime() / 1000000L; + HttpResponse httpResponse = httpClient.execute(httpGet); + if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { + boolean isS3UsStandardRegion = false; + if (url.contains(S3_IAD_ENDPOINT)) { + isS3UsStandardRegion = true; + url = url.replaceFirst(Pattern.quote(S3_IAD_ENDPOINT), S3_SEA_ENDPOINT); + } else if (url.contains(S3_SEA_ENDPOINT)) { + isS3UsStandardRegion = true; + url = url.replaceFirst(Pattern.quote(S3_SEA_ENDPOINT), S3_IAD_ENDPOINT); + } + + if (isS3UsStandardRegion) { + log.info("S3 Returned a 404 in the US Standard Region." + + " This likely indicates issues with eventual consistency and should be rare." + + " Attempting to find object in peer region." + + " See: https://w.amazon.com/index.php/S3#What_About_Eventual_Consistency.3F"); + + // Consume the previous response so that the HTTP client connection can be reused + EntityUtils.consumeQuietly(httpResponse.getEntity()); + + httpGet = new HttpGet(url); + httpResponse = httpClient.execute(httpGet); + } + } + long endTimeMillis = System.nanoTime() / 1000000L; + serviceMetrics.addTime(S3_GET_TIME_METRIC, endTimeMillis - startTimeMillis, SI.MILLI(SI.SECOND)); + + // Handle error responses + handleErrorResponseCodes(httpResponse); + + HttpEntity entity = httpResponse.getEntity(); + if (entity == null) { + throw new RuntimeException("The HTTP store returned success but no document."); + } + resultStream = entity.getContent(); + + // If this document has a sha256sum in the metadata, wrap with DigestInputStream to calculate the sha256 sum + // for validation of document integrity + String sha256sumStr = getUrlOutput.getSha256sum(); + if (sha256sumStr != null) { + resultStream = new DocumentValidatorInputStream(resultStream, BASE64_DECODER.decode(sha256sumStr)); + } + + // If encrypted, decipher the stream + if (getUrlOutput.isEncrypted()) { + try { + EncryptionInfo encryptionInfo = getUrlOutput.getEncryptionInfo(); + resultStream = buildCipherInputStream(encryptionInfo, resultStream, Cipher.DECRYPT_MODE); + + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalStateException e) { + throw new RuntimeException(e); + } + } + + // Determine if the stream should be unzipped as well + if (metadata.isDocumentGzipped()) { + resultStream = new GZIPInputStream(resultStream); + } + + // build the result + result = new DownloadBundle(metadata, resultStream, creationTimestamp.getTime()); + } catch (ClientProtocolException e) { + // Indicates an error in the HTTP protocol + throw new RuntimeException( + "Unable to download the file.", e); + } catch (EncryptionException e) { + // Unable to perform cipher operations + throw new RuntimeException( + "Unable to perform cipher operations.", e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("Couldn't get a Sha256 Message Digest instance", e); + } catch (IOException e) { + // Unable to download the file or operate on the stream + throw new RuntimeException( + String.format("Unable to download the file using URL %s (signing info redacted) " + + "or manipulate the InputStream.", truncatedUrl), + e); + } finally { + // kill the stream if we are throwing an exception + if (result == null) { + IOUtils.closeQuietly(resultStream); + } + } + return result; + } +*/ + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/HttpClientFactory.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/HttpClientFactory.java new file mode 100644 index 0000000..6ac32e7 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/HttpClientFactory.java @@ -0,0 +1,19 @@ +package com.amazon.spapi.sequencing.client.impl; + +import org.apache.http.client.HttpClient; + +/** + * Constructs purpose built HttpClient instances. + */ +public interface HttpClientFactory { + /** + * Constructs an HttpClient instance configured for GET requests. + * @return an HttpClient instance. + */ + HttpClient newGetClient(); + /** + * Constructs an HttpClient instance configured for PUT requests. + * @return an HttpClient instance. + */ + HttpClient newPutClient(); +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SPAPIUploadDestinationsHttpClientFactory.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SPAPIUploadDestinationsHttpClientFactory.java new file mode 100644 index 0000000..ac8d225 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SPAPIUploadDestinationsHttpClientFactory.java @@ -0,0 +1,71 @@ +package com.amazon.spapi.sequencing.client.impl; + +import com.google.common.collect.ImmutableMap; +import lombok.Setter; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.CoreConnectionPNames; +import org.apache.http.params.CoreProtocolPNames; + +import java.util.Map; + +/** + * HttpClientFactory implementation for SP-API Upload Destinations. + */ +@Setter +public class SPAPIUploadDestinationsHttpClientFactory implements HttpClientFactory { + private static final Map DEFAULT_CONNECTION_PARAMETERS; + + static { + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.put(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); + builder.put(CoreConnectionPNames.SO_TIMEOUT, 10000); + builder.put(CoreConnectionPNames.SO_KEEPALIVE, Boolean.TRUE); + + DEFAULT_CONNECTION_PARAMETERS = builder.build(); + } + + /** + *http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html. + */ + private Map getClientConnectionParametersOverrides = DEFAULT_CONNECTION_PARAMETERS; + + /** + *http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html. + */ + private Map putClientConnectionParametersOverrides = DEFAULT_CONNECTION_PARAMETERS; + + private String userAgent; + + /** {@inheritDoc} */ + @Override + public HttpClient newGetClient() { + return newHttpClient(getClientConnectionParametersOverrides); + } + + /** {@inheritDoc} */ + @Override + public HttpClient newPutClient() { + return newHttpClient(putClientConnectionParametersOverrides); + } + + /** + * Helper function to create a new DefaultHttpClient with the given connectionParameter Overrides. + * @param connectionParameters used to override the default properties of the HttpClient. + * @return a new, configured HttpClient. + */ + private HttpClient newHttpClient(Map connectionParameters) { + // TODO: evaluate necessity + // Preconditions.checkState(!Strings.isNullOrEmpty(userAgent), "You must supply a User-Agent."); + + DefaultHttpClient client = new DefaultHttpClient(); + for (Map.Entry param : connectionParameters.entrySet()) { + client.getParams().setParameter(param.getKey(), param.getValue()); + } + + if (userAgent != null) { + client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent); + } + return client; + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SequencerHelper.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SequencerHelper.java new file mode 100644 index 0000000..45bfb5f --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/SequencerHelper.java @@ -0,0 +1,156 @@ +package com.amazon.spapi.sequencing.client.impl; + +import com.amazon.spapi.sequencing.crypto.CryptoProvider; +import com.amazon.spapi.sequencing.crypto.EncryptionException; +import com.amazon.spapi.sequencing.crypto.EncryptionMaterials; +import com.amazon.spapi.sequencing.crypto.SymmetricCryptoProvider; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import io.swagger.client.model.EncryptionDetails; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.experimental.FieldDefaults; +import lombok.extern.apachecommons.CommonsLog; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpResponseException; +import org.apache.http.util.EntityUtils; + +import javax.crypto.CipherInputStream; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Base64; +import java.util.Objects; + +@Builder +@Getter +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +@CommonsLog +public class SequencerHelper { + + static final String SHA_256 = "SHA-256"; + static final String MD5 = "MD5"; + static final String S3_IAD_ENDPOINT = "s3-external-1.amazonaws.com"; + static final String S3_SEA_ENDPOINT = "s3-external-2.amazonaws.com"; + static final String S3_GET_TIME_METRIC = "S3GetTime"; + static final String S3_PUT_TIME_METRIC = "S3PutTime"; + static final String REQUIRED_KEY_ALGORITHM = "AES"; + static final int AES_BLOCK_SIZE = 16; + static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); + static final Base64.Decoder BASE64_DECODER = Base64.getDecoder(); + + private CryptoProvider cryptoProvider; + + private HttpClientFactory httpClientFactory; + + public static SequencerHelper defaultSequencerHelper() { + return SequencerHelper.builder() + .cryptoProvider(new SymmetricCryptoProvider()) + .httpClientFactory(new SPAPIUploadDestinationsHttpClientFactory()) + .build(); + } + + /** + * Remove the query string from a signed S3 URL to hide access key, signature, expiration info when logging. + * + * @param signedS3Url Signed S3 URL to truncate + * @return truncated URL + */ + String removeQueryFromUrl(String signedS3Url) { + if (signedS3Url == null) { + return null; + } + + try { + URL url = new URL(signedS3Url); + + if (url.getQuery() == null) { + return signedS3Url; + } else { + return signedS3Url.replace(url.getQuery(), ""); + } + } catch (MalformedURLException e) { + return null; + } + } + + + /** + * Wraps the provided InputStream in a CipherInputStream in DECRYPT mode. + * + * @param encryptionDetails The EncryptionInfo to use in constructing the Cipher. + * @param stream The InputStream to wrap. + * @param mode The cipher mode. + * @return The wrapped InputStream + * @throws EncryptionException if initializing the Cipher fails. + */ + InputStream buildCipherInputStream(EncryptionDetails encryptionDetails, InputStream stream, int mode) + throws EncryptionException { + Preconditions.checkArgument(!Objects.isNull(encryptionDetails), "Encryption details missing"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(encryptionDetails.getInitializationVector()), + "iv is null or empty." + ); + Preconditions.checkArgument(!Strings.isNullOrEmpty(encryptionDetails.getKey()), "key is null or empty."); + Preconditions.checkState(cryptoProvider != null, "The CryptoProvider must be non-null."); + + String key = encryptionDetails.getKey(); + String iv = encryptionDetails.getInitializationVector(); + + EncryptionMaterials materials = new EncryptionMaterials(new SecretKeySpec(BASE64_DECODER.decode(key), + REQUIRED_KEY_ALGORITHM + ), BASE64_DECODER.decode(iv)); + return new CipherInputStream(stream, cryptoProvider.getInitializedCipher(mode, materials)); + } + + + /** + * Examine the HttpResponse object and throw appropriate exceptions for non-success status + * codes. + * + * @param httpResponse the HttpResponse object to examine. + * @throws RuntimeException if the status code is not 200 level or 400 level (except 403 errors). + * @throws RuntimeException if the HttpResponse, or its nested StatusLine object are null. Or if the status + * code is a 400 level HTTP response code (except 403 errors, which are retriable). + */ + void handleErrorResponseCodes(HttpResponse httpResponse, AccessMechanism accessMechanism) throws RuntimeException { + // Handle error responses + if (httpResponse == null || httpResponse.getStatusLine() == null) { + throw new RuntimeException("Unable to " + accessMechanism.getDescription() + " the document."); + } + int status = httpResponse.getStatusLine().getStatusCode(); + log.info(String.format("Document " + accessMechanism.getDescription() + " returned status code %d", status)); + int statusRange = status / 100; + if (statusRange != 2) { // Not 2XX + HttpEntity entity = httpResponse.getEntity(); + if (entity != null) { + try { + log.warn(EntityUtils.toString(entity, "UTF-8")); + } catch (IOException e) { + log.warn("Got IOException when trying to decode response body into a String", e); + } + } + String reason = httpResponse.getStatusLine().getReasonPhrase(); + HttpResponseException error = new HttpResponseException(status, reason); + // A 403 response is typically due to the signed URL expiring before use. + // The root cause of the expiration is usually clock skew, so treat this as retriable. + if (status == HttpStatus.SC_FORBIDDEN) { + throw new RuntimeException( + String.format("A 403 response was returned. This typically indicates that the signed URL " + + "expired before use (usually due to clock skew). %s %s", status, reason), + error + ); + } else if (statusRange == 4) { // 4XX + throw new RuntimeException( + String.format("A 400 level response was returned: %s %s", status, reason), error); + } else { // 1XX, 3XX, 5XX + throw new RuntimeException( + String.format("A non 200/400 level response was returned: %s %s", status, reason), error); + } + } + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/UploadsSequencerImpl.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/UploadsSequencerImpl.java new file mode 100644 index 0000000..9a09842 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/impl/UploadsSequencerImpl.java @@ -0,0 +1,178 @@ +package com.amazon.spapi.sequencing.client.impl; + +import com.amazon.spapi.sequencing.client.UploadDetails; +import com.amazon.spapi.sequencing.client.UploadsSequencer; +import com.amazon.spapi.sequencing.crypto.EncryptionException; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import io.swagger.client.ApiException; +import io.swagger.client.api.UploadsApi; +import io.swagger.client.model.CreateUploadDestinationResponse; +import io.swagger.client.model.EncryptionDetails; +import io.swagger.client.model.UploadDestination; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.experimental.FieldDefaults; +import lombok.extern.apachecommons.CommonsLog; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.InputStreamEntity; + +import javax.crypto.Cipher; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * This implementation performs the expected crypto and compression work on the provided streams. + */ +@Builder +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +@CommonsLog +public class UploadsSequencerImpl implements UploadsSequencer { + + @Builder.Default + private final SequencerHelper helper = SequencerHelper.defaultSequencerHelper(); + + private UploadsApi uploadsApi; + + /** + * {@inheritDoc} + */ + @Override + public UploadDetails uploadToDestination(UploadDestination uploadDestination, String contentType, + long documentLength, + InputStream inputStream) throws RuntimeException { + Preconditions.checkArgument(uploadDestination != null, "documentMetadata is null."); + Preconditions.checkArgument(inputStream != null, "inputStream is null."); + Preconditions.checkArgument(documentLength >= 0, "documentLength must be at least zero."); + Preconditions.checkState(helper.getHttpClientFactory() != null, "The httpClientFactory is null."); + + String truncatedUrl = null; + try { + + final String uploadDestinationId = uploadDestination.getUploadDestinationId(); + if (Strings.isNullOrEmpty(uploadDestinationId)) { + throw new RuntimeException("The returned uploadDestinationId is null or empty."); + } + EncryptionDetails encryptionDetails = uploadDestination.getEncryptionDetails(); + if (encryptionDetails == null) { + throw new RuntimeException("The returned encryptionDetails is null."); + } + final String url = uploadDestination.getUrl(); + if (Strings.isNullOrEmpty(url)) { + throw new RuntimeException("The returned upload URL is null or empty."); + } + + // Wrap the stream with a CipherStream + inputStream = helper.buildCipherInputStream(encryptionDetails, inputStream, Cipher.ENCRYPT_MODE); + + // Wrap the cipher stream with a sha256 digest stream. + DigestInputStream sha256sumStream = new DigestInputStream(inputStream, MessageDigest.getInstance( + SequencerHelper.SHA_256)); + inputStream = sha256sumStream; + + truncatedUrl = helper.removeQueryFromUrl(url); + + // Put the file + HttpClient httpClient = helper.getHttpClientFactory().newPutClient(); + HttpPut httpPut = new HttpPut(url); + // This content length calculation is specific to AES + long contentLength = (documentLength / SequencerHelper.AES_BLOCK_SIZE + 1) * SequencerHelper.AES_BLOCK_SIZE; + // This sets the Content-Length header since we specified the contentLength + HttpEntity document = new InputStreamEntity(inputStream, contentLength); + httpPut.setHeader("Content-Type", contentType); + httpPut.setEntity(document); + + log.info(String.format("Uploading document with ID %s using URL %s (signing info redacted)", + uploadDestinationId, truncatedUrl + )); + HttpResponse httpResponse = httpClient.execute(httpPut); + + // Handle error responses + helper.handleErrorResponseCodes(httpResponse, AccessMechanism.UPLOAD); + log.info(String.format("Successfully uploaded document with ID %s", uploadDestinationId)); + + byte[] sha256sumDigest = sha256sumStream.getMessageDigest().digest(); + String sha256sum = SequencerHelper.BASE64_ENCODER.encodeToString(sha256sumDigest); + + + return UploadDetails.builder() + .sha256Sum(sha256sum) + .uploadDestinationId(uploadDestinationId) + .build(); + } catch (ClientProtocolException e) { + // Indicates an error in the HTTP protocol + throw new RuntimeException("Unable to upload the file.", e); + } catch (EncryptionException e) { + // Unable to perform cipher operations + throw new RuntimeException("Unable to perform cipher operations.", e); + } catch (IOException e) { + // Unable to upload the file or operate on the stream + throw new RuntimeException( + String.format("Unable to upload the file using URL %s (signing info redacted) " + + "or manipulate the InputStream.", truncatedUrl), + e + ); + } catch (NoSuchAlgorithmException e) { + // Unable to get a MAC on the encrypted doc + throw new IllegalStateException("Unable to create MessageDigest. Required algorithm not present.", e); + } finally { + IOUtils.closeQuietly(inputStream); + } + } + + @Override + public UploadDetails createDestinationAndUpload(String feedType, String contentType, File fileName) + throws FileNotFoundException, ApiException { + long documentLength = fileName.length(); + + return createDestinationAndUpload(feedType, contentType, documentLength, new FileInputStream(fileName)); + } + + @Override + public UploadDetails createDestinationAndUpload(String feedType, String contentType, long documentLength, + InputStream stream) throws ApiException { + + UploadDestination destination = createDestination(feedType, contentType, documentLength); + + return uploadToDestination(destination, contentType, documentLength, stream); + } + + public UploadDestination createDestination(String feedType, String contentType, long documentLength) + throws ApiException { + Preconditions.checkArgument(documentLength < (long) Integer.MAX_VALUE, + "Document length cannot exceed " + Integer.MAX_VALUE + " bytes." + ); + CreateUploadDestinationResponse response = uploadsApi.createUploadDestinationForFeed(feedType, contentType, + (int) documentLength + ); + return response.getPayload(); + } +/* + TODO + + public void submitFeed(UploadDetails uploadDetails) { + + CompleteDocumentUploadInput completeUploadInput = new CompleteDocumentUploadInput(); + completeUploadInput.setDocumentId(uploadDestinationId); + completeUploadInput.setObfuscatedCustomerId(uploadDestination.getObfuscatedCustomerId()); + completeUploadInput.setCompleteUploadToken(getUploadUrlOutput.getCompleteUploadToken()); + completeUploadInput.setSha256sum(sha256sum); + completeUploadInput.setContentMd5(contentMd5); + CompleteDocumentUploadSyncJobBuilder.build(completeUploadInput, metricsStrategy, client).runOnce(); + + } +*/ + + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/DemuxOutputStream.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/DemuxOutputStream.java new file mode 100644 index 0000000..6fe8a68 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/DemuxOutputStream.java @@ -0,0 +1,93 @@ +package com.amazon.spapi.sequencing.client.util; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.io.OutputStream; + +/** + * {@code OutputStream} wrapper class that forwards data written to this stream to an associated stream and allows + * switching of the associated stream on the fly. Associated stream can be {@code null} in which case all operations + * are, essentially, NOOP. + */ +public class DemuxOutputStream extends OutputStream { + + private OutputStream out; + + /** + * Creates a new {@code DemuxOutputStream} wrapper around given output stream. Accepts {@code null}. + * @param stream + * the output stream (can be {@code null}) + */ + public DemuxOutputStream(@Nullable OutputStream stream) { + out = stream; + } + + /** + * Binds new output stream. Originally bound output stream will be flushed and returned. Accepts {@code null}. + * @param stream + * the new input stream to bind (can be {@code null}) + * @return the previously bound input stream (can be {@code null}) + * @throws IOException + * if there was an error flushing the currently bound stream + */ + @Nullable + public OutputStream bind(@Nullable OutputStream stream) throws IOException { + OutputStream tmp = out; + out = stream; + if (tmp != null) { + tmp.flush(); + } + return tmp; + } + + /** + * {@inheritDoc} + */ + @Override + public void close() throws IOException { + if (out != null) { + out.close(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void flush() throws IOException { + if (out != null) { + out.flush(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(byte[] data, int offset, int len) throws IOException { + if (out != null) { + out.write(data, offset, len); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(byte[] data) throws IOException { + if (out != null) { + out.write(data); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(int data) throws IOException { + if (out != null) { + out.write(data); + } + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/TempInputStream.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/TempInputStream.java new file mode 100644 index 0000000..59310d8 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/client/util/TempInputStream.java @@ -0,0 +1,133 @@ +package com.amazon.spapi.sequencing.client.util; + +import lombok.extern.apachecommons.CommonsLog; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import java.io.*; + +/** + * An InputStream from a temporary file that deletes the file after it was completely read or closed. + */ +@CommonsLog +public class TempInputStream extends InputStream { + + private final FileInputStream fileStream; + private final File tempFile; + + /** + * Creates a new {@code TempInputStream} for a given file. + * @param file + * the file + * @throws FileNotFoundException + * if file doesn't exist + */ + public TempInputStream(File file) throws FileNotFoundException { + fileStream = new FileInputStream(file); + tempFile = file; + } + + /** + * File cleanup. + */ + private void cleanup() { + IOUtils.closeQuietly(fileStream); + + try { + FileUtils.forceDelete(tempFile); + } catch (FileNotFoundException e) { + // Ignore not found exceptions + return; + } catch (IOException e) { + log.warn("IOException when deleting TempInputStream file", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void close() throws IOException { + try { + fileStream.close(); + } finally { + cleanup(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public int read() throws IOException { + try { + int read = fileStream.read(); + if (read == -1) { + cleanup(); + } + return read; + } catch (IOException e) { + cleanup(); + throw e; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int read(byte[] bytes, int off, int len) throws IOException { + try { + int read = fileStream.read(bytes, off, len); + if (read == -1) { + cleanup(); + } + return read; + } catch (IOException e) { + cleanup(); + throw e; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int read(byte[] bytes) throws IOException { + try { + int read = fileStream.read(bytes); + if (read == -1) { + cleanup(); + } + return read; + } catch (IOException e) { + cleanup(); + throw e; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int available() throws IOException { + return fileStream.available(); + } + + /** + * {@inheritDoc} + */ + @Override + public long skip(long n) throws IOException { + return fileStream.skip(n); + } + + /** + * {@inheritDoc} + */ + @Override + protected void finalize() throws IOException { + close(); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/AsymmetricCryptoProvider.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/AsymmetricCryptoProvider.java new file mode 100644 index 0000000..9e48b2e --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/AsymmetricCryptoProvider.java @@ -0,0 +1,195 @@ +package com.amazon.spapi.sequencing.crypto; + +import com.google.common.base.Preconditions; + +import javax.annotation.Nonnull; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.Signature; +import java.security.InvalidKeyException; +import java.security.SignatureException; + + +/** + * An Asymmetric verison of {@link CryptoProvider} that uses RSA. + * Encryption is performed using RSA with PKCS1Padding. + * Signing is performed using SHA256withRSA. + */ +public class AsymmetricCryptoProvider implements CryptoProvider { + private static final String REQUIRED_KEY_ALGORITHM = "RSA"; + private static final String SIGNATURE_ALGORITHM = "SHA256withRSA"; + private static final String ENCRYPTION_ALGORITHM = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding"; + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] encrypt(@Nonnull byte[] plainText, @Nonnull EncryptionMaterials materials) + throws EncryptionException { + Preconditions.checkArgument(plainText != null, "plainText must be non-null."); + + Cipher cipher = getInitializedCipher(Cipher.ENCRYPT_MODE, materials); + return applyCipher(cipher, plainText); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] decrypt(@Nonnull byte[] cipherText, @Nonnull EncryptionMaterials materials) + throws EncryptionException { + Preconditions.checkArgument(cipherText != null, "cipherText must be non-null."); + + Cipher cipher = getInitializedCipher(Cipher.DECRYPT_MODE, materials); + return applyCipher(cipher, cipherText); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] sign(@Nonnull byte[] bytesToSign, @Nonnull EncryptionMaterials materials) + throws SignatureGenerationException { + Preconditions.checkArgument(bytesToSign != null, "bytesToSign must be non-null."); + + PrivateKey key = validatePrivateKey(materials); + + byte[] signatureBytes; + try { + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initSign(key, new SecureRandom()); + signature.update(bytesToSign); + signatureBytes = signature.sign(); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(String.format("NoSuchAlgorithmException for %s", SIGNATURE_ALGORITHM), e); + } catch (InvalidKeyException e) { + throw new SignatureGenerationException(e); + } catch (SignatureException e) { + throw new SignatureGenerationException(e); + } + + return signatureBytes; + } + + /** + * {@inheritDoc} + */ + @Override + public void validateSignature(@Nonnull byte[] signatureBytes, @Nonnull byte[] bytesToSign, + @Nonnull EncryptionMaterials materials) throws SignatureGenerationException, + SignatureValidationException { + Preconditions.checkArgument(signatureBytes != null, "signatureBytes must be non-null."); + Preconditions.checkArgument(bytesToSign != null, "bytesToSign must be non-null."); + + PublicKey key = validatePublicKey(materials); + + try { + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initVerify(key); + signature.update(bytesToSign); + if (!signature.verify(signatureBytes)) { + throw new SignatureValidationException("Signature mismatch. Possible tampering attempt."); + } + } catch (InvalidKeyException e) { + throw new SignatureGenerationException(e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(String.format("NoSuchAlgorithmException for %s", SIGNATURE_ALGORITHM), e); + } catch (SignatureException e) { + throw new SignatureGenerationException(e); + } + } + + /** + * Helper function to apply a cipher to some data and return the results. Performs exception mapping. + * @param cipher the Cipher to apply. + * @param text the text to apply the cipher to + * @return the text generated by applying the cipher to the input text + * @throws EncryptionException if there is a problem applying the cipher. + */ + @Nonnull + private byte[] applyCipher(@Nonnull Cipher cipher, @Nonnull byte[] text) throws EncryptionException { + try { + return cipher.doFinal(text); + } catch (IllegalBlockSizeException e) { + throw new EncryptionException(e); + } catch (BadPaddingException e) { + throw new EncryptionException(e); + } + } + + /** + * {@inheritDoc} + */ + @Nonnull + public Cipher getInitializedCipher(int mode, @Nonnull EncryptionMaterials materials) throws EncryptionException { + + Key key; + if (Cipher.DECRYPT_MODE == mode) { + key = validatePrivateKey(materials); + } else { + key = validatePublicKey(materials); + } + + Cipher cipher; + try { + cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); + cipher.init(mode, key, new SecureRandom()); + } catch (InvalidKeyException e) { + throw new EncryptionException(e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(String.format("NoSuchAlgorithmException for %s", SIGNATURE_ALGORITHM), e); + } catch (NoSuchPaddingException e) { + throw new IllegalStateException(String.format("NoSuchPaddingException for %s", SIGNATURE_ALGORITHM), e); + } + + return cipher; + } + + /** + * Helper function to ensure EncryptionMaterials are valid and the Key matches a specific class. + * @param materials the Materials to validate + * @param clazz the expected class of the key. + * @param message The error message if the key is not of the expected class + * @return the key. + */ + @Nonnull + private Key validateKey(@Nonnull EncryptionMaterials materials, @Nonnull Class clazz, + @Nonnull String message) { + Preconditions.checkArgument(materials != null, "Materials must be non-null."); + Preconditions.checkArgument(REQUIRED_KEY_ALGORITHM.equals(materials.getKey().getAlgorithm()), + "AsymmetricCryptoProvider requires an RSA key."); + Preconditions.checkArgument(clazz.isAssignableFrom(materials.getKey().getClass()), message); + + return materials.getKey(); + } + + /** + * Helper function that validates that the EncryptionMaterials contain a valid PrivateKey. + * @param materials the materials to validate + * @return the PrivateKey + */ + @Nonnull + private PrivateKey validatePrivateKey(@Nonnull EncryptionMaterials materials) { + return (PrivateKey) validateKey(materials, PrivateKey.class, "Key must be of type PrivateKey."); + } + + /** + * Helper function that validates that the EncryptionMaterials contain a valid PublicKey. + * @param materials the materials to validate + * @return the PublicKey + */ + @Nonnull + private PublicKey validatePublicKey(@Nonnull EncryptionMaterials materials) { + return (PublicKey) validateKey(materials, PublicKey.class, "Key must be of type PublicKey."); + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoException.java new file mode 100644 index 0000000..02dfed4 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoException.java @@ -0,0 +1,34 @@ +package com.amazon.spapi.sequencing.crypto; + + +/** + * Base class for any Cryptographic exception that occurs. + */ +public class CryptoException extends Exception { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public CryptoException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public CryptoException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public CryptoException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoProvider.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoProvider.java new file mode 100644 index 0000000..9d0a5a1 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/CryptoProvider.java @@ -0,0 +1,66 @@ +package com.amazon.spapi.sequencing.crypto; + +import javax.annotation.Nonnull; +import javax.crypto.Cipher; + +/** + * This class of objects performs encryption, decryption, signing (or hmacing), and signature/hmac validation. + */ +public interface CryptoProvider { + + /** + * Encrypt plain text into cipher text. + * @param plainText the plain text to encrypt. + * @param materials the EncryptionMaterials to use to perform the encryption + * @return the cipherText + * @throws EncryptionException if any problems occur during encryption. + */ + @Nonnull + byte[] encrypt(@Nonnull byte[] plainText, @Nonnull EncryptionMaterials materials) throws EncryptionException; + + /** + * Decrypts the cipher text into plain text. + * @param cipherText the cipher text to decrypt + * @param materials the EncryptionMaterials to use to perform decryption + * @return the plain text + * @throws EncryptionException if any problems occur during decryption. + */ + @Nonnull + byte[] decrypt(@Nonnull byte[] cipherText, @Nonnull EncryptionMaterials materials) throws EncryptionException; + + /** + * Create a signature or hmac based on the bytes provided. + * @param bytesToSign the bytes to use when calculating the signature or hmac + * @param materials the materials to use when generating the signature or hmac + * @return the signature/hmac bytes + * @throws SignatureGenerationException if any problems occur during signing/hmacing + */ + @Nonnull + byte[] sign(@Nonnull byte[] bytesToSign, @Nonnull EncryptionMaterials materials) + throws SignatureGenerationException; + + /** + * Validate that a signature/hmac matches the data that was signed. + * @param signature the signature/hmac to validate + * @param bytesToSign the data that was originally signed/hmac'd + * @param materials the materials to use when validating the signature or hmac + * @throws SignatureGenerationException if we are unable to generate the signature/hmac to validate against. + * @throws SignatureValidationException if the validation fails (i.e, the signature or hmac do not match). + * This indicates data corruption or a possible tampering attack. + */ + @Nonnull + void validateSignature(@Nonnull byte[] signature, @Nonnull byte[] bytesToSign, + @Nonnull EncryptionMaterials materials) throws SignatureGenerationException, + SignatureValidationException; + + /** + * Correctly Initialize a Cipher given the mode. The mode is specified by {@link Cipher}'s Mode constants. + * @param mode The mode of the cipher (i.e., encrypt or decrypt). See {@link Cipher}'s Mode constants. + * @param materials The EncryptionMaterials to initialize the cipher with + * @return an initialized Cipher object + * @throws EncryptionException if the Cipher could not be initialized. + */ + @Nonnull + Cipher getInitializedCipher(int mode, @Nonnull EncryptionMaterials materials) throws EncryptionException; + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/DefaultKeyConverter.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/DefaultKeyConverter.java new file mode 100644 index 0000000..893a783 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/DefaultKeyConverter.java @@ -0,0 +1,77 @@ +package com.amazon.spapi.sequencing.crypto; + +import com.google.common.base.Preconditions; + +import javax.annotation.Nonnull; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.security.*; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +/** + * Default implementation of the {@link KeyConverter} interface. + */ +public class DefaultKeyConverter implements KeyConverter { + private static final String SECRET_KEY_ALGORITHM = "AES"; + private static final String KEYPAIR_ALGORITHM = "RSA"; + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public SecretKey convertToSymmetricKey(@Nonnull byte[] material) { + Preconditions.checkArgument(material != null, "material must be non-null."); + return new SecretKeySpec(material, SECRET_KEY_ALGORITHM); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public PrivateKey convertToPrivateKey(@Nonnull byte[] material) throws com.amazon.spapi.sequencing.crypto.InvalidKeyException { + Preconditions.checkArgument(material != null, "material must be non-null."); + + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(material); + try { + KeyFactory factory = KeyFactory.getInstance(KEYPAIR_ALGORITHM); + return factory.generatePrivate(keySpec); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("RSA Algorithm is invalid. This indicates a badly configured Provider."); + } catch (InvalidKeySpecException e) { + throw new com.amazon.spapi.sequencing.crypto.InvalidKeyException("Invalid key.", e); + } + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public PublicKey convertToPublicKey(@Nonnull byte[] material) throws com.amazon.spapi.sequencing.crypto.InvalidKeyException { + Preconditions.checkArgument(material != null, "material must be non-null."); + + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(material); + try { + KeyFactory factory = KeyFactory.getInstance(KEYPAIR_ALGORITHM); + return factory.generatePublic(keySpec); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("RSA Algorithm is invalid. This indicates a badly configured Provider."); + } catch (InvalidKeySpecException e) { + throw new com.amazon.spapi.sequencing.crypto.InvalidKeyException("Invalid key.", e); + } + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public KeyPair convertToKeyPair(@Nonnull byte[] publicMaterial, @Nonnull byte[] privateMaterial) + throws InvalidKeyException { + return new KeyPair(convertToPublicKey(publicMaterial), convertToPrivateKey(privateMaterial)); + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionException.java new file mode 100644 index 0000000..14481ca --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionException.java @@ -0,0 +1,32 @@ +package com.amazon.spapi.sequencing.crypto; + +/** + * An exception that indicates an error occured during encryption or decryption of some data. + */ +public class EncryptionException extends CryptoException { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public EncryptionException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public EncryptionException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public EncryptionException(Throwable cause) { + super(cause); + } +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionMaterials.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionMaterials.java new file mode 100644 index 0000000..057feb9 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/EncryptionMaterials.java @@ -0,0 +1,60 @@ +package com.amazon.spapi.sequencing.crypto; + +import com.google.common.base.Preconditions; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.security.Key; +import java.util.Arrays; + +/** + * Key and initialization vector for use when encrypting things. + */ +@EqualsAndHashCode +public class EncryptionMaterials { + + @Nonnull + @Getter + private final Key key; + @Nullable + private final byte[] initializationVector; + + /** + * All Args Constructor. + * @param key non null Key for this EncryptionMaterials + * @param initializationVector Nullable initializationVector (used for CBC mode encryption). + */ + public EncryptionMaterials(@Nonnull Key key, @Nullable byte[] initializationVector) { + Preconditions.checkArgument(key != null, "Key must be non-null."); + + this.key = key; + this.initializationVector = copyInitializationVector(initializationVector); + } + + /** + * Get a copy of the initialization vector bytes. + * @return A copy of the initialization vector bytes. + */ + @Nullable + public byte[] getInitializationVector() { + return copyInitializationVector(initializationVector); + } + + /** + * A null-safe helper that copies the initialization vector bytes into a new array if the IV is non-null. + * @param iv the byte array to copy + * @return a copy of iv if iv is non null, otherwise null. + */ + @Nullable + private byte[] copyInitializationVector(byte[] iv) { + byte[] newInitializationVector = null; + if (iv != null) { + newInitializationVector = Arrays.copyOf(iv, iv.length); + } + + return newInitializationVector; + } +} + diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/InvalidKeyException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/InvalidKeyException.java new file mode 100644 index 0000000..17c7610 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/InvalidKeyException.java @@ -0,0 +1,34 @@ +package com.amazon.spapi.sequencing.crypto; + +/** + * InvalidKeyException indicates that a key was invalid. + * + */ +public class InvalidKeyException extends CryptoException { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public InvalidKeyException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public InvalidKeyException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public InvalidKeyException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/KeyConverter.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/KeyConverter.java new file mode 100644 index 0000000..d6df5fc --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/KeyConverter.java @@ -0,0 +1,50 @@ +package com.amazon.spapi.sequencing.crypto; + +import javax.annotation.Nonnull; +import javax.crypto.SecretKey; +import java.security.KeyPair; +import java.security.PrivateKey; +import java.security.PublicKey; + +/** + * A class of objects that converts the raw bytes of an encoded key into a Key. + */ +public interface KeyConverter { + + /** + * Convert a byte[] into a SecretKey. + * @param material the byte[] to covert + * @return the SecretKey + */ + @Nonnull + SecretKey convertToSymmetricKey(@Nonnull byte[] material); + + /** + * Convert a PKCS8 encoded byte[] into a PrivateKey. + * @param material the byte[] to covert + * @return the PrivateKey + * @throws InvalidKeyException if the byte[] is not a properly encoded key + */ + @Nonnull + PrivateKey convertToPrivateKey(@Nonnull byte[] material) throws InvalidKeyException; + + /** + * Convert a X509 encoded byte[] into a PublicKey. + * @param material the byte[] to covert + * @return the PublicKey + * @throws InvalidKeyException if the byte[] is not a properly encoded key + */ + @Nonnull + PublicKey convertToPublicKey(@Nonnull byte[] material) throws InvalidKeyException; + + /** + * Convert a X509 encoded byte[] and a PKCS8 encoded byte[] into a KeyPair. + * @param publicMaterial the X509 encoded byte[] to covert into the public key + * @param privateMaterial the PKCS8 encoded byte[] to covert into the private key + * @return the SecretKey + * @throws InvalidKeyException if either the public or private material is invalid + */ + @Nonnull + KeyPair convertToKeyPair(@Nonnull byte[] publicMaterial, @Nonnull byte[] privateMaterial) + throws InvalidKeyException; +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureException.java new file mode 100644 index 0000000..c121a20 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureException.java @@ -0,0 +1,33 @@ +package com.amazon.spapi.sequencing.crypto; + +/** + * Base class for exceptions related to Signature/HMAC problems. + */ +public class SignatureException extends CryptoException { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public SignatureException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public SignatureException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public SignatureException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureGenerationException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureGenerationException.java new file mode 100644 index 0000000..652f385 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureGenerationException.java @@ -0,0 +1,33 @@ +package com.amazon.spapi.sequencing.crypto; + +/** + * Indicates that an error occured when generating a Signature or HMAC. + */ +public class SignatureGenerationException extends SignatureException { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public SignatureGenerationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public SignatureGenerationException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public SignatureGenerationException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureValidationException.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureValidationException.java new file mode 100644 index 0000000..4474ab7 --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SignatureValidationException.java @@ -0,0 +1,34 @@ +package com.amazon.spapi.sequencing.crypto; + +/** + * Indicates that the signature/hmac did not verify and that there was a mismatch. + * This indicates either data corruption or a possible tamper attack. + */ +public class SignatureValidationException extends SignatureException { + + /** + * Constructor taking both a message and cause. + * @param message the message + * @param cause the cause + */ + public SignatureValidationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor taking only a message. + * @param message the message + */ + public SignatureValidationException(String message) { + super(message); + } + + /** + * Constructor taking only a cause. + * @param cause the cause + */ + public SignatureValidationException(Throwable cause) { + super(cause); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SymmetricCryptoProvider.java b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SymmetricCryptoProvider.java new file mode 100644 index 0000000..bec05af --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/main/java/com/amazon/spapi/sequencing/crypto/SymmetricCryptoProvider.java @@ -0,0 +1,158 @@ +package com.amazon.spapi.sequencing.crypto; + +import com.google.common.base.Preconditions; +import lombok.extern.apachecommons.CommonsLog; + +import javax.annotation.Nonnull; +import javax.crypto.Mac; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import java.security.InvalidAlgorithmParameterException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.InvalidKeyException; +import java.util.Arrays; +/** + * A Symmetric verison of {@link CryptoProvider} that uses AES and HmacSha256. + * Encryption is performed using AES/CBC/PKCS5Padding + * HMAC is performed using HmacSha256. + */ +@CommonsLog +public class SymmetricCryptoProvider implements CryptoProvider { + private static final String REQUIRED_KEY_ALGORITHM = "AES"; + private static final String ENCRYPTION_ALGORITHM = "AES/CBC/PKCS5Padding"; + private static final String MAC_ALGORITHM = "HmacSha256"; + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] encrypt(@Nonnull byte[] plainText, @Nonnull EncryptionMaterials materials) + throws EncryptionException { + Preconditions.checkArgument(plainText != null, "plainText must be non-null."); + + Cipher cipher = getInitializedCipher(Cipher.ENCRYPT_MODE, materials); + + return applyCipher(cipher, plainText); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] decrypt(@Nonnull byte[] cipherText, @Nonnull EncryptionMaterials materials) + throws EncryptionException { + Preconditions.checkArgument(cipherText != null, "cipherText must be non-null."); + + Cipher cipher = getInitializedCipher(Cipher.DECRYPT_MODE, materials); + + return applyCipher(cipher, cipherText); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public byte[] sign(@Nonnull byte[] bytesToSign, @Nonnull EncryptionMaterials materials) + throws SignatureGenerationException { + Preconditions.checkArgument(bytesToSign != null, "bytesToSign must be non-null.."); + validateMaterials(materials, false); + + Mac mac; + try { + mac = Mac.getInstance(MAC_ALGORITHM); + mac.init(materials.getKey()); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(String.format("NoSuchAlgorithmException for %s", MAC_ALGORITHM), e); + } catch (InvalidKeyException e) { + throw new SignatureGenerationException(e); + } + + return mac.doFinal(bytesToSign); + } + + /** + * {@inheritDoc} + */ + @Override + public void validateSignature(@Nonnull byte[] signature, @Nonnull byte[] bytesToSign, + @Nonnull EncryptionMaterials materials) throws SignatureValidationException, SignatureGenerationException { + Preconditions.checkArgument(signature != null, "signature must be non-null."); + Preconditions.checkArgument(bytesToSign != null, "bytesToSign must be non-null."); + validateMaterials(materials, false); + + byte[] expectedSignature = sign(bytesToSign, materials); + + if (!MessageDigest.isEqual(signature, expectedSignature)) { + if (log.isTraceEnabled()) { + log.trace(String.format("Signature mismatch detected. Expected=%s, Actual=%s", + Arrays.toString(expectedSignature), Arrays.toString(signature))); + } + throw new SignatureValidationException("Signature mismatch. Possible tampering attempt."); + } + } + + /** + * Helper function to apply a cipher to some data and return the results. Performs exception mapping. + * @param cipher the Cipher to apply. + * @param text the text to apply the cipher to + * @return the text generated by applying the cipher to the input text + * @throws EncryptionException if there is a problem applying the cipher. + */ + @Nonnull + private byte[] applyCipher(@Nonnull Cipher cipher, @Nonnull byte[] text) throws EncryptionException { + try { + return cipher.doFinal(text); + } catch (IllegalBlockSizeException e) { + throw new EncryptionException(e); + } catch (BadPaddingException e) { + throw new EncryptionException(e); + } + } + + /** + * {@inheritDoc} + */ + @Nonnull + public Cipher getInitializedCipher(int mode, @Nonnull EncryptionMaterials materials) throws EncryptionException { + validateMaterials(materials, true); + + Cipher cipher; + try { + cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); + cipher.init(mode, materials.getKey(), + new IvParameterSpec(materials.getInitializationVector()), new SecureRandom()); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(String.format("NoSuchAlgorithmException for %s", ENCRYPTION_ALGORITHM), e); + } catch (NoSuchPaddingException e) { + throw new IllegalStateException(String.format("NoSuchPaddingException for %s", ENCRYPTION_ALGORITHM), e); + } catch (InvalidKeyException e) { + throw new EncryptionException(e); + } catch (InvalidAlgorithmParameterException e) { + throw new EncryptionException(e); + } + + return cipher; + } + + /** + * Helper function to ensure EncryptionMaterials are valid. + * @param materials the Materials to validate + * @param ivRequired whether or not this function should treat the InitializationVector as required. + * The IV is required for encryption/decryption but not for HMAC operations. + */ + private void validateMaterials(@Nonnull EncryptionMaterials materials, boolean ivRequired) { + Preconditions.checkArgument(materials != null, "Materials must be non-null."); + Preconditions.checkArgument(REQUIRED_KEY_ALGORITHM.equals(materials.getKey().getAlgorithm()), + "SymmetricCryptoProvider requires an AES Key."); + Preconditions.checkArgument(!ivRequired || materials.getInitializationVector() != null, "IV must be non-null."); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/main/resources/certs/InternalAndExternalTrustStore.jks b/clients/sellingpartner-api-frp-helper-java/src/main/resources/certs/InternalAndExternalTrustStore.jks new file mode 100644 index 0000000..f2d9548 Binary files /dev/null and b/clients/sellingpartner-api-frp-helper-java/src/main/resources/certs/InternalAndExternalTrustStore.jks differ diff --git a/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/CreateDestinationAndUploadTest.java b/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/CreateDestinationAndUploadTest.java new file mode 100644 index 0000000..b4ebc4a --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/CreateDestinationAndUploadTest.java @@ -0,0 +1,107 @@ +package com.amazon.spapi; + +import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials; +import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials; +import com.amazon.spapi.sequencing.client.UploadDetails; +import com.amazon.spapi.sequencing.client.UploadsSequencer; +import com.amazon.spapi.sequencing.client.impl.UploadsSequencerImpl; +import io.swagger.client.ApiException; +import io.swagger.client.api.UploadsApi; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URL; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * API tests for UploadsApi + */ +public class CreateDestinationAndUploadTest { + + private final String feedType = "_POST_PRODUCT_DATA_"; + + private final UploadsSequencer sequencer; + + { + sequencer = testSequencer(); + + // Fill out the API sequencer with your credentials here. + } + + static { + System.setProperty("javax.net.ssl.trustStore", + getResourceFile("certs/InternalAndExternalTrustStore.jks").getPath() + ); + System.setProperty("javax.net.ssl.trustStorePassword", "amazon"); + } + + private UploadsSequencer createSequencer(String accessKeyId, String secretKey, String clientId, String clientSecret, + String refreshToken) { + AWSAuthenticationCredentials + awsAuthenticationCredentials = AWSAuthenticationCredentials.builder() + .accessKeyId(accessKeyId) + .secretKey(secretKey) + .region("us-west-2") + .build(); + + + LWAAuthorizationCredentials lwaAuthorizationCredentials = + LWAAuthorizationCredentials.builder() + .clientId(clientId) + .clientSecret(clientSecret) + .refreshToken( + refreshToken) + .endpoint("https://api.integ.amazon.com/auth/O2/token") + .build(); + UploadsApi api = new UploadsApi.Builder().awsAuthenticationCredentials(awsAuthenticationCredentials) + .lwaAuthorizationCredentials(lwaAuthorizationCredentials) + .endpoint("https://marketplaceapi-beta.amazonservices.com") + .build(); + + return UploadsSequencerImpl.builder() + .uploadsApi(api) + .build(); + } + + private static File getResourceFile(String fileName) { + final URL fileUrl = UploadsApi.class.getClassLoader().getResource(fileName); + assertNotNull(fileName + " cannot be reached", fileUrl); + final File file = new File(fileUrl.getFile()); + assertTrue(file.exists()); + return file; + } + + @Test + @Ignore + public void createDestinationThenUpload() throws FileNotFoundException, ApiException { + //file must be in "test/resources" + final File flatFileInventoryLoader = getResourceFile("Feed_101__POST_PRODUCT_DATA_.xml"); + final UploadDetails details = sequencer.createDestinationAndUpload(feedType, + "text/xml", + flatFileInventoryLoader + ); + System.out.println("SHA256Sum = " + details.getSha256Sum() + " // UploadDestinationId = " + details.getUploadDestinationId()); + assertNotNull(details); + assertNotNull(details.getUploadDestinationId()); + } + + @Test + @Ignore + public void makeItPass() { + assertTrue(true); + } + + private UploadsSequencer testSequencer() { + return createSequencer("", + "", + "", + "", + "" + ); + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/ReportDownloadTest.java b/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/ReportDownloadTest.java new file mode 100644 index 0000000..0711a3a --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/test/java/com/amazon/spapi/ReportDownloadTest.java @@ -0,0 +1,70 @@ +package com.amazon.spapi; + +import com.amazon.spapi.sequencing.client.DownloadsSequencer; +import com.amazon.spapi.sequencing.client.impl.DownloadsSequencerImpl; +import io.swagger.client.model.EncryptionDetails; +import io.swagger.client.model.UploadDestination; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class ReportDownloadTest { + + private final DownloadsSequencer sequencer; + + private UploadDestination destinationToDownload; + + { + sequencer = DownloadsSequencerImpl.builder().build(); + // Fill out the API sequencer with your credentials here. + } + + + static { + System.setProperty("javax.net.ssl.trustStore", + getResourceFile("certs/InternalAndExternalTrustStore.jks").getPath() + ); + System.setProperty("javax.net.ssl.trustStorePassword", "amazon"); + } + + + @Before + public void before() { + // TODO: fill this out based on payload + EncryptionDetails encryptionDetails = new EncryptionDetails().standard(EncryptionDetails.StandardEnum.AES) + .initializationVector("") + .key(""); + destinationToDownload = new UploadDestination().encryptionDetails(encryptionDetails) + .url(""); + } + + @Test + // TODO: remove @Ignore when ready to test + @Ignore + public void downloadAndOutputDecryptedReport() throws IOException { + File fileToOutputTo = new File("DecryptedReport.xml"); + sequencer.downloadDecryptThenWriteFile(destinationToDownload, false, fileToOutputTo); + } + + @Test + public void makeItPass() { + assertTrue(true); + } + + + private static File getResourceFile(String fileName) { + final URL fileUrl = DownloadsSequencerImpl.class.getClassLoader().getResource(fileName); + assertNotNull(fileName + " cannot be reached", fileUrl); + final File file = new File(fileUrl.getFile()); + assertTrue(file.exists()); + return file; + } + +} diff --git a/clients/sellingpartner-api-frp-helper-java/src/test/resources/Feed_101__POST_PRODUCT_DATA_.xml b/clients/sellingpartner-api-frp-helper-java/src/test/resources/Feed_101__POST_PRODUCT_DATA_.xml new file mode 100644 index 0000000..588fa5f --- /dev/null +++ b/clients/sellingpartner-api-frp-helper-java/src/test/resources/Feed_101__POST_PRODUCT_DATA_.xml @@ -0,0 +1 @@ + \ No newline at end of file