public function registerAuthenticationRecord(User $user, string $username, string $password): AuthenticationRecordInterface
    {
        if (!$user->getId()) {
            throw new PersistedUserRequiredException("Your user must have an ID before you can create auth records with it");
        }

        if ($this->authenticationProvider->findByUsername($username)) {
            throw new UsernameTakenException();
        }

        if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
            if ($user->getEmail() !== $username) {
                throw new MismatchedEmailsException();
            }

            if ($emailUser = $this->userProvider->findByEmail($username)) {
                if ($emailUser !== $user) {
                    throw new EmailUsernameTakenException();
                }
            }
        }

        $hash = password_hash($password, PASSWORD_DEFAULT);
        $auth = $this->authenticationProvider->create(
            $user->getId(),
            $username,
            $hash,
            KeyFactory::generateEncryptionKey()->getRawKeyMaterial()
        );
        $this->authenticationProvider->save($auth);

        return $auth;
    }