OpenFlyers SSO WordPress plugin 1.2.0

Read the member name and email from a custom report (profile_report_id).
This commit is contained in:
Polyedre
2026-11-18 20:30:16 +01:00
parent c5f6232509
commit 6b54fd524b
6 changed files with 363 additions and 42 deletions

View File

@@ -3,7 +3,7 @@
* Plugin Name: OpenFlyers SSO
* Plugin URI: https://openflyers.com
* Description: Authentification des membres d'un espace prive WordPress avec leurs identifiants OpenFlyers (OAuth 2.0, mTLS et signature HTTP). Aucun mot de passe OpenFlyers n'est stocke sur le site.
* Version: 1.1.0
* Version: 1.2.0
* Author: OpenFlyers
* License: GPL-2.0-or-later
* Text Domain: openflyers-sso
@@ -19,7 +19,7 @@ if (!defined('ABSPATH')) {
exit;
}
define('OF_SSO_VERSION', '1.1.0');
define('OF_SSO_VERSION', '1.2.0');
define('OF_SSO_FILE', __FILE__);
define('OF_SSO_PATH', plugin_dir_path(__FILE__));
define('OF_SSO_OPTION', 'of_sso_settings');
@@ -75,6 +75,7 @@ function of_sso_get_settings()
'auto_create' => 0,
'default_role' => 'subscriber',
'redirect_after_login' => '',
'profile_report_id' => 0,
'show_login_button' => 1,
'debug' => 0,
);
@@ -297,11 +298,15 @@ class OF_SSO_Auth
self::STATE_TTL
);
// Reading the member's name and e-mail from a report needs the custom reports scope.
$settings = of_sso_get_settings();
$scope = empty($settings['profile_report_id']) ? 'default.login' : 'default.login reports.readonly';
$url = $client->getAuthorizeUrl(
of_sso_get_redirect_uri(),
$state,
OF_SSO_Client::computeCodeChallenge($codeVerifier),
apply_filters('of_sso_scope', 'default.login')
apply_filters('of_sso_scope', $scope)
);
wp_redirect($url);
@@ -400,9 +405,12 @@ class OF_SSO_Auth
)
);
// Extra data (name, e-mail, membership status) is not exposed by the 'default.login' scope.
// Sites needing it hook this filter and feed it from an OpenFlyers report.
$profile = apply_filters('of_sso_user_profile', array(), $ofUserId, $token, $client);
/*
* Name and e-mail are not exposed by the 'default.login' scope. They come from the report set
* in the settings, and sites needing more can still adjust them through this filter.
*/
$profile = apply_filters('of_sso_user_profile', self::fetchReportProfile($ofUserId, $token, $client), $ofUserId, $token, $client);
$profile = is_array($profile) ? $profile : array();
if (!empty($existing)) {
$user = $existing[0];
@@ -411,6 +419,8 @@ class OF_SSO_Auth
return $user;
}
$profile = self::withoutForeignEmail($profile, 0);
if (empty($settings['auto_create'])) {
OF_SSO_Logger::log('No WordPress user linked to OpenFlyers id ' . $ofUserId);
@@ -429,6 +439,8 @@ class OF_SSO_Auth
'user_pass' => wp_generate_password(32, true, true),
'user_email' => $email,
'display_name' => !empty($profile['display_name']) ? $profile['display_name'] : $login,
'first_name' => !empty($profile['first_name']) ? $profile['first_name'] : '',
'last_name' => !empty($profile['last_name']) ? $profile['last_name'] : '',
'role' => $settings['default_role'],
)
);
@@ -440,11 +452,70 @@ class OF_SSO_Auth
}
update_user_meta($userId, OF_SSO_USER_META, (string) $ofUserId);
self::applyProfile($userId, $profile);
return get_user_by('id', $userId);
}
/**
* Read the member's name and e-mail from the OpenFlyers report set in the settings.
*
* A failure never blocks the login: the member is logged in without these details.
*
* @param int $ofUserId OpenFlyers user identifier.
* @param array $token Token array.
* @param OF_SSO_Client $client Configured client.
* @return array Profile fields, empty when no report is set or when it could not be read.
*/
private static function fetchReportProfile($ofUserId, array $token, OF_SSO_Client $client)
{
$settings = of_sso_get_settings();
if (empty($settings['profile_report_id'])) {
return array();
}
$profile = $client->getMemberProfile($token, (int) $settings['profile_report_id'], $ofUserId);
if (!is_array($profile)) {
OF_SSO_Logger::log('Member report ' . (int) $settings['profile_report_id'] . ' could not be read: ' . $client->getLastError());
return array();
}
return $profile;
}
/**
* Drop an e-mail address that is invalid or already used by another WordPress account.
*
* Two accounts cannot share an address in WordPress, and an account is never linked to a member
* on the strength of its e-mail address alone.
*
* @param array $profile Profile fields.
* @param int $userId WordPress user receiving the profile, 0 for an account about to be created.
* @return array
*/
private static function withoutForeignEmail(array $profile, $userId)
{
if (empty($profile['user_email'])) {
return $profile;
}
$email = sanitize_email($profile['user_email']);
$owner = is_email($email) ? email_exists($email) : false;
if (!is_email($email) or ($owner and (int) $owner !== (int) $userId)) {
OF_SSO_Logger::log('E-mail address of the member ignored: invalid or already used by another account');
unset($profile['user_email']);
return $profile;
}
$profile['user_email'] = $email;
return $profile;
}
/**
* Copy the optional profile fields onto the WordPress user.
*
@@ -455,7 +526,7 @@ class OF_SSO_Auth
private static function applyProfile($userId, array $profile)
{
$fields = array_intersect_key(
$profile,
self::withoutForeignEmail($profile, $userId),
array_flip(array('user_email', 'display_name', 'first_name', 'last_name'))
);
@@ -464,7 +535,11 @@ class OF_SSO_Auth
}
$fields['ID'] = $userId;
wp_update_user($fields);
$result = wp_update_user($fields);
if (is_wp_error($result)) {
OF_SSO_Logger::log('Profile update failed for user ' . (int) $userId . ': ' . $result->get_error_message());
}
}
/**