-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8471ac
commit 8bbc19f
Showing
2 changed files
with
45 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
namespace Fido2NetLib; | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using System.Collections.Generic; | ||
|
||
public static class StringExtensions | ||
namespace Fido2NetLib | ||
{ | ||
public static string ToFullyQualifiedOrigin(this string origin) | ||
public static class StringExtensions | ||
{ | ||
var uri = new Uri(origin); | ||
public static string ToFullyQualifiedOrigin(this string origin) | ||
{ | ||
if (IsWildCardUrl(origin)) | ||
{ | ||
var uri = new Uri(origin.Remove(origin.IndexOf("*."), 2)); | ||
if (UriHostNameType.Unknown != uri.HostNameType) | ||
return uri.IsDefaultPort ? $"{uri.Scheme}://*.{uri.Host}" : $"{uri.Scheme}://*.{uri.Host}:{uri.Port}"; | ||
} | ||
else | ||
{ | ||
var uri = new Uri(origin); | ||
if (UriHostNameType.Unknown != uri.HostNameType) | ||
return uri.IsDefaultPort ? $"{uri.Scheme}://{uri.Host}" : $"{uri.Scheme}://{uri.Host}:{uri.Port}"; | ||
} | ||
|
||
if (UriHostNameType.Unknown != uri.HostNameType) | ||
return uri.IsDefaultPort ? $"{uri.Scheme}://{uri.Host}" : $"{uri.Scheme}://{uri.Host}:{uri.Port}"; | ||
return origin; | ||
} | ||
|
||
return origin; | ||
public static bool ContainsUrl(this IReadOnlySet<string> fullyQualifiedExpectedOrigins, string fullyQualifiedOrigin) | ||
{ | ||
foreach (var fullyQualifiedExpectedOrigin in fullyQualifiedExpectedOrigins) | ||
{ | ||
if ((IsWildCardUrl(fullyQualifiedExpectedOrigin) && IsMatch(fullyQualifiedExpectedOrigin, fullyQualifiedOrigin)) || (fullyQualifiedExpectedOrigin.Equals(fullyQualifiedOrigin, StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static bool IsWildCardUrl(string origin) | ||
{ | ||
string pattern = @"^[a-zA-Z][a-zA-Z0-9+\-.]*:\/\/\*\..*$"; | ||
return Regex.IsMatch(origin, pattern); | ||
} | ||
|
||
private static bool IsMatch(string wildcardUrl, string testUrl) | ||
{ | ||
var pattern = "^" + Regex.Escape(wildcardUrl).Replace("\\*", "[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?") + "$"; | ||
return Regex.IsMatch(testUrl, pattern); | ||
} | ||
} | ||
} |
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