public SecretAppController(ILogger <SecretAppController> logger, NpmConfig npmConfig, IAppsService apps, IChallengeRepository challengeRepository, ObfuscatedEndpointsRepository obfuscatedEndpoints)
 {
     _logger              = logger;
     _npmConfig           = npmConfig;
     _apps                = apps;
     _challengeRepository = challengeRepository;
     _obfuscatedEndpoints = obfuscatedEndpoints;
 }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.NullValueHandling     = NullValueHandling.Ignore;
                options.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;
            });;

            var npmConfig = new NpmConfig();

            Configuration.GetSection("npm").Bind(npmConfig);
            services.AddSingleton(npmConfig);

            var jwtConfig = Configuration.GetSection("jwtConfig").Get <JwtConfig>();

            services.AddSingleton(jwtConfig);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = true;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = jwtConfig.Issuer,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.Secret)),
                    ValidAudience            = jwtConfig.Audience,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ClockSkew = TimeSpan.FromMinutes(1)
                };
            });

            services
            .AddSingleton <IJwtAuthManager, JwtAuthManager>()
            .AddSingleton <IUserService, UserService>()
            .AddSingleton <IAppsService, AppsService>()
            .AddSingleton <IChallengeRepository, ChallengeRepository>()
            .AddSingleton <ObfuscatedEndpointsRepository>();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = "Sceptical Beavers Example API",
                    Version = "v1",
                    Contact = new OpenApiContact
                    {
                        Name  = "Nikita Chizhov",
                        Email = "*****@*****.**"
                    },
                    Description = "Based on https://github.com/dotnet-labs/JwtAuthDemo"
                });

                var securityScheme = new OpenApiSecurityScheme
                {
                    Name         = "JWT Authentication",
                    Description  = "Enter JWT Bearer token **_only_**",
                    In           = ParameterLocation.Header,
                    Type         = SecuritySchemeType.Http,
                    Scheme       = "bearer",
                    BearerFormat = "JWT",
                    Reference    = new OpenApiReference
                    {
                        Id   = JwtBearerDefaults.AuthenticationScheme,
                        Type = ReferenceType.SecurityScheme
                    }
                };
                c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { securityScheme, new string[] { } }
                });
            });

            services.AddCors(options =>
            {
                options.AddPolicy(CorsPolicy,
                                  builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });
            });
        }