This is working to sync SSO records when I run it in my development environment. I’m testing this inside of a class in the wp-discourse plugin. $this->options[]
is from wp-discourse. You could just hard-code the values. I’ll look at it some more tomorrow.
Class DiscourseSSO {
// Add the action hook
public function __construct() {
add_action( 'profile_update', array( $this, 'sync_sso' ), 10, 2 );
}
public function sync_sso( $user_id, $old_data ) {
$user = get_user_by( 'id', $user_id );
if ( $user->user_email !== $old_data->user_email ) {
$url = $this->options['url'] . '/admin/users/sync_sso';
$api_key = $this->options['api-key'];
$api_username = $this->options['publish-username'];
$sso_secret = $this->options['sso-secret'];
$params = array(
'username' => $user->user_login,
'email' => $user->user_email,
'external_id' => $user_id,
);
// base64 encode the SSO params.
$sso_user = base64_encode( http_build_query( $params ) );
// Create the signature for Discourse to match against the payload.
$sig = hash_hmac( 'sha256', $sso_user, $sso_secret );
$response = wp_remote_post( esc_url_raw( $url ), array(
'body' => array(
'sso' => $sso_user,
'sig' => $sig,
'api_key' => $api_key,
'api_username' => $api_username,
),
) );
$response = json_decode( wp_remote_retrieve_body( $response ) );
// Do something with the response.
}
}
}