From 48370c7564fbcb980bdcd7c2bc2a2380d286440f Mon Sep 17 00:00:00 2001 From: Harald Wolff-Thobaben Date: Sat, 28 May 2022 19:17:00 +0200 Subject: [PATCH] Added IHttpAuthenticationSource --- ln.http/HttpContext.cs | 17 ++++++++++------- ln.http/HttpServer.cs | 10 ++++++++++ ln.http/IHttpAuthenticationSource.cs | 7 +++++++ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 ln.http/IHttpAuthenticationSource.cs diff --git a/ln.http/HttpContext.cs b/ln.http/HttpContext.cs index 27beaea..fb64e84 100644 --- a/ln.http/HttpContext.cs +++ b/ln.http/HttpContext.cs @@ -4,17 +4,17 @@ namespace ln.http { public class HttpContext { - public HTTPServer HttpServer { get; } + public HttpServer HttpServer { get; } public HttpRequest Request { get; set; } public HttpResponse Response { get; set; } public HttpPrincipal AuthenticatedPrincipal { get; private set; } - public HttpContext(HTTPServer httpServer) + public HttpContext(HttpServer httpServer) { HttpServer = httpServer; } - public HttpContext(HTTPServer httpServer, HttpRequest httpRequest) : this(httpServer) + public HttpContext(HttpServer httpServer, HttpRequest httpRequest) : this(httpServer) { Request = httpRequest; RoutableUri = httpRequest.RequestUri.AbsolutePath; @@ -22,12 +22,15 @@ namespace ln.http 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; - return true; + if (authenticationSource.AuthenticatePrincipal(this, out HttpPrincipal httpPrincipal)) + { + AuthenticatedPrincipal = httpPrincipal; + return true; + } } return false; } diff --git a/ln.http/HttpServer.cs b/ln.http/HttpServer.cs index 253af21..64db98f 100644 --- a/ln.http/HttpServer.cs +++ b/ln.http/HttpServer.cs @@ -18,6 +18,11 @@ namespace ln.http public TextWriter LoggingWriter { get; set; } + + private HashSet _authenticationSources = new HashSet(); + public IEnumerable AuthenticationSources => _authenticationSources; + + public HttpServer() : this(Console.Out) { } @@ -31,6 +36,11 @@ namespace ln.http 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(HttpRouterDelegate routerDelegate) => _routerDelegates.Add(routerDelegate); diff --git a/ln.http/IHttpAuthenticationSource.cs b/ln.http/IHttpAuthenticationSource.cs new file mode 100644 index 0000000..99bb6c9 --- /dev/null +++ b/ln.http/IHttpAuthenticationSource.cs @@ -0,0 +1,7 @@ +namespace ln.http +{ + public interface IHttpAuthenticationSource + { + bool AuthenticatePrincipal(HttpContext httpContext, out HttpPrincipal httpPrincipal); + } +} \ No newline at end of file