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

Get user info by email. #19

Merged
merged 1 commit into from
Oct 3, 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
7 changes: 7 additions & 0 deletions src/Configuration/OAuth2Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
final class OAuth2Configuration
{
public const SSO_USER_ID_PLACEHOLDER_URL = '{userId}';
public const SSO_USER_EMAIL_PLACEHOLDER_URL = '{email}';

public function __construct(
private readonly string $ssoAccessTokenUrl,
private readonly string $ssoAuthorizeUrl,
private readonly string $ssoRedirectUrl,
private readonly string $ssoUserInfoUrl,
private readonly string $ssoUserInfoByEmailUrl,
/** @var class-string<SsoUserDto> */
private readonly string $ssoUserInfoClass,
private readonly string $ssoClientId,
Expand Down Expand Up @@ -48,6 +50,11 @@ public function getSsoUserInfoUrl(?string $userId): string
return str_replace(self::SSO_USER_ID_PLACEHOLDER_URL, $userId, $this->ssoUserInfoUrl);
}

public function getSsoUserInfoByEmailUrl(string $email): string
{
return str_replace(self::SSO_USER_EMAIL_PLACEHOLDER_URL, urlencode($email), $this->ssoUserInfoByEmailUrl);
}

/**
* @return class-string<SsoUserDto>
*/
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/AnzuSystemsAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function load(array $configs, ContainerBuilder $container): void
->setArgument('$ssoAuthorizeUrl', $oauth2Section['authorize_url'])
->setArgument('$ssoRedirectUrl', $oauth2Section['redirect_url'])
->setArgument('$ssoUserInfoUrl', $oauth2Section['user_info_url'])
->setArgument('$ssoUserInfoByEmailUrl', $oauth2Section['user_info_by_email_url'])
->setArgument('$ssoUserInfoClass', $oauth2Section['user_info_class'])
->setArgument('$ssoClientId', $oauth2Section['client_id'])
->setArgument('$ssoClientSecret', $oauth2Section['client_secret'])
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ private function addOAuth2AuthorizationSection(): NodeDefinition
OAuth2Configuration::SSO_USER_ID_PLACEHOLDER_URL,
))
->end()
->scalarNode('user_info_by_email_url')
->defaultValue('')
->info(sprintf(
'You can use placeholder "%s", which will be replaced with user email.',
OAuth2Configuration::SSO_USER_ID_PLACEHOLDER_URL,
))
->end()
->scalarNode('access_token_cache')
->cannotBeEmpty()
->defaultValue('cache.app')
Expand Down
19 changes: 19 additions & 0 deletions src/HttpClient/OAuth2HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ public function getSsoUserInfo(?string $id = null): SsoUserDto
}
}

public function getSsoUserInfoByEmail(string $email): SsoUserDto
{
try {
$response = $this->client->request(
method: Request::METHOD_GET,
url: $this->configuration->getSsoUserInfoByEmailUrl($email),
options: [
'auth_bearer' => $this->requestAccessTokenForClientService()->getAccessToken(),
]
);

return $this->serializer->deserialize($response->getContent(), $this->configuration->getSsoUserInfoClass());
} catch (ExceptionInterface $exception) {
throw UnsuccessfulUserInfoRequestException::create('User info request failed!', $exception);
} catch (SerializerException $exception) {
throw UnsuccessfulUserInfoRequestException::create('User info response deserialization failed!', $exception);
}
}

/**
* @throws UnsuccessfulAccessTokenRequestException
*
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/AnzuSystemsAuthExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ private function getFullConfig(): array
user_repository_service_id: App\Repository\UserRepository
authorize_url: 'https://example.com/authorize-url'
user_info_url: 'https://example.com/user-info-url'
user_info_by_email_url: 'https://example.com/user-info-by-email-url'
state_token_salt: 'qux-quux'
access_token_url: 'https://example.com/access-token-url'
redirect_url: 'https://example.com/redirect-url'
Expand Down
Loading