Beispiel #1
0
        public override void Configure(Container container)
        {
            JsConfig.DateHandler = DateHandler.ISO8601;

            var appSettings = new AppSettings();

            ServiceExceptionHandlers.Add((httpReq, request, exception) =>
            {
                var logger = LogManager.GetLogger(GetType());
                logger.Error(exception);
                return(null);
            });


            container.Register <ICacheClient>(new MemoryCacheClient {
                FlushOnDispose = false
            });


            JsConfig.AssumeUtc = true;

            container.RegisterAs <FordereAuthEventHandler, IAuthEvents>();

            container.Register <IDbConnectionFactory>(
                new OrmLiteConnectionFactory(
                    "Server = {0}; Database = {1}; Uid = {2}; Pwd = {3}".Fmt(
                        appSettings.Get("DB.Host"),
                        appSettings.Get("DB.Name"),
                        appSettings.Get("DB.User"),
                        appSettings.Get("DB.Pass")),
                    MySqlDialect.Provider));

            container.Register <IUserAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>()));

            var authProvider = new IAuthProvider[]
            {
                new CredentialsAuthProvider(),
                new JwtAuthProvider(appSettings),
            }.ToList();

            if (appSettings.Get("Debug", false))
            {
                authProvider.Add(new BasicAuthProvider());
            }

            var authFeature = new AuthFeature(() => new FordereAuthUserService(), authProvider.ToArray());

            this.Plugins.Add(new RegistrationFeature());
            this.Plugins.Add(authFeature);


            this.Plugins.Add(new RequestLogsFeature
            {
                // do not log request bodies of requests containing passwords
                HideRequestBodyForRequestDtoTypes = new[] { typeof(Authenticate), typeof(Register), typeof(UpdateUserProfileRequest) },
            });

            if (appSettings.Get("CORS.Enabled", false))
            {
                this.Plugins.Add(
                    new CorsFeature(
                        allowedOrigins: appSettings.GetString("CORS.AllowedOrigins"),
                        allowedMethods: "OPTIONS,GET,POST,PUT,DELETE,PATCH",
                        allowedHeaders: "Content-Type,Authorization,division_id",
                        allowCredentials: true));
            }

            if (appSettings.Get("Debug", false))
            {
                this.Plugins.Add(new PostmanFeature());
                this.Plugins.Add(new OpenApiFeature());
            }

            if (appSettings.Get("Debug", false) == false)
            {
                this.Plugins.RemoveAll(x => x is MetadataFeature);
            }

            this.SetConfig(new HostConfig
            {
                // TODO SSH This sets ss-pid/ss-opt to NOT HttpOnly.. is this a security issue?
                AllowNonHttpOnlyCookies = true,
                DebugMode = appSettings.Get("Debug", false)
            });

            this.RegisterTypedRequestFilter <ICaptchaRequest>(Filter.Captcha);
            this.RegisterTypedRequestFilter <EnterMatchAppointmentRequest>(Filter.EnterMatchAppointment);
            this.RegisterTypedRequestFilter <EnterMatchResultRequest>(Filter.EnterMatchResult);

            this.RegisterTypedResponseFilter <TeamDto>(Filter.TeamPlayerDetails);

            PreRequestFilters.Add((httpReq, httpRes) =>
            {
                if (httpReq.Verb.ToUpper() == "PATCH")
                {
                    httpReq.UseBufferedStream = true;
                }
            });
        }