Skip to content

Commit

Permalink
Merge pull request #19 from anzusystems/feature_user_info_by_email
Browse files Browse the repository at this point in the history
Get user info by email.
  • Loading branch information
TomasHermanek authored Oct 3, 2024
2 parents 9c55a50 + 53854b6 commit 4e52abe
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
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

0 comments on commit 4e52abe

Please sign in to comment.