ln.http/ln.http/HttpContext.cs

39 lines
1.1 KiB
C#

using ln.http.router;
namespace ln.http
{
public class HttpContext
{
public HttpServer HttpServer { get; }
public HttpRequest Request { get; set; }
public HttpResponse Response { get; set; }
public HttpPrincipal AuthenticatedPrincipal { get; private set; }
public HttpContext(HttpServer httpServer)
{
HttpServer = httpServer;
}
public HttpContext(HttpServer httpServer, HttpRequest httpRequest) : this(httpServer)
{
Request = httpRequest;
RoutableUri = httpRequest.RequestUri.AbsolutePath;
}
public string RoutableUri { get; set; }
public bool Authenticate()
{
foreach (var authenticationSource in HttpServer.AuthenticationSources)
{
if (authenticationSource.AuthenticatePrincipal(this, out HttpPrincipal httpPrincipal))
{
AuthenticatedPrincipal = httpPrincipal;
return true;
}
}
return false;
}
public void DeAuthenticate() => AuthenticatedPrincipal = null;
}
}