Exemple #1
0
        public async Task <IActionResult> Register(RegisterViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                Notify(FlashNotificationType.Error, "Invalid credentials.");

                return(RedirectToAction("Register"));
            }

            return(await _userService.RegisterAsync(viewModel.Email, viewModel.Password)
                   .ExecuteAsync(
                       onSuccess: async() =>
            {
                Notify(FlashNotificationType.Success, "Your account has been created.");
                try
                {
                    var user = await _userService.GetAsync(viewModel.Email);
                    await _apiKeyService.CreateAsync(viewModel.Email);
                    await _organizationService.CreateDefaultAsync(user.Id);
                }
                catch (Exception exception)
                {
                    //Not important
                    Logger.Error(exception);
                }

                return RedirectToAction("Login");
            },
                       onFailure: ex =>
            {
                Notify(FlashNotificationType.Error, ex.Message);
                return RedirectToAction("Register");
            }));
        }
Exemple #2
0
        public async Task CreateAsync(string username, string password, Role?role = null)
        {
            if (username.Empty())
            {
                throw new ArgumentException("Username can not be empty.", nameof(username));
            }
            if (password.Empty())
            {
                throw new ArgumentException("Password can not be empty.", nameof(password));
            }

            var user = await _userRepository.GetAsync(username);

            if (user != null)
            {
                throw new ArgumentException($"User {username} already exists.", nameof(username));
            }

            user = new User(username, role.GetValueOrDefault(Role.User));
            user.SetPassword(password, _encrypter);
            user.Activate();
            await _userRepository.AddAsync(user);

            await _apiKeyService.CreateAsync(username);

            Logger.Info($"User '{user.Username}' was created with role '{role}'.");
        }
        public async Task HandleAsync(CreateApiKey command)
        {
            await _apiKeyService.CreateAsync(command.ApiKeyId, command.UserId);

            var apiKey = await _apiKeyService.GetAsync(command.ApiKeyId);

            await _bus.PublishAsync(new ApiKeyCreated(command.Request.Id, command.UserId, apiKey.Value.Key));
        }
        public async Task <IActionResult> CreateApiKey()
        {
            await _apiKeyService.CreateAsync(UserId).Execute(
                onSuccess: () => Notify(FlashNotificationType.Success, "API key has been created."),
                onFailure: ex => Notify(FlashNotificationType.Error, ex.Message));

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult <ApiKey> > CreateApiKey([FromBody] ApiKey apiKey)
        {
            apiKey = await _apiKeyService.CreateAsync(apiKey);

            var tenant = HttpContext.GetTenant();

            await PublishApiKeyChangeEventAsync(TopicType.Modify, apiKey, tenant !.Id !);

            return(base.CreatedAtAction(nameof(FindOne), new { id = apiKey.Id }, apiKey));
        }
Exemple #6
0
        public async Task <ActionResult <ApiKey> > Create()
        {
            try
            {
                var created = await apiKeyService.CreateAsync();

                if (created == null)
                {
                    return(StatusCode(500));
                }

                return(Created(string.Empty, created));
            }
            catch (Exception e)
            {
                Log.Error(e, "An error occured while creating a new api key");
                return(StatusCode(500));
            }
        }
Exemple #7
0
        public ApiKeyModule(IApiKeyService apiKeyService) : base("api-keys")
        {
            this.RequiresAuthentication();

            Post("", async args =>
            {
                var request = BindRequest <CreateApiKey>();
                var apiKey  = await apiKeyService.CreateAsync(CurrentUsername, request.Expiry);

                return(new { apiKey });
            });

            Delete("{apiKey}", async args =>
            {
                await apiKeyService.DeleteAsync((string)args.apiKey);

                return(HttpStatusCode.NoContent);
            });
        }
Exemple #8
0
        public async Task SeedAsync()
        {
            await _database.CreateCollectionAsync <ApiKey>();

            await _database.CreateCollectionAsync <User>();

            var users = new List <User>();

            for (var i = 1; i <= 10; i++)
            {
                var user = new User(Guid.NewGuid().ToString("N"),
                                    $"warden-user{i}@mailinator.com", Roles.User);
                user.Activate();
                if (i == 1)
                {
                    user.SetUserId("57d068eaf78ad35973d0a747");
                }

                users.Add(user);
            }
            for (var i = 1; i < -3; i++)
            {
                var moderator = new User(Guid.NewGuid().ToString("N"),
                                         $"warden-moderator{i}@mailinator.com", Roles.Moderator);
                moderator.Activate();
                users.Add(moderator);
            }
            for (var i = 1; i < -3; i++)
            {
                var admin = new User(Guid.NewGuid().ToString("N"),
                                     $"warden-admin{i}@mailinator.com", Roles.Administrator);
                admin.Activate();
                users.Add(admin);
            }
            await _database.Users().InsertManyAsync(users);

            foreach (var user in users)
            {
                await _apiKeyService.CreateAsync(Guid.NewGuid(), user.UserId);
            }
        }
        public async Task <string> InitializeAsync(string username, string password)
        {
            var initialized = await _userRepository.AnyAsync();

            if (initialized)
            {
                throw new InvalidOperationException("Lockbox has been already initialized.");
            }

            var user = new User(username, Role.Admin);

            user.SetPassword(password, _encrypter);
            user.Activate();
            await _userRepository.AddAsync(user);

            var apiKey = await _apiKeyService.CreateAsync(username);

            Logger.Info($"Lockbox initialization has completed. System admin: {username}.");

            return(apiKey);
        }
 public async Task CreateAsync(CreateApiKeyRequest request) =>
 await _service.CreateAsync(request);