From abf3812dcd044d3ca46ca264f952bfa543d961b1 Mon Sep 17 00:00:00 2001 From: Albie Date: Mon, 3 Aug 2020 15:05:42 +0100 Subject: [PATCH 1/3] bump data framework --- DragonFruit.Link/DragonFruit.Link.csproj | 2 +- .../Requests/SteamUserTradeCancelRequest.cs | 4 +- .../Requests/SteamUserTradeDeclineRequest.cs | 4 +- .../SteamGameServerAccountCreationRequest.cs | 4 +- .../SteamGameServerDeletionRequest.cs | 4 +- .../SteamGameServerLoginResetRequest.cs | 4 +- .../SteamGameServerMemoChangeRequest.cs | 4 +- DragonFruit.Link/SteamApiClient.cs | 47 ++++++++----------- DragonFruit.Link/SteamApiRequest.cs | 27 ++++------- 9 files changed, 41 insertions(+), 59 deletions(-) diff --git a/DragonFruit.Link/DragonFruit.Link.csproj b/DragonFruit.Link/DragonFruit.Link.csproj index 97873df..768645b 100644 --- a/DragonFruit.Link/DragonFruit.Link.csproj +++ b/DragonFruit.Link/DragonFruit.Link.csproj @@ -17,7 +17,7 @@ - + diff --git a/DragonFruit.Link/Economy/Requests/SteamUserTradeCancelRequest.cs b/DragonFruit.Link/Economy/Requests/SteamUserTradeCancelRequest.cs index ecd9e7c..af3eeff 100644 --- a/DragonFruit.Link/Economy/Requests/SteamUserTradeCancelRequest.cs +++ b/DragonFruit.Link/Economy/Requests/SteamUserTradeCancelRequest.cs @@ -8,8 +8,8 @@ namespace DragonFruit.Link.Economy.Requests { public class SteamUserTradeCancelRequest : SteamApiRequest { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IEconService"; public override string InterfaceMethod => "CancelTradeOffer"; diff --git a/DragonFruit.Link/Economy/Requests/SteamUserTradeDeclineRequest.cs b/DragonFruit.Link/Economy/Requests/SteamUserTradeDeclineRequest.cs index 416908e..4eca58f 100644 --- a/DragonFruit.Link/Economy/Requests/SteamUserTradeDeclineRequest.cs +++ b/DragonFruit.Link/Economy/Requests/SteamUserTradeDeclineRequest.cs @@ -8,8 +8,8 @@ namespace DragonFruit.Link.Economy.Requests { public class SteamUserTradeDeclineRequest : SteamApiRequest { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IEconService"; public override string InterfaceMethod => "DeclineTradeOffer"; diff --git a/DragonFruit.Link/Servers/Requests/SteamGameServerAccountCreationRequest.cs b/DragonFruit.Link/Servers/Requests/SteamGameServerAccountCreationRequest.cs index 3cd403f..1231c3f 100644 --- a/DragonFruit.Link/Servers/Requests/SteamGameServerAccountCreationRequest.cs +++ b/DragonFruit.Link/Servers/Requests/SteamGameServerAccountCreationRequest.cs @@ -8,8 +8,8 @@ namespace DragonFruit.Link.Servers.Requests { public class SteamGameServerAccountCreationRequest : SteamApiRequest, IForSteamApp { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IGameServersService"; public override string InterfaceMethod => "CreateAccount"; diff --git a/DragonFruit.Link/Servers/Requests/SteamGameServerDeletionRequest.cs b/DragonFruit.Link/Servers/Requests/SteamGameServerDeletionRequest.cs index 1a12df5..526950a 100644 --- a/DragonFruit.Link/Servers/Requests/SteamGameServerDeletionRequest.cs +++ b/DragonFruit.Link/Servers/Requests/SteamGameServerDeletionRequest.cs @@ -8,8 +8,8 @@ namespace DragonFruit.Link.Servers.Requests { public class SteamGameServerDeletionRequest : SteamApiRequest, IForSteamUser { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IGameServersService"; public override string InterfaceMethod => "DeleteAccount"; diff --git a/DragonFruit.Link/Servers/Requests/SteamGameServerLoginResetRequest.cs b/DragonFruit.Link/Servers/Requests/SteamGameServerLoginResetRequest.cs index 9093973..75308a0 100644 --- a/DragonFruit.Link/Servers/Requests/SteamGameServerLoginResetRequest.cs +++ b/DragonFruit.Link/Servers/Requests/SteamGameServerLoginResetRequest.cs @@ -8,8 +8,8 @@ namespace DragonFruit.Link.Servers.Requests { public class SteamGameServerLoginResetRequest : SteamApiRequest, IForSteamUser { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IGameServersService"; public override string InterfaceMethod => "ResetLoginToken"; diff --git a/DragonFruit.Link/Servers/Requests/SteamGameServerMemoChangeRequest.cs b/DragonFruit.Link/Servers/Requests/SteamGameServerMemoChangeRequest.cs index 30a946b..6a71823 100644 --- a/DragonFruit.Link/Servers/Requests/SteamGameServerMemoChangeRequest.cs +++ b/DragonFruit.Link/Servers/Requests/SteamGameServerMemoChangeRequest.cs @@ -9,8 +9,8 @@ namespace DragonFruit.Link.Servers.Requests { public class SteamGameServerMemoChangeRequest : SteamApiRequest, IForSteamUser { - public override Methods Method => Methods.Post; - public override DataTypes DataType => DataTypes.Encoded; + protected override Methods Method => Methods.Post; + protected override BodyType BodyType => BodyType.Encoded; public override string Interface => "IGameServersService"; public override string InterfaceMethod => "SetMemo"; diff --git a/DragonFruit.Link/SteamApiClient.cs b/DragonFruit.Link/SteamApiClient.cs index 57f326c..443c1ea 100644 --- a/DragonFruit.Link/SteamApiClient.cs +++ b/DragonFruit.Link/SteamApiClient.cs @@ -1,7 +1,7 @@ // DragonFruit Link API Copyright 2020 (C) DragonFruit Network // Licensed under the GNU GPLv3 License. Refer to the license.md file at the root of the repo for more info -using System.Net.Http; +using System.Globalization; using DragonFruit.Common.Data; using DragonFruit.Link.Exceptions; @@ -14,51 +14,44 @@ public class SteamApiClient : ApiClient #region Constructors - public SteamApiClient(string apiKey, string userAgent) - : this(apiKey) + public SteamApiClient() + : base(CultureInfo.InvariantCulture) // creates a new ApiJsonSerializer { - UserAgent = userAgent; } public SteamApiClient(string apiKey) + : this() { _apiKey = apiKey; _apiKeySet = true; } - public SteamApiClient() + public SteamApiClient(string apiKey, string userAgent) + : this(apiKey) { - // this should only be for non-auth requiring things - _apiKeySet = false; + UserAgent = userAgent; } #endregion - public T Perform(SteamApiRequest request) where T : class/*, ISteamApiResponse*/ + protected override void ValidateRequest(ApiRequest requestData) { - CheckAuthorization(request); - return base.Perform(request); - } - - public HttpResponseMessage Perform(SteamApiRequest request) - { - CheckAuthorization(request); - return base.Perform(request); - } - - private void CheckAuthorization(SteamApiRequest request) - { - if (!request.RequireApiKey) + if (requestData is SteamApiRequest steamRequest) { - return; - } + if (!steamRequest.RequireApiKey) + { + return; + } - if (!_apiKeySet) - { - throw new SteamApiKeyMissingException(); + if (!_apiKeySet) + { + throw new SteamApiKeyMissingException(); + } + + steamRequest.ApiKey = _apiKey; } - request.ApiKey = _apiKey; + base.ValidateRequest(requestData); } } } diff --git a/DragonFruit.Link/SteamApiRequest.cs b/DragonFruit.Link/SteamApiRequest.cs index c5134a4..495035f 100644 --- a/DragonFruit.Link/SteamApiRequest.cs +++ b/DragonFruit.Link/SteamApiRequest.cs @@ -9,43 +9,32 @@ namespace DragonFruit.Link { + [JsonObject(MemberSerialization.OptIn)] public abstract class SteamApiRequest : ApiRequest { - #region Overrides - - public override Methods Method => Methods.Get; - - public override string AcceptedContent => "application/json"; - - #endregion - - #region Authentication - - //we use the RequireApiKey instead, as keys need to be sent as a query - public override bool RequireAuth => false; + protected override Methods Method => Methods.Get; + protected override bool RequireAuth => false; //this only checks headers, ours is in the query //replaces RequireAuth, see above public abstract bool RequireApiKey { get; } - [QueryParameter("key"), JsonIgnore] - public string ApiKey { get; set; } - - #endregion + #region Path - #region Path Constructors - public virtual string? PathOverride { get; } public override string Path => PathOverride ?? $"https://api.steampowered.com/{Interface}/{InterfaceMethod}/v{MethodVersion}/"; public abstract int MethodVersion { get; } - + public abstract string Interface { get; } public abstract string InterfaceMethod { get; } #endregion + [QueryParameter("key")] + public string ApiKey { get; set; } = null!; + [QueryParameter("format")] protected string OutputFormat => "json"; } From daa3d5d09ccb4cdd495e86798becd97fb58853bc Mon Sep 17 00:00:00 2001 From: Albie Date: Mon, 3 Aug 2020 15:08:53 +0100 Subject: [PATCH 2/3] validate before injection --- DragonFruit.Link/SteamApiClient.cs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/DragonFruit.Link/SteamApiClient.cs b/DragonFruit.Link/SteamApiClient.cs index 443c1ea..1305841 100644 --- a/DragonFruit.Link/SteamApiClient.cs +++ b/DragonFruit.Link/SteamApiClient.cs @@ -36,22 +36,24 @@ public SteamApiClient(string apiKey, string userAgent) protected override void ValidateRequest(ApiRequest requestData) { - if (requestData is SteamApiRequest steamRequest) + base.ValidateRequest(requestData); + + if (!(requestData is SteamApiRequest steamRequest)) { - if (!steamRequest.RequireApiKey) - { - return; - } + return; + } - if (!_apiKeySet) - { - throw new SteamApiKeyMissingException(); - } + if (!steamRequest.RequireApiKey) + { + return; + } - steamRequest.ApiKey = _apiKey; + if (!_apiKeySet) + { + throw new SteamApiKeyMissingException(); } - base.ValidateRequest(requestData); + steamRequest.ApiKey = _apiKey; } } } From a5f617d4eeecd740f80fff5e19810c8c6066c0a8 Mon Sep 17 00:00:00 2001 From: Albie Date: Fri, 7 Aug 2020 13:47:08 +0100 Subject: [PATCH 3/3] update everything --- DragonFruit.Link.Tests/DragonFruit.Link.Tests.csproj | 8 ++++---- DragonFruit.Link/DragonFruit.Link.csproj | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/DragonFruit.Link.Tests/DragonFruit.Link.Tests.csproj b/DragonFruit.Link.Tests/DragonFruit.Link.Tests.csproj index b54c876..046e4a5 100644 --- a/DragonFruit.Link.Tests/DragonFruit.Link.Tests.csproj +++ b/DragonFruit.Link.Tests/DragonFruit.Link.Tests.csproj @@ -7,10 +7,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/DragonFruit.Link/DragonFruit.Link.csproj b/DragonFruit.Link/DragonFruit.Link.csproj index 768645b..98980f5 100644 --- a/DragonFruit.Link/DragonFruit.Link.csproj +++ b/DragonFruit.Link/DragonFruit.Link.csproj @@ -17,7 +17,7 @@ - +