Skip to content

Commit

Permalink
Merge pull request #1026 from clement911/master
Browse files Browse the repository at this point in the history
RequestInfo should not include the access token in case it gets logge…
  • Loading branch information
clement911 committed Feb 22, 2024
2 parents d8568fb + 586a7c9 commit cd8e9ec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions ShopifySharp.Tests/ShopifyException_Tests.cs
Expand Up @@ -30,7 +30,7 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, HttpCo
Content = content
};

msg.Headers.Add("X-Shopify-Access-Token", Utils.AccessToken);
msg.Headers.Add(ShopifyService.REQUEST_HEADER_ACCESS_TOKEN, Utils.AccessToken);

return msg;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task Throws_On_Error_String()

try
{
ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody);
ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody);
}
catch (ShopifyRateLimitException)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task Throws_On_Error_Object()

try
{
ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody);
ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody);
}
catch (ShopifyRateLimitException)
{
Expand Down Expand Up @@ -205,7 +205,7 @@ public async Task Throws_On_Error_Arrays()

try
{
ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody);
ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody);
}
catch (ShopifyRateLimitException)
{
Expand Down
19 changes: 17 additions & 2 deletions ShopifySharp/Infrastructure/CloneableRequestMessage.cs
@@ -1,5 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

Expand Down Expand Up @@ -66,7 +66,7 @@ private static async Task<HttpContent> CloneToStreamOrReadOnlyMemoryContent(Http
var rs = new ReadOnlyMemory<byte>(await originalStreamContent.ReadAsByteArrayAsync());
clonedContent = new ReadOnlyMemoryContent(rs);
#else
var ms = new MemoryStream();
var ms = new System.IO.MemoryStream();
await originalStreamContent.CopyToAsync(ms);
ms.Position = 0;
clonedContent = new StreamContent(ms);
Expand All @@ -79,5 +79,20 @@ private static async Task<HttpContent> CloneToStreamOrReadOnlyMemoryContent(Http

return clonedContent;
}

public string GetRequestInfo()
{
var headers = this.Headers.Where(kv => kv.Value != null && kv.Key != ShopifyService.REQUEST_HEADER_ACCESS_TOKEN)
.Select(kv => $"\t{kv.Key}: {string.Join(", ", kv.Value)}");
return $"""
Method: {this.Method}
RequestUri: {this.RequestUri}
Content: {this.Content?.GetType().Name}
Headers:
[
{string.Join(Environment.NewLine, headers)}
]
""";
}
}
}
Expand Up @@ -17,8 +17,6 @@ namespace ShopifySharp
/// </summary>
public class LeakyBucketExecutionPolicy : IRequestExecutionPolicy
{
private const string REQUEST_HEADER_ACCESS_TOKEN = "X-Shopify-Access-Token";

private static ConcurrentDictionary<string, MultiShopifyAPIBucket> _shopAccessTokenToLeakyBucket = new ConcurrentDictionary<string, MultiShopifyAPIBucket>();

private readonly Func<RequestContext> _getRequestContext;
Expand Down Expand Up @@ -269,7 +267,7 @@ CloneableRequestMessage baseRequest

private string GetAccessToken(HttpRequestMessage client)
{
return client.Headers.TryGetValues(REQUEST_HEADER_ACCESS_TOKEN, out var values) ?
return client.Headers.TryGetValues(ShopifyService.REQUEST_HEADER_ACCESS_TOKEN, out var values) ?
values.FirstOrDefault() :
null;
}
Expand Down
10 changes: 6 additions & 4 deletions ShopifySharp/Services/ShopifyService.cs
Expand Up @@ -18,7 +18,9 @@ namespace ShopifySharp
{
public abstract class ShopifyService : IShopifyService
{
#nullable enable
public const string REQUEST_HEADER_ACCESS_TOKEN = "X-Shopify-Access-Token";

#nullable enable

public virtual string APIVersion => "2024-01";
public virtual bool SupportsAPIVersioning => true;
Expand Down Expand Up @@ -159,7 +161,7 @@ protected CloneableRequestMessage PrepareRequestMessage(RequestUri uri, HttpMeth

if (!string.IsNullOrEmpty(_AccessToken))
{
msg.Headers.Add("X-Shopify-Access-Token", _AccessToken);
msg.Headers.Add(REQUEST_HEADER_ACCESS_TOKEN, _AccessToken);
}

msg.Headers.Add("Accept", "application/json");
Expand Down Expand Up @@ -213,11 +215,11 @@ private string ReadLinkHeader(HttpResponseHeaders responseHeaders)
#endif
//Check for and throw exception when necessary.
CheckResponseExceptions(baseRequestMessage.ToString(), response, rawResult);
CheckResponseExceptions(baseRequestMessage.GetRequestInfo(), response, rawResult);
var result = method == HttpMethod.Delete ? default : Serializer.Deserialize<T>(rawResult, rootElement, dateParseHandlingOverride);
return new RequestResult<T>(baseRequestMessage.ToString(), response, response.Headers, result, rawResult, ReadLinkHeader(response.Headers));
return new RequestResult<T>(baseRequestMessage.GetRequestInfo(), response, response.Headers, result, rawResult, ReadLinkHeader(response.Headers));
}, cancellationToken, graphqlQueryCost);

return policyResult;
Expand Down
4 changes: 2 additions & 2 deletions ShopifySharp/Utilities/ShopifyOauthUtility.cs
Expand Up @@ -242,7 +242,7 @@ string clientSecret
using var response = await client.SendAsync(request);
var rawDataString = await response.Content.ReadAsStringAsync();

ShopifyService.CheckResponseExceptions(request.ToString(), response, rawDataString);
ShopifyService.CheckResponseExceptions(request.GetRequestInfo(), response, rawDataString);

var json = JToken.Parse(rawDataString);
return new AuthorizationResult(json.Value<string>("access_token"), json.Value<string>("scope")?.Split(','));

Check warning on line 248 in ShopifySharp/Utilities/ShopifyOauthUtility.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference argument for parameter 'accessToken' in 'AuthorizationResult.AuthorizationResult(string accessToken, string[]? grantedScopes)'.
Expand Down Expand Up @@ -285,7 +285,7 @@ string existingStoreAccessToken
using var response = await client.SendAsync(request);
var rawDataString = await response.Content.ReadAsStringAsync();

ShopifyService.CheckResponseExceptions(request.ToString(), response, rawDataString);
ShopifyService.CheckResponseExceptions(request.GetRequestInfo(), response, rawDataString);

var json = JToken.Parse(rawDataString);
// TODO: throw a ShopifyJsonParseException if value is null. Exception should have a RawBody property.
Expand Down

0 comments on commit cd8e9ec

Please sign in to comment.