diff --git a/lib/Controller/SAMLController.php b/lib/Controller/SAMLController.php index e289840..c0dffab 100644 --- a/lib/Controller/SAMLController.php +++ b/lib/Controller/SAMLController.php @@ -119,6 +119,8 @@ class SAMLController extends Controller { throw new \InvalidArgumentException('No valid uid given, please check your attribute mapping. Given uid: ' . $uid); } + $uid = $this->userBackend->testEncodedObjectGUID($uid); + // if this server acts as a global scale master and the user is not // a local admin of the server we just create the user and continue // no need to update additional attributes diff --git a/lib/UserBackend.php b/lib/UserBackend.php index d421002..ba0cb95 100644 --- a/lib/UserBackend.php +++ b/lib/UserBackend.php @@ -691,4 +691,46 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend { } } } + + /** + * returns the plain text UUID if the provided $uid string is a + * base64-encoded binary string representing e.g. the objectGUID. Otherwise + * + */ + public function testEncodedObjectGUID(string $uid): string { + $candidate = base64_decode($uid, true); + if($candidate === false) { + return $uid; + } + $candidate = $this->convertObjectGUID2Str($candidate); + // the regex only matches the structure of the UUID, not its semantic + // (i.e. version or variant) simply to be future compatible + if(preg_match('/^[a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8}$/i', $candidate) === 1) { + $uid = $candidate; + } + return $uid; + } + + /** + * @see \OCA\User_LDAP\Access::convertObjectGUID2Str + */ + public function convertObjectGUID2Str($oguid) { + $hex_guid = bin2hex($oguid); + $hex_guid_to_guid_str = ''; + for($k = 1; $k <= 4; ++$k) { + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); + } + $hex_guid_to_guid_str .= '-'; + for($k = 1; $k <= 2; ++$k) { + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); + } + $hex_guid_to_guid_str .= '-'; + for($k = 1; $k <= 2; ++$k) { + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); + } + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); + + return strtoupper($hex_guid_to_guid_str); + } }