diff --git a/openid-connect-generic.php b/openid-connect-generic.php index 5035a05..8803fc6 100644 --- a/openid-connect-generic.php +++ b/openid-connect-generic.php @@ -3,7 +3,7 @@ Plugin Name: OpenID Connect - Generic Client Plugin URI: https://github.com/daggerhart/openid-connect-generic Description: Connect to an OpenID Connect identity provider with Authorization Code Flow -Version: 2.0 +Version: 2.1 Author: daggerhart Author URI: http://www.daggerhart.com License: GPLv2 Copyright (c) 2015 daggerhart @@ -288,9 +288,9 @@ class OpenID_Connect_Generic { $email = $user_identity; // allow claim details to determine username - if ( isset( $user_claim['name'] ) && isset( $user_claim['email'] ) ) { - $username = $user_claim['name']; + if ( isset( $user_claim['email'] ) ) { $email = $user_claim['email']; + $username = $this->get_username_from_claim( $user_claim ); } // if no name exists, attempt another request for userinfo else if ( isset( $token_response['access_token'] ) ) { @@ -302,13 +302,10 @@ class OpenID_Connect_Generic { } $user_claim = json_decode( $user_claim_result['body'], true ); - - if ( isset( $user_claim['name'] ) ) { - $username = $user_claim['name']; - } - + if ( isset( $user_claim['email'] ) ) { $email = $user_claim['email']; + $username = $this->get_username_from_claim( $user_claim ); } } @@ -576,6 +573,46 @@ class OpenID_Connect_Generic { function wp_logout(){ setcookie( $this->cookie_id_key , '1', 0, COOKIEPATH, COOKIE_DOMAIN, true ); } + + /** + * Avoid user_login collisions by incrementing + * + * @param $user_claim array + * @return string + */ + function get_username_from_claim( $user_claim ){ + if ( isset( $user_claim['preferred_username'] ) && !empty( $user_claim['preferred_username'] ) ) { + $desired_username = $user_claim['preferred_username']; + } + else if ( isset( $user_claim['name'] ) && !empty( $user_claim['name'] ) ) { + $desired_username = $user_claim['name']; + } + else if ( isset( $user_claim['email'] ) && !empty( $user_claim['email'] ) ) { + $tmp = explode( '@', $user_claim['email'] ); + $desired_username = $tmp[0]; + } + else { + // nothing to build a name from + return false; + } + + // normalize the data a bit + $desired_username = strtolower( preg_replace( '/[^a-zA-Z\_0-9]/', '', $desired_username ) ); + + // copy the username for incrementing + $username = $desired_username; + + // original user gets "name" + // second user gets "name2" + // etc + $count = 1; + while ( username_exists( $username ) ) { + $count++; + $username = $desired_name . $count; + } + + return $username; + } } new OpenID_Connect_Generic(); diff --git a/readme.md b/readme.md index a022d90..d2cf0fb 100644 --- a/readme.md +++ b/readme.md @@ -32,11 +32,14 @@ by this client is like so: `https://example.com/wp-admin/admin-ajax.php?action= Replace `example.com` with your domain name and path to WordPress. - ### Changelog +**2.1** + +* Working my way closer to spec. Possible breaking change. Now checking for preferred_username as priority. +* New username determination to avoid collisions + **2.0** Complete rewrite - diff --git a/readme.txt b/readme.txt index b895270..e5bb446 100644 --- a/readme.txt +++ b/readme.txt @@ -40,6 +40,11 @@ Replace `example.com` with your domain name and path to WordPress. == Changelog == += 2.1 = + +* Possible breaking change. Now checking for preferred_username as priority. +* New username determination to avoid collisions + = 2.0 = Complete rewrite of another plugin