Exemple #1
0
        private static void ProcessDbCommands(string[] args, IHost host)
        {
            var services = (IServiceScopeFactory)host.Services.GetService(typeof(IServiceScopeFactory));

            using (var scope = services.CreateScope())
            {
                var context       = scope.ServiceProvider.GetRequiredService <DblDipDbContext>();
                var store         = scope.ServiceProvider.GetRequiredService <EventStore>();
                var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>();

                if (args.Contains("ci"))
                {
                    args = new string[4] {
                        "dropdb", "migratedb", "seeddb", "stop"
                    }
                }
                ;

                if (args.Contains("dropdb"))
                {
                    context.Database.EnsureDeleted();
                    store.Database.EnsureDeleted();
                }

                if (args.Contains("migratedb"))
                {
                    context.Database.Migrate();
                    store.Database.Migrate();
                }

                if (args.Contains("seeddb"))
                {
                    context.Database.EnsureCreated();
                    store.Database.EnsureCreated();
                    DbInitializer.Initialize(context, store, configuration);
                }

                if (args.Contains("secret"))
                {
                    Console.WriteLine(SecretGenerator.Generate());
                    Environment.Exit(0);
                }

                if (args.Contains("stop"))
                {
                    Environment.Exit(0);
                }
            }
        }
        public TokenProviderTests()
        {
            var secret = SecretGenerator.Generate();

            var values = new Dictionary <string, string> {
                { $"{nameof(Authentication)}:{nameof(Authentication.TokenPath)}", "/token" },
                { $"{nameof(Authentication)}:{nameof(Authentication.ExpirationMinutes)}", "1" },
                { $"{nameof(Authentication)}:{nameof(Authentication.JwtKey)}", secret },
                { $"{nameof(Authentication)}:{nameof(Authentication.JwtIssuer)}", "UnitTests" },
                { $"{nameof(Authentication)}:{nameof(Authentication.JwtAudience)}", "UnitTests" },
                { $"{nameof(Authentication)}:{nameof(Authentication.AuthType)}", "Test" }
            };

            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(values)
                                .Build();

            _tokenProvider = new TokenProvider(configuration);
        }
        public async Task PromptAsync(CancellationToken cancellationToken)
        {
            var selectedOption = await PromptSecretTypeAsync(cancellationToken);

            switch (selectedOption)
            {
            case 1:
                var secret = SecretGenerator.Generate();

                Console.WriteLine($"Client Secret: {secret}");
                Console.WriteLine();

                var hashed256 = secret.Sha256();
                var hashed512 = secret.Sha512();

                Console.WriteLine("Your 256-bit hashed value is:");
                Console.WriteLine(hashed256);
                Console.WriteLine("Your 512-bit hashed value is:");
                Console.WriteLine(hashed512);
                Console.WriteLine();
                break;

            case 2:
                var keys = RsaKeyPairGenerator.Generate();

                Console.WriteLine("Private Key:");
                Console.WriteLine(keys.PrivateKey);
                Console.WriteLine("Public Json Web Key:");
                Console.WriteLine(keys.PublicKey);
                Console.WriteLine();

                Console.WriteLine("Enter YES to write to file system:");
                var writePromptResponse = Console.ReadLine();

                if (writePromptResponse.Equals("YES", StringComparison.InvariantCultureIgnoreCase))
                {
                    await _writePrivateKeyPrompt.PromptAsync(keys.PrivateKey, cancellationToken);
                }
                break;
            }
        }
        public static IConfiguration Create()
        {
            if (configuration == null)
            {
                var secret = SecretGenerator.Generate();

                configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(new Dictionary <string, string>()
                {
                    { "Seed:DefaultUser:Username", "*****@*****.**" },
                    { "Seed:DefaultUser:Password", "HireMe" },
                    { $"{nameof(Authentication)}:{nameof(Authentication.TokenPath)}", "/api/users/token" },
                    { $"{nameof(Authentication)}:{nameof(Authentication.ExpirationMinutes)}", "10080" },
                    { $"{nameof(Authentication)}:{nameof(Authentication.JwtKey)}", secret },
                    { $"{nameof(Authentication)}:{nameof(Authentication.JwtIssuer)}", "localhost" },
                    { $"{nameof(Authentication)}:{nameof(Authentication.JwtAudience)}", "all" },
                    { $"{nameof(Authentication)}:{nameof(Authentication.AuthType)}", "HireMe" }
                })
                                .Build();
            }

            return(configuration);
        }