Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes OAuth1 signature with special characters (#2126, #1945) #2127

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/RestSharp/Authenticators/OAuth/OAuthTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static string GetNonce() {
internal static IEnumerable<string> SortParametersExcludingSignature(WebPairCollection parameters)
=> parameters
.Where(x => !x.Name.EqualsIgnoreCase("oauth_signature"))
.Select(x => new WebPair(UrlEncodeStrict(x.Name), UrlEncodeStrict(x.Value)))
.Select(x => new WebPair(UrlEncodeStrict(x.Name), UrlEncodeRelaxed(x.Value)))
.OrderBy(x => x, WebPair.Comparer)
.Select(x => x.GetQueryParameter(false));

Expand Down
26 changes: 19 additions & 7 deletions test/RestSharp.Tests.Integrated/OAuth1Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,29 @@ public async Task Can_Authenticate_OAuth1_With_Querystring_Parameters() {
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void Properly_Encodes_Parameter_Names() {
var postData = new WebPairCollection {
{ "name[first]", "Chuck" },
{ "name[last]", "Testa" }
};
[Theory]
[MemberData(nameof(EncodeParametersTestData))]
public void Properly_Encodes_Parameter_Names(IList<(string, string)> parameters, string expected) {
var postData = new WebPairCollection();
postData.AddRange(parameters.Select(x => new WebPair(x.Item1, x.Item2)));
var sortedParams = OAuthTools.SortParametersExcludingSignature(postData);

sortedParams.First().Should().Be("name%5Bfirst%5D=Chuck");
sortedParams.First().Should().Be(expected);
}

public static IEnumerable<object[]> EncodeParametersTestData =>
new List<object[]>
{
new object[] {
new List<(string, string)> { ("name[first]", "Chuck"), ("name[last]", "Testa") },
"name%5Bfirst%5D=Chuck"
},
new object[] {
new List<(string, string)> { ("country", "España") },
"country=Espa%C3%B1a"
}
};

[Fact]
public void Use_RFC_3986_Encoding_For_Auth_Signature_Base() {
// reserved characters for 2396 and 3986
Expand Down