Exemplo n.º 1
0
        public async Task Invoke(DownstreamContext context)
        {
            if (IsAuthenticatedRoute(context.DownstreamReRoute))
            {
                _logger.LogDebug($"{context.HttpContext.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");

                var result = await context.HttpContext.AuthenticateAsync(context.DownstreamReRoute.AuthenticationOptions.AuthenticationProviderKey);

                context.HttpContext.User = result.Principal;

                if (context.HttpContext.User.Identity.IsAuthenticated)
                {
                    _logger.LogDebug($"Client has been authenticated for {context.HttpContext.Request.Path}");
                    await _next.Invoke(context);
                }
                else
                {
                    var error = new List <Error>
                    {
                        new UnauthenticatedError(
                            $"Request for authenticated route {context.HttpContext.Request.Path} by {context.HttpContext.User.Identity.Name} was unauthenticated")
                    };

                    _logger.LogError($"Client has NOT been authenticated for {context.HttpContext.Request.Path} and pipeline error set. {error.ToErrorString()}");

                    SetPipelineError(context, error);
                }
            }
            else
            {
                _logger.LogTrace($"No authentication needed for {context.HttpContext.Request.Path}");

                await _next.Invoke(context);
            }
        }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            _logger.TraceMiddlewareEntry();

            if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
            {
                _logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlwareName} checking if client is authenticated");

                var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);

                if (authenticationHandler.IsError)
                {
                    _logger.LogError($"Error getting authentication handler for {context.Request.Path}. {authenticationHandler.Errors.ToErrorString()}");
                    SetPipelineError(authenticationHandler.Errors);
                    _logger.TraceMiddlewareCompleted();
                    return;
                }

                await authenticationHandler.Data.Handler.Handle(context);


                if (context.User.Identity.IsAuthenticated)
                {
                    _logger.LogDebug($"Client has been authenticated for {context.Request.Path}");

                    _logger.TraceInvokeNext();
                    await _next.Invoke(context);

                    _logger.TraceInvokeNextCompleted();
                    _logger.TraceMiddlewareCompleted();
                }
                else
                {
                    var error = new List <Error>
                    {
                        new UnauthenticatedError(
                            $"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated")
                    };

                    _logger.LogError($"Client has NOT been authenticated for {context.Request.Path} and pipeline error set. {error.ToErrorString()}");
                    SetPipelineError(error);

                    _logger.TraceMiddlewareCompleted();
                    return;
                }
            }
            else
            {
                _logger.LogTrace($"No authentication needed for {context.Request.Path}");

                _logger.TraceInvokeNext();
                await _next.Invoke(context);

                _logger.TraceInvokeNextCompleted();
                _logger.TraceMiddlewareCompleted();
            }
        }
 public virtual void OcelotMiddlewareException(Exception exception, DownstreamContext context, string name)
 {
     _logger.LogTrace($"Ocelot.MiddlewareException: {name}; {exception.Message};");
     Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
 }
 public virtual void OnMiddlewareStarting(HttpContext httpContext, string name)
 {
     _logger.LogTrace($"MiddlewareStarting: {name}; {httpContext.Request.Path}");
     Event(httpContext, $"MiddlewareStarting: {name}; {httpContext.Request.Path}");
 }
Exemplo n.º 5
0
 public static void TraceMiddlewareEntry(this IOcelotLogger logger)
 {
     logger.LogTrace($"entered {logger.Name}");
 }
Exemplo n.º 6
0
 public static void TraceMiddlewareCompleted(this IOcelotLogger logger)
 {
     logger.LogTrace($"completed {logger.Name}");
 }
Exemplo n.º 7
0
 public static void TraceInvokeNextCompleted(this IOcelotLogger logger)
 {
     logger.LogTrace($"returned to {logger.Name} after next middleware completed");
 }
Exemplo n.º 8
0
 public static void TraceInvokeNext(this IOcelotLogger logger)
 {
     logger.LogTrace($"invoking next middleware from {logger.Name}");
 }