Пример #1
0
        public BaseController(IService <TModel> service, IHttpAuthService authService)
        {
            this.service     = service ?? throw new ArgumentNullException(nameof(service));
            this.authService = authService ?? throw new ArgumentNullException(nameof(authService));

            this.service.ValidationDictionary = new ModelStateWrapper(ModelState);
            this.service.AuthService          = authService;
        }
Пример #2
0
 public AuthController(IService <AuthToken> service, IHttpAuthService authService) : base(service, authService)
 {
 }
Пример #3
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpAuthService httpAuthService, ILogger logger)
        {
            // cors config
            app.UseCors(c =>
            {
                c.AllowAnyHeader();
                c.AllowAnyMethod();
                c.AllowAnyOrigin();
                c.AllowCredentials();
            });

            // ocelot config
            var ocelotConfiguration = new OcelotPipelineConfiguration
            {
                PreErrorResponderMiddleware = async(ctx, next) =>
                {
                    await next.Invoke();
                },
                AuthenticationMiddleware = async(ctx, next) =>
                {
                    try
                    {
                        if (string.IsNullOrEmpty(ctx?.DownstreamReRoute?.AuthenticationOptions?.AuthenticationProviderKey))
                        {
                            await next.Invoke();

                            return;
                        }

                        var token = GetAuthToken(ctx.HttpContext);

                        if (httpAuthService == null)
                        {
                            ctx.HttpContext.Response.StatusCode = 401;
                            await next.Invoke();

                            return;
                        }

                        var user = await httpAuthService.GetUser(token);

                        if (user == null || user.Data == null || !user.IsSuccess)
                        {
                            ctx.HttpContext.Response.StatusCode = 401;
                            await next.Invoke();

                            return;
                        }

                        var userIdString = AttachUserId(ctx.HttpContext, user.Data.Id);
                        if (!string.IsNullOrEmpty(userIdString))
                        {
                            ctx.DownstreamRequest.AbsolutePath = userIdString;
                            ctx.DownstreamRequest.Query        = $"userId={user.Data.Id}";
                        }

                        await next.Invoke();
                    }
                    catch (Exception ex)
                    {
                        var errors = ctx.Errors.Select(a => $"{a.Message}; ");
                        logger.Error(ex, $"KnotDiary.ApiGateway.Startup | AuthenticationMiddleware | Authentication has thrown an error | {errors}");
                        ctx.HttpContext.Response.StatusCode = 401;
                        await next.Invoke();
                    }
                }
            };

            app.UseOcelot(ocelotConfiguration).Wait();
        }
Пример #4
0
 public UsersController(IService <User> service, IHttpAuthService authService) : base(service, authService)
 {
 }
 public OAuth2AuthenticationFilter(IHttpAuthService authService, IService <AuthToken> tokenService)
 {
     this.authService  = (OAuth2AuthService)authService ?? throw new ArgumentNullException(nameof(authService));
     this.tokenService = tokenService ?? throw new ArgumentNullException(nameof(tokenService));
 }
Пример #6
0
 public CrudController(IService <TModel> service, IHttpAuthService authService) : base(service, authService)
 {
 }