openid-connect-generic icon indicating copy to clipboard operation
openid-connect-generic copied to clipboard

Azure AD B2C no user email mapped

Open KrzychuR opened this issue 5 years ago • 2 comments

Example token, decoded on https://jwt.io

image

I've tried to change Email Formatting to '{emails}' but it didn't help

KrzychuR avatar Jan 11 '20 21:01 KrzychuR

Temporary workarround:

	private function get_email_from_claim( $user_claim, $error_on_missing_key = false ) {
		if ( ! empty( $this->settings->email_format ) ) {
			if(isset($user_claim[$this->settings->email_format]) && is_array($user_claim[$this->settings->email_format]))
			{
				return $user_claim[$this->settings->email_format][0];
			}

			return $this->format_string_with_claim( $this->settings->email_format, $user_claim, $error_on_missing_key );
		}
		return null;
	}

And.. change Email Formatting (config) to 'emails'.

KrzychuR avatar Jan 11 '20 22:01 KrzychuR

Found a bit cleaner fix by hooking into the openid-connect-generic-update-user-using-current-claim hook:

/**
 * @param WP_Error|WP_User $user
 * @param $user_claim
 *
 * @return void
 */
function update_user_using_current_claim($user, $user_claim) {
	// get the user's email address from the first element of the emails claim
	if ( ! is_wp_error( $user ) && isset( $user_claim['emails'] ) && is_array( $user_claim['emails'] ) ) {
		$user_email = $user_claim['emails'][0];
		// update the user's email address
		$user->user_email = $user_email;
		// update the user's email address in the database
		$update_email = wp_update_user( $user );
		if(is_wp_error($update_email)) {
			// something went wrong, so log the error
			error_log("Error updating user email address: " . $update_email->get_error_message());
			// error code
			error_log("Error code: " . $update_email->get_error_code());
		}
		// set users role to premium member
		$user->set_role( 'premium_member' );
	}
}
add_action('openid-connect-generic-update-user-using-current-claim', 'update_user_using_current_claim', 10, 2);

ericmulder avatar Jul 05 '22 13:07 ericmulder