Beispiel #1
0
 public UsersController(GitHubAuth ghAuth,
                        CoreAuth coreAuth,
                        GitHubClient client,
                        SymmetricAlgorithm encAlgo,
                        ModRepoContext context)
 {
     githubAuthSettings = ghAuth;
     coreAuthSettings   = coreAuth;
     this.client        = client;
     stateEncAlgo       = encAlgo;
     repoContext        = context;
     UpdateCurrentRandomData();
 }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var mode             = Configuration.GetValue <string>("DBMode");
            var connectionString = Configuration.GetConnectionString("DefaultConnection");

            var ghAuthSettings = new GitHubAuth();

            Configuration.Bind("GithubAuth", ghAuthSettings);
            services.AddSingleton(ghAuthSettings);

            var coreAuthSettings = new CoreAuth();

            Configuration.Bind("CoreAuth", coreAuthSettings);
            services.AddSingleton(coreAuthSettings);

            var asmVersion = new SemVer.Version(Assembly.GetExecutingAssembly()
                                                .GetCustomAttribute <AssemblyInformationalVersionAttribute>() !.InformationalVersion);

            services.AddSingleton(asmVersion);

            services.AddScoped <GitHubClient>(s =>
                                              new GitHubClient(
                                                  new Connection(
                                                      new ProductHeaderValue("BeatMods2",
                                                                             s.GetRequiredService <SemVer.Version>().ToString()))));

            services
            .AddSingleton <IActionContextAccessor, ActionContextAccessor>()
            .AddScoped(x => x
                       .GetRequiredService <IUrlHelperFactory>()
                       .GetUrlHelper(x.GetRequiredService <IActionContextAccessor>().ActionContext));
            services.AddSingleton <SymmetricAlgorithm>(s => {
                using var key = Utils.CoerceToSize(256 / 8, s.GetRequiredService <GitHubAuth>().StateEncKeyBytes);
                return(new AesManaged()
                {
                    Key = key.Memory.ToArray(),
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.ISO10126
                });
            });

            Action <DbContextOptionsBuilder> builder;

            if (mode == "postgres")
            {
                builder = options => options.UseNpgsql(connectionString);
            }
            else
            {
                throw new Exception($"Invalid mode {mode}");
            }

            services.AddDbContext <ModRepoContext>(builder);

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => {
                Configuration.Bind("JwtSettings", options);
                options.TokenValidationParameters.IssuerSigningKey
                    = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(coreAuthSettings.JwtSecret));
            });

            services.AddRouting();

            services.AddControllers()
            .AddNewtonsoftJson();
        }