forked from bcgov/supreme-court-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from bcgov/bugfix/sso-issue
Fix for SSO Login Issue
- Loading branch information
Showing
5 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
namespace Scv.Api.Helpers | ||
using System; | ||
|
||
namespace Scv.Api.Helpers | ||
{ | ||
public static class XForwardedForHelper | ||
{ | ||
public static string BuildUrlString(string forwardedHost, string forwardedPort, string baseUrl) | ||
public static string BuildUrlString(string forwardedHost, string forwardedPort, string baseUrl, string remainingPath = "", string query = "") | ||
{ | ||
var portComponent = string.IsNullOrEmpty(forwardedPort) || forwardedPort == "80" || forwardedPort == "443" ? "" : $":{forwardedPort}"; | ||
return $"https://{forwardedHost}{portComponent}{baseUrl}"; | ||
var sanitizedPath = baseUrl; | ||
if (!string.IsNullOrEmpty(remainingPath)) | ||
{ | ||
sanitizedPath = string.Format("{0}/{1}", baseUrl.TrimEnd('/'), remainingPath.TrimStart('/')); | ||
} | ||
|
||
var uriBuilder = new UriBuilder | ||
{ | ||
Scheme = "https", | ||
Host = forwardedHost, | ||
Path = sanitizedPath, | ||
Query = query | ||
}; | ||
|
||
var portComponent = | ||
string.IsNullOrEmpty(forwardedPort) || forwardedPort == "80" || forwardedPort == "443" | ||
? "" | ||
: $":{forwardedPort}"; | ||
|
||
if (!string.IsNullOrEmpty(portComponent)) | ||
{ | ||
int port; | ||
int.TryParse(forwardedPort, out port); | ||
uriBuilder.Port = port; | ||
} | ||
|
||
return uriBuilder.Uri.AbsoluteUri; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System.Net; | ||
using System.Collections.Generic; | ||
using System; | ||
using Bogus; | ||
using Scv.Api.Helpers; | ||
using Xunit; | ||
|
||
namespace tests.api.Helpers | ||
{ | ||
public class XForwardedForHelperTests | ||
{ | ||
[Fact] | ||
public void BuildUrlString_ShouldRemoveDoubleSlashesInUrlPath() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var path2 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 8080; | ||
var expected = $"https://{host}:{port}/{path1}/{path2}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", $"{path1}///", $"//{path2}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldRemoveDoubleSlashesInUrlPathWithManyForwardSlash() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var path2 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 80; | ||
var expected = $"https://{host}/{path1}/{path2}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", $"{path1}//////////////", $"///////////////{path2}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldExcludeWhenPortIs443() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var path2 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 443; | ||
var expected = $"https://{host}/{path1}/{path2}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", path1, path2); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldExcludeWhenPortIs80() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var path2 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 80; | ||
var expected = $"https://{host}/{path1}/{path2}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", path1, path2); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldReturnCorrectURLWhenNoOtherUrlPath() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 80; | ||
var expected = $"https://{host}/{path1}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", path1); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldReturnCorrectURLWithQueryParams() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var path1 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var path2 = WebUtility.UrlEncode(faker.Random.Word()); | ||
var port = 80; | ||
string param1 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Random.Number(1, 100)}"); | ||
string param2 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Internet.UserName()}"); | ||
string param3 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Random.Bool()}"); | ||
|
||
var expected = $"https://{host}/{path1}/{path2}?{param1}&{param2}&{param3}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", path1, path2, $"{param1}&{param2}&{param3}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void BuildUrlString_ShouldReturnCorrectURLRandomPaths() | ||
{ | ||
var faker = new Faker(); | ||
var host = faker.Internet.DomainName(); | ||
var basePath = WebUtility.UrlEncode(faker.Random.Word()); | ||
var remainingPathCount = faker.Random.Number(1, 5); | ||
var paths = new List<string>(); | ||
var port = 80; | ||
|
||
string param1 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Random.Number(1, 100)}"); | ||
string param2 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Internet.UserName()}"); | ||
string param3 = WebUtility.UrlEncode($"{faker.Lorem.Word()}={faker.Random.Bool()}"); | ||
|
||
for (int i = 0; i < remainingPathCount; i++) | ||
{ | ||
paths.Add(WebUtility.UrlEncode(faker.Random.Word())); | ||
} | ||
|
||
var expected = $"https://{host}/{basePath}/{string.Join("/", paths)}?{param1}&{param2}&{param3}"; | ||
|
||
var result = XForwardedForHelper.BuildUrlString(host, $"{port}", basePath, string.Join("/", paths), $"{param1}&{param2}&{param3}"); | ||
|
||
Console.WriteLine(expected); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters