Improve SGU logon resilience and client UX
This commit is contained in:
1 parent
3d0897316d
commit
da01343985
27 files changed
+869
-37
No files matched your search
@@ -0,0 +1,105 @@
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SGU.AuthBroker.Core.Profiles;
|
||||
|
||||
public static partial class SguHtmlDecoder
|
||||
{
|
||||
private const int MetaScanBytes = 8 * 1024;
|
||||
|
||||
static SguHtmlDecoder()
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
}
|
||||
|
||||
public static string Decode(ReadOnlySpan<byte> bytes, string? declaredCharset = null)
|
||||
{
|
||||
if (bytes.IsEmpty)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
foreach (string charset in GetCandidateCharsets(bytes, declaredCharset))
|
||||
{
|
||||
try
|
||||
{
|
||||
Encoding baseEncoding = Encoding.GetEncoding(charset);
|
||||
Encoding strictEncoding = Encoding.GetEncoding(
|
||||
baseEncoding.CodePage,
|
||||
EncoderFallback.ExceptionFallback,
|
||||
DecoderFallback.ExceptionFallback);
|
||||
return strictEncoding.GetString(bytes);
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
{
|
||||
// Some SGU responses declare UTF-8 but contain Windows-1252 bytes.
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// Ignore unknown declarations and continue with content detection.
|
||||
}
|
||||
}
|
||||
|
||||
return Encoding.GetEncoding(1252).GetString(bytes);
|
||||
}
|
||||
|
||||
private static IEnumerable<string> GetCandidateCharsets(
|
||||
ReadOnlySpan<byte> bytes,
|
||||
string? declaredCharset)
|
||||
{
|
||||
List<string> candidates = [];
|
||||
HashSet<string> seen = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
AddCandidate(candidates, seen, DetectBom(bytes));
|
||||
AddCandidate(candidates, seen, declaredCharset);
|
||||
|
||||
int scanLength = Math.Min(bytes.Length, MetaScanBytes);
|
||||
string header = Encoding.Latin1.GetString(bytes[..scanLength]);
|
||||
Match metaCharset = CharsetPattern().Match(header);
|
||||
if (metaCharset.Success)
|
||||
{
|
||||
AddCandidate(candidates, seen, metaCharset.Groups["charset"].Value);
|
||||
}
|
||||
|
||||
AddCandidate(candidates, seen, "utf-8");
|
||||
AddCandidate(candidates, seen, "windows-1252");
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private static void AddCandidate(
|
||||
List<string> candidates,
|
||||
HashSet<string> seen,
|
||||
string? charset)
|
||||
{
|
||||
string? normalized = charset?.Trim().Trim('"', '\'');
|
||||
if (!string.IsNullOrWhiteSpace(normalized) && seen.Add(normalized))
|
||||
{
|
||||
candidates.Add(normalized);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? DetectBom(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
if (bytes.StartsWith(new byte[] { 0xEF, 0xBB, 0xBF }))
|
||||
{
|
||||
return "utf-8";
|
||||
}
|
||||
|
||||
if (bytes.StartsWith(new byte[] { 0xFF, 0xFE }))
|
||||
{
|
||||
return "utf-16";
|
||||
}
|
||||
|
||||
if (bytes.StartsWith(new byte[] { 0xFE, 0xFF }))
|
||||
{
|
||||
return "unicodeFFFE";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[GeneratedRegex(
|
||||
"charset\\s*=\\s*[\\\"']?\\s*(?<charset>[A-Za-z0-9._-]+)",
|
||||
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
|
||||
private static partial Regex CharsetPattern();
|
||||
}
|
||||
@@ -27,11 +27,11 @@ public static class SguProfileParser
|
||||
|
||||
InstitutionalProfile profile = new(
|
||||
EmployeeNumber: employeeNumber,
|
||||
DisplayName: Limit(displayName, 256),
|
||||
DisplayName: NormalizeTitle(displayName, 256),
|
||||
Email: NormalizeEmail(ExtractSpanText(html, EmailId)),
|
||||
EmployeeType: Limit(ExtractSpanText(html, EmployeeTypeId), 256),
|
||||
JobTitle: Limit(ExtractSpanText(html, JobTitleId), 64),
|
||||
Department: Limit(ExtractSpanText(html, DepartmentId), 64));
|
||||
EmployeeType: NormalizeSentence(ExtractSpanText(html, EmployeeTypeId), 256),
|
||||
JobTitle: NormalizeTitle(ExtractSpanText(html, JobTitleId), 64),
|
||||
Department: NormalizeTitle(ExtractSpanText(html, DepartmentId), 64));
|
||||
return profile.HasValues ? profile : null;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public static class SguProfileParser
|
||||
ArgumentNullException.ThrowIfNull(html);
|
||||
|
||||
InstitutionalProfile profile = new(
|
||||
DisplayName: Limit(ExtractSpanText(html, MenuNameId), 256));
|
||||
DisplayName: NormalizeTitle(ExtractSpanText(html, MenuNameId), 256));
|
||||
return profile.HasValues ? profile : null;
|
||||
}
|
||||
|
||||
@@ -167,13 +167,27 @@ public static class SguProfileParser
|
||||
return null;
|
||||
}
|
||||
|
||||
return address.Address;
|
||||
return address.Address.ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static string? NormalizeTitle(string? value, int maximumLength)
|
||||
{
|
||||
string? candidate = Limit(value, maximumLength);
|
||||
return candidate is null ? null : SpanishTextNormalizer.ToTitleCase(candidate);
|
||||
}
|
||||
|
||||
private static string? NormalizeSentence(string? value, int maximumLength)
|
||||
{
|
||||
string? candidate = Limit(value, maximumLength);
|
||||
return candidate is null ? null : SpanishTextNormalizer.ToSentenceCase(candidate);
|
||||
}
|
||||
|
||||
private static string? Limit(string? value, int maximumLength)
|
||||
{
|
||||
string? candidate = value?.Trim();
|
||||
return string.IsNullOrEmpty(candidate) || candidate.Length > maximumLength
|
||||
return string.IsNullOrEmpty(candidate) ||
|
||||
candidate.Length > maximumLength ||
|
||||
candidate.Contains('\uFFFD')
|
||||
? null
|
||||
: candidate;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace SGU.AuthBroker.Core.Profiles;
|
||||
|
||||
public static class SpanishTextNormalizer
|
||||
{
|
||||
private static readonly CultureInfo SpanishCulture = CultureInfo.GetCultureInfo("es-MX");
|
||||
|
||||
private static readonly HashSet<string> LowercaseParticles = new(
|
||||
[
|
||||
"a", "al", "da", "das", "de", "del", "do", "dos", "e", "el",
|
||||
"la", "las", "los", "o", "u", "van", "von", "y"
|
||||
],
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static string ToTitleCase(string value)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
|
||||
string[] words = value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int index = 0; index < words.Length; index++)
|
||||
{
|
||||
string lowercase = words[index].ToLower(SpanishCulture);
|
||||
string comparisonToken = lowercase.Trim('(', ')', '[', ']', '{', '}', ',', '.', ';', ':');
|
||||
words[index] = index > 0 && LowercaseParticles.Contains(comparisonToken)
|
||||
? lowercase
|
||||
: CapitalizeCompound(lowercase);
|
||||
}
|
||||
|
||||
return string.Join(' ', words);
|
||||
}
|
||||
|
||||
public static string ToSentenceCase(string value)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
|
||||
string lowercase = value.ToLower(SpanishCulture);
|
||||
StringBuilder result = new(lowercase);
|
||||
for (int index = 0; index < result.Length; index++)
|
||||
{
|
||||
if (!char.IsLetter(result[index]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result[index] = char.ToUpper(result[index], SpanishCulture);
|
||||
break;
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
private static string CapitalizeCompound(string value)
|
||||
{
|
||||
StringBuilder result = new(value.Length);
|
||||
bool capitalizeNextLetter = true;
|
||||
foreach (char character in value)
|
||||
{
|
||||
if (capitalizeNextLetter && char.IsLetter(character))
|
||||
{
|
||||
result.Append(char.ToUpper(character, SpanishCulture));
|
||||
capitalizeNextLetter = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Append(character);
|
||||
}
|
||||
|
||||
if (character is '-' or '\'' or '\u2019')
|
||||
{
|
||||
capitalizeNextLetter = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user