Added IHttpAuthenticationSource

master
Harald Wolff 2022-05-28 19:17:00 +02:00
parent a1a55d6d10
commit 48370c7564
3 changed files with 27 additions and 7 deletions

View File

@ -4,17 +4,17 @@ namespace ln.http
{ {
public class HttpContext public class HttpContext
{ {
public HTTPServer HttpServer { get; } public HttpServer HttpServer { get; }
public HttpRequest Request { get; set; } public HttpRequest Request { get; set; }
public HttpResponse Response { get; set; } public HttpResponse Response { get; set; }
public HttpPrincipal AuthenticatedPrincipal { get; private set; } public HttpPrincipal AuthenticatedPrincipal { get; private set; }
public HttpContext(HTTPServer httpServer) public HttpContext(HttpServer httpServer)
{ {
HttpServer = httpServer; HttpServer = httpServer;
} }
public HttpContext(HTTPServer httpServer, HttpRequest httpRequest) : this(httpServer) public HttpContext(HttpServer httpServer, HttpRequest httpRequest) : this(httpServer)
{ {
Request = httpRequest; Request = httpRequest;
RoutableUri = httpRequest.RequestUri.AbsolutePath; RoutableUri = httpRequest.RequestUri.AbsolutePath;
@ -22,12 +22,15 @@ namespace ln.http
public string RoutableUri { get; set; } public string RoutableUri { get; set; }
public bool Authenticate(HttpAuthenticationDelegate authenticationDelegate) public bool Authenticate()
{ {
if (authenticationDelegate(this, out HttpPrincipal principal)) foreach (var authenticationSource in HttpServer.AuthenticationSources)
{ {
AuthenticatedPrincipal = principal; if (authenticationSource.AuthenticatePrincipal(this, out HttpPrincipal httpPrincipal))
return true; {
AuthenticatedPrincipal = httpPrincipal;
return true;
}
} }
return false; return false;
} }

View File

@ -18,6 +18,11 @@ namespace ln.http
public TextWriter LoggingWriter { get; set; } public TextWriter LoggingWriter { get; set; }
private HashSet<IHttpAuthenticationSource> _authenticationSources = new HashSet<IHttpAuthenticationSource>();
public IEnumerable<IHttpAuthenticationSource> AuthenticationSources => _authenticationSources;
public HttpServer() : this(Console.Out) public HttpServer() : this(Console.Out)
{ {
} }
@ -31,6 +36,11 @@ namespace ln.http
AddRouter(router); AddRouter(router);
} }
public void RegisterAuthenticationSource(IHttpAuthenticationSource authenticationSource) =>
_authenticationSources.Add(authenticationSource);
public void UnregisterAuthenticationSource(IHttpAuthenticationSource authenticationSource) =>
_authenticationSources.Remove(authenticationSource);
public void AddRouter(HttpRouter httpRouter) => AddRouter(httpRouter.Route); public void AddRouter(HttpRouter httpRouter) => AddRouter(httpRouter.Route);
public void AddRouter(HttpRouterDelegate routerDelegate) => _routerDelegates.Add(routerDelegate); public void AddRouter(HttpRouterDelegate routerDelegate) => _routerDelegates.Add(routerDelegate);

View File

@ -0,0 +1,7 @@
namespace ln.http
{
public interface IHttpAuthenticationSource
{
bool AuthenticatePrincipal(HttpContext httpContext, out HttpPrincipal httpPrincipal);
}
}