Added IHttpAuthenticationSource
parent
a1a55d6d10
commit
48370c7564
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,11 @@ namespace ln.http
|
|||
|
||||
public TextWriter LoggingWriter { get; set; }
|
||||
|
||||
|
||||
private HashSet<IHttpAuthenticationSource> _authenticationSources = new HashSet<IHttpAuthenticationSource>();
|
||||
public IEnumerable<IHttpAuthenticationSource> 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);
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
namespace ln.http
|
||||
{
|
||||
public interface IHttpAuthenticationSource
|
||||
{
|
||||
bool AuthenticatePrincipal(HttpContext httpContext, out HttpPrincipal httpPrincipal);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue