From 1de016485b48f2253ce77f1eaafa9f93573fc4f1 Mon Sep 17 00:00:00 2001 From: Robert Staddon Date: Thu, 12 May 2016 19:19:03 -0500 Subject: [PATCH] Allow users with existing WordPress accounts to sign in An error occurs if a user with an existing WordPress account tries to sign in using OpenID Connect. This patch fixes this problem by adding the OpenID Connect meta data to the existing user's account after successful authorization. --- .../openid-connect-generic-client-wrapper.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/includes/openid-connect-generic-client-wrapper.php b/includes/openid-connect-generic-client-wrapper.php index f96bd67..7f8133c 100644 --- a/includes/openid-connect-generic-client-wrapper.php +++ b/includes/openid-connect-generic-client-wrapper.php @@ -412,6 +412,11 @@ class OpenID_Connect_Generic_Client_Wrapper { $username = $this->get_username_from_claim( $user_claim ); } + // Before trying to create the user, first check if a user with the same email already exists + if( $uid = email_exists( $email ) ) { + return $this->update_existing_user( $uid, $subject_identity ); + } + // allow other plugins / themes to determine authorization // of new accounts based on the returned user claim $create_user = apply_filters( 'openid-connect-generic-user-creation-test', TRUE, $user_claim ); @@ -443,4 +448,25 @@ class OpenID_Connect_Generic_Client_Wrapper { return $user; } + + + /** + * Update an existing user with OpenID Connect meta data + * + * @param $uid + * @param $subject_identity + * + * @return \WP_Error | \WP_User + */ + function update_existing_user( $uid, $subject_identity ) { + // add the OpenID Connect meta data + add_user_meta( $uid, 'openid-connect-generic-user', TRUE, TRUE ); + add_user_meta( $uid, 'openid-connect-generic-subject-identity', (string) $subject_identity, TRUE ); + + // allow plugins / themes to take action on user update + do_action( 'openid-connect-generic-user-update', $uid ); + + // return our updated user + return get_user_by( 'id', $uid ); + } }