Exemple #1
0
        public AuthenticationRepo(YamlSettings yamlSettings, DataKeys dataKeys, AuthSkeleton authSkeleton)
        {
            _yamlSettings = yamlSettings;
            _dataKeys     = dataKeys;
            _authSkeleton = authSkeleton;
            _auths        = new List <Auth>();

            var data = YamlConfigurationFileParser.Parse(_yamlSettings.YamlDataFile);

            foreach (KeyValuePair <string, YamlSequenceNode> entry in data)
            {
                if (entry.Key.Contains(_dataKeys.AuthKey))
                {
                    foreach (YamlMappingNode node in entry.Value) // Auths
                    {
                        Auth auth = new Auth();
                        foreach (var item in node.Children) // each auth
                        {
                            var key   = item.Key.ToString();
                            var value = item.Value;

                            if (key.Equals(_authSkeleton.UserName))
                            {
                                auth.UserName = value.ToString();
                            }
                            else if (key.Equals(_authSkeleton.Password))
                            {
                                auth.Password = value.ToString();
                            }
                        }
                        _auths.Add(auth);
                    }
                }
            }
        }
Exemple #2
0
        public void InstallServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddMvc();
            services.AddCors();


            var clientEndPoint = new ClientEndPoint();

            configuration.Bind(nameof(clientEndPoint), clientEndPoint);
            services.AddSingleton <ClientEndPoint>(clientEndPoint);

            var authSkeleton = new AuthSkeleton();

            configuration.Bind(nameof(authSkeleton), authSkeleton);
            services.AddSingleton <AuthSkeleton>(authSkeleton);

            var customerSkeleton = new CustomerSkeleton();

            configuration.Bind(nameof(customerSkeleton), customerSkeleton);
            services.AddSingleton <CustomerSkeleton>(customerSkeleton);

            var dataKeys = new DataKeys();

            configuration.Bind(nameof(dataKeys), dataKeys);
            services.AddSingleton <DataKeys>(dataKeys);

            var yamlSettings = new YamlSettings();

            configuration.Bind(nameof(yamlSettings), yamlSettings);
            services.AddSingleton <YamlSettings>(yamlSettings);

            var jwtSettings = new JwtSettings();

            configuration.Bind(nameof(jwtSettings), jwtSettings);
            services.AddSingleton <JwtSettings>(jwtSettings);

            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)),
                ValidateIssuer           = false,
                ValidateAudience         = false,
                RequireExpirationTime    = false,
                ValidateLifetime         = true
            };

            services.AddSingleton(tokenValidationParameters);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.SaveToken = true;
                x.TokenValidationParameters = tokenValidationParameters;
            });

            services.AddSwaggerGen(x =>
            {
                x.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Customer API", Version = "v1"
                });
                var security = new Dictionary <string, IEnumerable <string> >
                {
                    { "Bearer", new string[0] }
                };
                x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });
                x.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header
                        },
                        new List <string>()
                    }
                });
            });
        }