Enrich AD thumbnailPhoto from SGU profile images
This commit is contained in:
1 parent
21890047e2
commit
77091c023d
9 files changed
+336
-11
No files matched your search
@@ -1,5 +1,7 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SGU.AuthBroker.Core.Authentication;
|
||||
using SGU.AuthBroker.Core.Identity;
|
||||
@@ -163,7 +165,8 @@ public sealed class NtlmCredentialValidatorTests
|
||||
"""),
|
||||
JsonResponse("""
|
||||
{"d":[{"p_IdCP":"090001","p_Nombre":"FLORIDA","p_Cp":"01000"}]}
|
||||
"""));
|
||||
"""),
|
||||
PhotoResponse(TestJpeg()));
|
||||
NtlmCredentialValidator validator = CreateValidator(handler);
|
||||
|
||||
NtlmValidationResult result = await validator.ValidateAsync(
|
||||
@@ -184,6 +187,7 @@ public sealed class NtlmCredentialValidatorTests
|
||||
Assert.Equal("Ciudad de México", result.Profile.State);
|
||||
Assert.Equal("01000", result.Profile.PostalCode);
|
||||
Assert.Equal(InstitutionalGender.Female, result.Profile.Gender);
|
||||
Assert.Equal(TestJpeg(), result.Profile.ThumbnailPhoto);
|
||||
Assert.Equal(
|
||||
[
|
||||
"/psulsa/",
|
||||
@@ -193,7 +197,8 @@ public sealed class NtlmCredentialValidatorTests
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetDireccion",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetLocalidadListado",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetColoniasListado"
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetColoniasListado",
|
||||
"/psulsa/gadmon/admonPersonal/ashx/Fotografia.ashx"
|
||||
],
|
||||
handler.RequestPaths);
|
||||
Assert.Equal("{\"CodigoPostal\":\"01000\"}", handler.RequestBodies[5]);
|
||||
@@ -201,6 +206,9 @@ public sealed class NtlmCredentialValidatorTests
|
||||
Assert.Equal(
|
||||
"{\"pIdEstado\":\"\",\"pLocalidad\":\"\",\"CodigoPostal\":\"01000\"}",
|
||||
handler.RequestBodies[7]);
|
||||
Assert.Equal(
|
||||
"https://sgu.example/psulsa/gadmon/admonPersonal/ashx/Fotografia.ashx?id=017045&tp=2",
|
||||
handler.RequestUris[8]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -278,7 +286,8 @@ public sealed class NtlmCredentialValidatorTests
|
||||
"""),
|
||||
JsonResponse("""
|
||||
{"d":[{"p_IdCP":"090001","p_Nombre":"FLORIDA","p_Cp":"01000"}]}
|
||||
"""));
|
||||
"""),
|
||||
Response(HttpStatusCode.NotFound));
|
||||
NtlmCredentialValidator validator = CreateValidator(handler);
|
||||
|
||||
NtlmValidationResult result = await validator.ValidateAsync(
|
||||
@@ -311,9 +320,13 @@ public sealed class NtlmCredentialValidatorTests
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetDireccion",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetLocalidadListado",
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetColoniasListado"
|
||||
"/psulsa/gadmon/capitalhumano/datos/ubicacion.aspx/GetColoniasListado",
|
||||
"/psulsa/gadmon/admonPersonal/ashx/Fotografia.ashx"
|
||||
],
|
||||
handler.RequestPaths);
|
||||
Assert.Equal(
|
||||
"https://sgu.example/psulsa/gadmon/admonPersonal/ashx/Fotografia.ashx?id=123456&tp=2",
|
||||
handler.RequestUris[9]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -347,6 +360,109 @@ public sealed class NtlmCredentialValidatorTests
|
||||
handler.RequestPaths[3]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task VerifiedStudentReceivesPhotoFromAuthenticatedSguSession()
|
||||
{
|
||||
byte[] jpeg = TestJpeg();
|
||||
SequenceHandler handler = new(
|
||||
Challenge(),
|
||||
Response(HttpStatusCode.OK),
|
||||
Response(HttpStatusCode.OK,
|
||||
"<span id=\"ctl00_contenedor_HistorialAlumno1_lblClaveAlumnoHP\">123456</span>"),
|
||||
PhotoResponse(jpeg));
|
||||
NtlmValidationResult result = await CreateValidator(handler).ValidateAsync(
|
||||
Student,
|
||||
"test-password",
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(NtlmValidationStatus.Valid, result.Status);
|
||||
Assert.Equal(jpeg, result.Profile?.ThumbnailPhoto);
|
||||
Assert.Equal(
|
||||
"https://sgu.example/psulsa/gadmon/admonPersonal/ashx/Fotografia.ashx?id=123456&tp=1",
|
||||
handler.RequestUris[3]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("text/html")]
|
||||
[InlineData("image/jpeg")]
|
||||
public async Task InvalidStudentPhotoDoesNotBlockAuthentication(string mediaType)
|
||||
{
|
||||
byte[] invalid = new byte[150];
|
||||
invalid[0] = 0xFF;
|
||||
invalid[1] = 0xD8;
|
||||
invalid[2] = 0xFF;
|
||||
invalid[^2] = 0xFF;
|
||||
invalid[^1] = 0xD9;
|
||||
SequenceHandler handler = new(
|
||||
Challenge(),
|
||||
Response(HttpStatusCode.OK),
|
||||
Response(HttpStatusCode.OK,
|
||||
"<span id=\"ctl00_contenedor_HistorialAlumno1_lblClaveAlumnoHP\">123456</span>"),
|
||||
PhotoResponse(invalid, mediaType));
|
||||
NtlmValidationResult result = await CreateValidator(handler).ValidateAsync(
|
||||
Student,
|
||||
"test-password",
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(NtlmValidationStatus.Valid, result.Status);
|
||||
Assert.Equal("123456", result.Profile?.EmployeeNumber);
|
||||
Assert.Null(result.Profile?.ThumbnailPhoto);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OversizedStudentPhotoDoesNotBlockAuthentication()
|
||||
{
|
||||
SequenceHandler handler = new(
|
||||
Challenge(),
|
||||
Response(HttpStatusCode.OK),
|
||||
Response(HttpStatusCode.OK,
|
||||
"<span id=\"ctl00_contenedor_HistorialAlumno1_lblClaveAlumnoHP\">123456</span>"),
|
||||
PhotoResponse(new byte[100 * 1024 + 1]));
|
||||
NtlmValidationResult result = await CreateValidator(handler).ValidateAsync(
|
||||
Student,
|
||||
"test-password",
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(NtlmValidationStatus.Valid, result.Status);
|
||||
Assert.Equal("123456", result.Profile?.EmployeeNumber);
|
||||
Assert.Null(result.Profile?.ThumbnailPhoto);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UnverifiedStudentNumberNeverFetchesPhoto()
|
||||
{
|
||||
SequenceHandler handler = new(
|
||||
Challenge(),
|
||||
Response(HttpStatusCode.OK),
|
||||
Response(HttpStatusCode.OK,
|
||||
"<span id=\"ctl00_contenedor_HistorialAlumno1_lblClaveAlumnoHP\">654321</span>"));
|
||||
NtlmValidationResult result = await CreateValidator(handler).ValidateAsync(
|
||||
Student,
|
||||
"test-password",
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(NtlmValidationStatus.Valid, result.Status);
|
||||
Assert.Null(result.Profile);
|
||||
Assert.Equal(3, handler.RequestUris.Count);
|
||||
}
|
||||
|
||||
private static byte[] TestJpeg()
|
||||
{
|
||||
using Bitmap bitmap = new(16, 16);
|
||||
using MemoryStream output = new();
|
||||
bitmap.Save(output, ImageFormat.Jpeg);
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
private static HttpResponseMessage PhotoResponse(byte[] bytes, string mediaType = "image/jpeg") =>
|
||||
new(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new ByteArrayContent(bytes)
|
||||
{
|
||||
Headers = { ContentType = new MediaTypeHeaderValue(mediaType) }
|
||||
}
|
||||
};
|
||||
|
||||
private static NtlmCredentialValidator CreateValidator(SequenceHandler handler)
|
||||
{
|
||||
BrokerOptions options = new()
|
||||
@@ -406,6 +522,8 @@ public sealed class NtlmCredentialValidatorTests
|
||||
|
||||
public List<string> RequestPaths { get; } = [];
|
||||
|
||||
public List<string> RequestUris { get; } = [];
|
||||
|
||||
public List<string?> RequestBodies { get; } = [];
|
||||
|
||||
protected override Task<HttpResponseMessage> SendAsync(
|
||||
@@ -414,6 +532,7 @@ public sealed class NtlmCredentialValidatorTests
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
RequestPaths.Add(request.RequestUri!.AbsolutePath);
|
||||
RequestUris.Add(request.RequestUri.AbsoluteUri);
|
||||
RequestBodies.Add(request.Content is null
|
||||
? null
|
||||
: request.Content.ReadAsStringAsync(cancellationToken).GetAwaiter().GetResult());
|
||||
|
||||
Reference in new issue
Block a user