daniel-frak / keycloak-user-migration

A Keycloak plugin for migrating users from legacy systems
MIT License
271 stars 127 forks source link

rest api doesnt import wp users #159

Open warioishere opened 1 month ago

warioishere commented 1 month ago

Hello again. sorry for spamming this repo but still need some help. I managed to install the plugin. But it seems the plugin doesnt import my wp users.

I used this rest point

/* Create Custom Endpoint */

add_action('rest_api_init', 'create_keycloak_endpoints');

function create_keycloak_endpoints() {
    register_rest_route(
        'wp/v2',
        '/keycloak-user-migration/(?P<username>.+)',
        [
            'methods' => 'GET',
            'callback' => 'keycloak_get',
        ]
    );
    register_rest_route(
        'wp/v2',
        '/keycloak-user-migration/(?P<username>.+)',
        [
            'methods' => 'POST',
            'callback' => 'keycloak_post',
        ]
    );
}

function keycloak_get($request) {
        $username = $request['username'];
        $user = get_user_by('email', $username);
        if (!$user) {
                $user = get_user_by('login', $username);
        }
        if (!$user) {
                write_log('not found '.$username);
                return new WP_REST_Response(['message' => 'not found '.$username], 404);
        }
    return [
                'id' => $user->ID,
                'username' => $user->user_login,
                'email' => $user->user_email,
        'firstName' => $user->user_firstname,
        'lastName' => $user->user_lastname,
                'enabled' => true,
                'emailVerified' => true,
        ];
}

function keycloak_post($request) {
        $username = $request['username'];
        $password = $request['password'];
        $user = get_user_by('login', $username);
        if (!$user) {
                $user = get_user_by('email', $username);
        }
        if (!$user || !wp_check_password($password, $user->user_pass, $user->ID)) {
                write_log('wrong_password for '.$username);
                return new WP_REST_Response(['message' => 'wrong_password for '.$username], 404);
        }
        return true;
}

if (!function_exists('write_log')) {
    function write_log($log)  {
        if (is_array($log) || is_object($log)) {
            error_log(print_r($log, true));
        } else {
            error_log($log);
        }
    }
}

and I can also call it with `curl -X GET "https://mydomain.com/wp-json/wp/v2/keycloak-user-migration/testuser" which gives me a correct response.

But using the url in the plugin: https://mydomain.com/wp-json/wp/v2/keycloak-user-migration with and without /doesnt work.

I dont get any logs on keycloak, nor on wp if there is errors during calls. When I try to syncronize, it says skipped syncronziation as synchronisation is beeing processed

What am I doing wrong again? Any hints?

daniel-frak commented 1 month ago

Hello!

Some things to try:

A lack of logs on Keycloak's side may indicate that the user is just not passing password verification (perhaps because of the POST endpoint not being configured correctly).