Skip to content

Commit

Permalink
Added optional option tenantId for Azure authorizator and compiler …
Browse files Browse the repository at this point in the history
…extension
  • Loading branch information
tg666 committed Jan 29, 2024
1 parent 7ccf38c commit 6d8846e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extensions:
enabled: true # default, not necessary to define
clientId: '<client id>'
clientSecret: '<client id>'
tenantId: '<tenant id>' # optional, use this option only if your Azure Entra ID application is configured as a single tenant.
options: [] # additional options that are passed into the client
authenticator: App\OAuth\AzureAuthenticator
```
Expand Down
9 changes: 9 additions & 0 deletions src/Authorization/Azure/AzureAuthorizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class AzureAuthorizator extends AbstractAuthorizator
{
public const OptClientId = 'clientId';
public const OptClientSecret = 'clientSecret';
public const OptTenantId = 'tenantId';
public const OptOptions = 'options';

protected function createClient(ConfigInterface $config): AbstractProvider
Expand All @@ -35,6 +36,14 @@ protected function createClient(ConfigInterface $config): AbstractProvider
$baseGraphUri = $client->getRootMicrosoftGraphUri(null);
$client->scope = 'openid profile email offline_access ' . $baseGraphUri . '/User.Read';

if ($config->has(self::OptTenantId)) {
$tenantId = (string) $config->get(self::OptTenantId);

if ('' !== $tenantId) {
$client->tenant = $tenantId;
}
}

return $client;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Bridge/Nette/DI/AzureOAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ protected function getFlowConfigOptions(): array
AzureAuthorizator::OptClientSecret => Expect::string()
->required()
->dynamic(),
AzureAuthorizator::OptTenantId => Expect::string()
->nullable()
->dynamic(),
AzureAuthorizator::OptOptions => Expect::array(),
];
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Bridge/Nette/DI/AzureOAuthExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ final class AzureOAuthExtensionTest extends TestCase
AzureAuthorizator::OptClientSecret => 'secret',
],
],
'with tenant id' => [
__DIR__ . '/config/azure/config.withTenantId.neon',
'azure',
true,
AuthenticatorFixture::class,
Config::class,
[
AzureAuthorizator::OptClientId => 'client',
AzureAuthorizator::OptClientSecret => 'secret',
AzureAuthorizator::OptTenantId => '123',
],
],
'config as statement' => [
__DIR__ . '/config/azure/config.configAsStatement.neon',
'azure',
Expand Down
10 changes: 10 additions & 0 deletions tests/Bridge/Nette/DI/config/azure/config.withTenantId.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extensions:
oauth: SixtyEightPublishers\OAuth\Bridge\Nette\DI\OAuthExtension
oauth.azure: SixtyEightPublishers\OAuth\Bridge\Nette\DI\AzureOAuthExtension

oauth.azure:
config:
clientId: client
clientSecret: secret
tenantId: '123'
authenticator: SixtyEightPublishers\OAuth\Tests\Fixtures\AuthenticatorFixture

0 comments on commit 6d8846e

Please sign in to comment.