Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit d05125f

Browse files
authored
Merge pull request #240 from stormpath/235-fix-sauthc1-parens
Fix URL encoding for OAuth requests
2 parents e2834e4 + 4af80a4 commit d05125f

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

src/Stormpath.SDK.Abstractions/Http/UrlEncoding.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ public static string Encode(string value, bool isPath = false, bool canonicalize
3939

4040
var encoded = WebUtility.UrlEncode(value);
4141

42-
// WebUtility doesn't escape ! by default
43-
encoded = encoded.Replace("!", "%21");
42+
// WebUtility doesn't escape some characters by default
43+
encoded = encoded
44+
.Replace("!", "%21")
45+
.Replace("(", "%28")
46+
.Replace(")", "%29");
4447

4548
// Perform some custom Stormpath encoding
4649
if (canonicalize)
4750
{
4851
encoded = encoded
4952
.Replace("+", "%20") // Spaces as %20
5053
.Replace("*", "%2A")
51-
.Replace("%7E", "~") // Tildes stay as they are
52-
.Replace("(", "%28")
53-
.Replace(")", "%29");
54+
.Replace("%7E", "~"); // Tildes stay unencoded
5455

5556
if (isPath)
5657
{

test/Stormpath.SDK.Tests.Integration/Async/Oauth_tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public async Task Password_grant_with_special_characters(TestClientProvider clie
151151

152152
// Add the test accounts
153153
var randomEmail = new RandomEmail("testmail.stormpath.com");
154-
var password = "P@sword#123$!";
154+
var password = "P@ss* word#123$!()~";
155155
await createdApplication.CreateAccountAsync("Test", "testerman", randomEmail, password);
156156

157157
var passwordGrantRequest = OauthRequests.NewPasswordGrantRequest()

test/Stormpath.SDK.Tests.Integration/Sync/Oauth_tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void Password_grant_with_special_characters(TestClientProvider clientBuil
147147

148148
// Add the test accounts
149149
var randomEmail = new RandomEmail("testmail.stormpath.com");
150-
var password = "P@sword#123$!";
150+
var password = "P@ss* word#123$!()~";
151151
createdApplication.CreateAccount("Test", "testerman", randomEmail, password);
152152

153153
var passwordGrantRequest = OauthRequests.NewPasswordGrantRequest()

test/Stormpath.SDK.Tests/FormUrlEncoder_tests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,27 @@ public void Encodes_password_grant_attempt()
4646
result.ShouldContain("password=Secret1");
4747
result.ShouldContain("accountStore=https%3A%2F%2Fapi.stormpath.com%2Fv1%2Fdirectories%2F1bcd23ec1d0aEXAMPLE");
4848
}
49+
50+
/// <summary>
51+
/// Regression test for https://github.com/stormpath/stormpath-sdk-dotnet/issues/235
52+
/// </summary>
53+
[Fact]
54+
public void Encodes_parenthesis_correctly()
55+
{
56+
var dataStore = TestDataStore.Create();
57+
58+
var createGrantAttempt = dataStore.Instantiate<IPasswordGrantAuthenticationAttempt>();
59+
createGrantAttempt.SetLogin("[email protected]");
60+
createGrantAttempt.SetPassword("Testing123()");
61+
createGrantAttempt.SetAccountStore("https://api.stormpath.com/v1/directories/1bcd23ec1d0aEXAMPLE");
62+
63+
var properties = (createGrantAttempt as AbstractResource).GetResourceData().GetUpdatedProperties().ToDictionary();
64+
var result = new FormUrlEncoder(properties)
65+
.ToString()
66+
.Split('&');
67+
68+
result.ShouldContain("username=nate%40stormpath.com");
69+
result.ShouldContain("password=Testing123%28%29");
70+
}
4971
}
5072
}

test/Stormpath.SDK.Tests/UrlEncoding_tests.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,10 @@ public void Canononicalizes_path_correctly()
6060
canonicalizedWithPath.ShouldBe("/");
6161
}
6262

63-
[Fact]
64-
public void Does_not_escape_parenthesis()
65-
{
66-
var escaped = UrlEncoding.Encode("()");
67-
68-
escaped.ShouldBe("()");
69-
}
70-
7163
[Fact]
7264
public void Canonicalizes_parenthesis()
7365
{
74-
var canonicalized = UrlEncoding.Encode("()", canonicalize: true);
66+
var canonicalized = UrlEncoding.Encode("()");
7567

7668
canonicalized.ShouldBe("%28%29");
7769
}

0 commit comments

Comments
 (0)