Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9458: Fix creation of uuid usernames #30

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions helfi_tunnistamo.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

declare(strict_types = 1);

use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Form\FormStateInterface;
use Drupal\helfi_tunnistamo\Plugin\OpenIDConnectClient\Tunnistamo;
use Drupal\openid_connect\Entity\OpenIDConnectClientEntity;
Expand Down Expand Up @@ -75,4 +76,11 @@ function helfi_tunnistamo_openid_connect_userinfo_alter(
if (empty($userinfo['email'])) {
$userinfo['email'] = helfi_tunnistamo_create_email($userinfo);
}

// Unset preferred_username if it is a UUID so that new users get their
// name from `name` field instead.
if (!empty($userinfo['preferred_username']) && Uuid::isValid($userinfo['preferred_username'])) {
unset($userinfo['preferred_username']);
}

}
15 changes: 12 additions & 3 deletions tests/src/Kernel/UserInfoAlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function setUp() : void {
parent::setUp();

$this->installSchema('externalauth', ['authmap']);
$this->installSchema('user', ['users_data']);
$this->installConfig('user');
}

Expand Down Expand Up @@ -61,11 +62,11 @@ private function openIdConnect() : OpenIDConnect {
}

/**
* Tests authorization email fallback.
* Tests authorization email and username fallback.
*
* @dataProvider authorizationData
*/
public function testAuthorization(array $userInfo, string $expectedEmail) : void {
public function testAuthorization(array $userInfo, string $expectedEmail, string $expectedUsername) : void {
$status = $this->openIdConnect()
->completeAuthorization($this->getClientMock($userInfo), [
'access_token' => '123',
Expand All @@ -75,7 +76,9 @@ public function testAuthorization(array $userInfo, string $expectedEmail) : void
/** @var \Drupal\externalauth\Authmap $authmap */
$authmap = $this->container->get('externalauth.authmap');
$uid = $authmap->getUid($userInfo['sub'], 'openid_connect.tunnistamo');
$this->assertEquals($expectedEmail, User::load($uid)->getEmail());
$user = User::load($uid);
$this->assertEquals($expectedEmail, $user->getEmail());
$this->assertEquals($expectedUsername, $user->getAccountName());
}

/**
Expand All @@ -92,16 +95,22 @@ public function authorizationData() : array {
[
'email' => '',
'sub' => '123',
'name' => 'Cats',
'preferred_username' => '9cf5e439-529b-4d6c-b9f3-4738fb90c55f',
],
'[email protected]',
'Cats',
],
// Make sure the original email is used when set.
[
[
'email' => '[email protected]',
'sub' => '123',
'name' => 'Cats',
'preferred_username' => 'Dogs',
],
'[email protected]',
'Dogs',
],
];
}
Expand Down
Loading