示例#1
0
 public static void UseCors(this IMiddlewareSupport middleware, Action <CorsOptions> setOptions = null)
 {
     middleware.Use(async(context, next) =>
     {
         context.Response.SetCorsHeaders(CorsOptions.GetCorsOptions(setOptions));
         await next(context);
     });
 }
示例#2
0
        public static void JwtAuth(this IMiddlewareSupport middlewareSupport, ITokenDecoder <JwtToken> tokenDecoder,
                                   ILogger logger, Func <Request, bool> excludeFilter = null)
        {
            middlewareSupport.Use(async(context, next) =>
            {
                if (excludeFilter != null && excludeFilter(context.Request))
                {
                    await next(context);
                    return;
                }

                const string jwtHeaderPrefix = "Bearer ";

                if (!context.Request.Headers.ContainsKey("Authorization"))
                {
                    await Task.Run(() => context.Response.StatusCode = HttpStatusCode.Unauthorized);
                    return;
                }

                var authorizationHeader = context.Request.Headers["Authorization"].FirstOrDefault();
                if (authorizationHeader == null || !authorizationHeader.StartsWith(jwtHeaderPrefix))
                {
                    await Task.Run(() => context.Response.StatusCode = HttpStatusCode.Unauthorized);
                    return;
                }

                var token = authorizationHeader.Substring(jwtHeaderPrefix.Length);

                try
                {
                    var payload = tokenDecoder.DecodeToken(token);

                    var tokenExpires = ExpDateTimeConverter.ToDateTime(payload.exp);

                    if (tokenExpires <= DateTime.UtcNow)
                    {
                        await Task.Run(() => context.Response.StatusCode = HttpStatusCode.Unauthorized);
                        return;
                    }

                    context.Extensions.Register(new Session.Session(payload.username));
                }
                catch (Exception e)
                {
                    logger.LogError(e, $"Request {context.Request.Method} {context.Request.Url} unauthorized!");
                    await Task.Run(() => context.Response.StatusCode = HttpStatusCode.Unauthorized);
                    return;
                }

                await next(context);
            });
        }
示例#3
0
        public static void UseRestErrorHandling(
            this IMiddlewareSupport middlewareSupport,
            IErrorHandlingConfiguration configuration,
            ExceptionStatusCodeMapper mapper,
            ILogger <ErrorHandlingModule> logger)
        {
            middlewareSupport.Use(async(context, next) =>
            {
                try
                {
                    await next(context);
                }
                catch (Exception e)
                {
                    logger.LogError(e, $"Error during processing request: {context.Request.Method} {context.Request.Url}");
                    var mimeTypes = context.Request.GetResponseMimeTypesPriority(new[]
                    {
                        MediaTypeNames.Application.Json,
                        MediaTypeNames.Text.Html
                    });

                    if (mimeTypes != null)
                    {
                        switch (mimeTypes.FirstOrDefault())
                        {
                        case MediaTypeNames.Application.Json:
                            await context.Response.SetJsonResponse(e, mapper, configuration);
                            return;

                        case MediaTypeNames.Text.Html:
                            await context.Response.SetHtmlResponse(e, configuration);
                            return;
                        }
                    }

                    await context.Response.SetHtmlResponse(e, configuration);
                }
            });
        }