Skip to content

Commit 0975ab8

Browse files
committed
fix: #225: revert autoconfiguration of League\OAuth2\Server\Grant\GrantTypeInterface
1 parent 41caa90 commit 0975ab8

9 files changed

+26
-181
lines changed

docs/implementing-custom-grant-type.md

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
```
4444

4545
1. In order to enable the new grant type in the authorization server you must register the service in the container.
46-
`\League\OAuth2\Server\Grant\GrantTypeInterface` is registered for autoconfiguration
47-
48-
- but you can manually declare the service with the `league.oauth2_server.authorization_server.grant` tag:
46+
And the service must be tagged with the `league.oauth2_server.authorization_server.grant` tag:
4947

5048
```yaml
5149
services:
@@ -55,24 +53,7 @@
5553
- {name: league.oauth2_server.authorization_server.grant}
5654
```
5755

58-
- If you prefer php configuration, you could use `AutoconfigureTag` symfony attribute for the same result :
59-
60-
```php
61-
<?php
62-
...
63-
64-
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
65-
66-
#[AutoconfigureTag(name: 'league.oauth2_server.authorization_server.grant')]
67-
final class FakeGrant extends AbstractGrant implements GrantTypeInterface
68-
{
69-
...
70-
}
71-
```
72-
73-
1. In order to defined access token TTL for your custom grant, you could:
74-
75-
- define a custom access token TTL for your grant using `accessTokenTTL` tag attribute :
56+
You could define a custom access token TTL for your grant using `accessTokenTTL` tag attribute :
7657

7758
```yaml
7859
services:
@@ -82,7 +63,7 @@
8263
- {name: league.oauth2_server.authorization_server.grant, accessTokenTTL: PT5H}
8364
```
8465

85-
- or via `AutoconfigureTag` :
66+
If you prefer php configuration, you could use `AutoconfigureTag` symfony attribute for the same result :
8667

8768
```php
8869
<?php
@@ -97,22 +78,7 @@
9778
}
9879
```
9980

100-
- and last option is usage of `\League\Bundle\OAuth2ServerBundle\Attribute\WithAccessTokenTTL` attribute (note: service must be registered with correct tag before, this is already done if autoconfiguration is activated):
101-
102-
```php
103-
<?php
104-
...
105-
106-
use League\Bundle\OAuth2ServerBundle\Attribute\WithAccessTokenTTL;
107-
108-
#[WithAccessTokenTTL(accessTokenTTL: 'PT5H')]
109-
final class FakeGrant extends AbstractGrant implements GrantTypeInterface
110-
{
111-
...
112-
}
113-
```
114-
115-
For all of these options, if `accessTokenTTL` is not defined, then bundle config is used `league_oauth2_server.authorization_server.access_token_ttl` (same as `league.oauth2_server.access_token_ttl.default` service container parameter). \
81+
If `accessTokenTTL` tag attribute is not defined, then bundle config is used `league_oauth2_server.authorization_server.access_token_ttl` (same as `league.oauth2_server.access_token_ttl.default` service container parameter). \
11682
`null` is considered as defined, to allow to unset ttl. \
11783
`league_oauth2_server.authorization_server.refresh_token_ttl` is also accessible for your implementation using `league.oauth2_server.refresh_token_ttl.default` service container parameter.
11884

src/Attribute/WithAccessTokenTTL.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/DependencyInjection/CompilerPass/GrantTypePass.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace League\Bundle\OAuth2ServerBundle\DependencyInjection\CompilerPass;
66

7-
use League\Bundle\OAuth2ServerBundle\Attribute\WithAccessTokenTTL;
87
use League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface;
98
use League\OAuth2\Server\AuthorizationServer;
109
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -28,48 +27,36 @@ public function process(ContainerBuilder $container): void
2827

2928
// enable grant type for each
3029
foreach ($taggedServices as $id => $tags) {
31-
// we only use the first tag
32-
// this allow to override autoregistration of interface to be able to set accessTokenTTL
33-
// since autoregistered tag are defined in last
34-
$attributes = array_shift($tags);
35-
30+
// skip of custom grant using \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface
31+
// since there are handled by \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantConfigurator
32+
// TODO remove code bloc when bundle interface and configurator will be deleted
3633
try {
3734
$grantDefinition = $container->findDefinition($id);
3835
/** @var class-string|null $grantClass */
3936
$grantClass = $grantDefinition->getClass();
4037
if (null !== $grantClass) {
4138
$refGrantClass = new \ReflectionClass($grantClass);
42-
43-
// skip of custom grant using \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface
44-
// since there are handled by \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantConfigurator
45-
// TODO remove code bloc when bundle interface and configurator will be deleted
4639
if ($refGrantClass->implementsInterface(GrantTypeInterface::class)) {
4740
continue;
4841
}
49-
50-
// read of accessTokenTTL from WithAccessTokenTTL attribute
51-
$withAccessTokenTTLAttributes = $refGrantClass->getAttributes(WithAccessTokenTTL::class);
52-
if (\count($withAccessTokenTTLAttributes) > 0) {
53-
// we only use first attribute, because WithAccessTokenTTL is not repeatable
54-
$withAccessTokenTTLAttributeArguments = array_shift($withAccessTokenTTLAttributes)->getArguments();
55-
$attributes['accessTokenTTL'] = array_shift($withAccessTokenTTLAttributeArguments);
56-
}
5742
}
5843
} catch (\ReflectionException) {
59-
// handling of this service as native one or without attribute
44+
// handling of this service as native one
6045
}
6146

62-
// use WithAccessTokenTTL value then accessTokenTTL tag attribute if exists, otherwise use global bundle config
63-
$accessTokenTTLValue = \array_key_exists('accessTokenTTL', $attributes)
64-
? $attributes['accessTokenTTL']
65-
: $container->getParameter('league.oauth2_server.access_token_ttl.default');
66-
67-
$definition->addMethodCall('enableGrantType', [
68-
new Reference($id),
69-
(\is_string($accessTokenTTLValue))
70-
? new Definition(\DateInterval::class, [$accessTokenTTLValue])
71-
: $accessTokenTTLValue,
72-
]);
47+
foreach ($tags as $attributes) {
48+
// use accessTokenTTL tag attribute if exists, otherwise use global bundle config
49+
$accessTokenTTLValue = \array_key_exists('accessTokenTTL', $attributes)
50+
? $attributes['accessTokenTTL']
51+
: $container->getParameter('league.oauth2_server.access_token_ttl.default');
52+
53+
$definition->addMethodCall('enableGrantType', [
54+
new Reference($id),
55+
(\is_string($accessTokenTTLValue))
56+
? new Definition(\DateInterval::class, [$accessTokenTTLValue])
57+
: $accessTokenTTLValue,
58+
]);
59+
}
7360
}
7461
}
7562
}

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use League\OAuth2\Server\CryptKey;
3232
use League\OAuth2\Server\Grant\AuthCodeGrant;
3333
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
34-
use League\OAuth2\Server\Grant\GrantTypeInterface as LeagueGrantTypeInterface;
3534
use League\OAuth2\Server\Grant\ImplicitGrant;
3635
use League\OAuth2\Server\Grant\PasswordGrant;
3736
use League\OAuth2\Server\Grant\RefreshTokenGrant;
@@ -70,9 +69,6 @@ public function load(array $configs, ContainerBuilder $container)
7069
$container->findDefinition(OAuth2Authenticator::class)
7170
->setArgument(3, $config['role_prefix']);
7271

73-
$container->registerForAutoconfiguration(LeagueGrantTypeInterface::class)
74-
->addTag('league.oauth2_server.authorization_server.grant');
75-
7672
// TODO remove code bloc when bundle interface and configurator will be deleted
7773
$container->registerForAutoconfiguration(GrantTypeInterface::class)
7874
->addTag('league.oauth2_server.authorization_server.grant');

tests/Fixtures/FakeGrantNullAccessTokenTTLWithAttribute.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/Fixtures/FakeGrantUndefinedAccessTokenTTLOnlyAutoconfigured.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/Fixtures/FakeGrantWithAttribute.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/Integration/AuthorizationServerCustomGrantTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrant;
88
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantNullAccessTokenTTL;
9-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantNullAccessTokenTTLWithAttribute;
109
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantUndefinedAccessTokenTTL;
11-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantUndefinedAccessTokenTTLOnlyAutoconfigured;
12-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantWithAttribute;
1310
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeLegacyGrant;
1411
use League\OAuth2\Server\AuthorizationServer;
1512
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -33,14 +30,10 @@ public function testAuthorizationServerHasOurCustomGrantEnabled(): void
3330
$enabledGrantTypes = $reflectionProperty->getValue($authorizationServer);
3431
$grantTypeAccessTokenTTL = $reflectionTTLProperty->getValue($authorizationServer);
3532

36-
$this->assertGrantConfig('fake_grant', new \DateInterval('PT5H'), $enabledGrantTypes, $grantTypeAccessTokenTTL, FakeGrant::class);
33+
$this->assertGrantConfig('fake_grant', new \DateInterval('PT3H'), $enabledGrantTypes, $grantTypeAccessTokenTTL, FakeGrant::class);
3734
$this->assertGrantConfig(FakeGrantNullAccessTokenTTL::class, new \DateInterval('PT1H'), $enabledGrantTypes, $grantTypeAccessTokenTTL);
3835
$this->assertGrantConfig(FakeGrantUndefinedAccessTokenTTL::class, new \DateInterval('PT2H'), $enabledGrantTypes, $grantTypeAccessTokenTTL);
3936

40-
$this->assertGrantConfig(FakeGrantWithAttribute::class, new \DateInterval('PT5H'), $enabledGrantTypes, $grantTypeAccessTokenTTL);
41-
$this->assertGrantConfig(FakeGrantNullAccessTokenTTLWithAttribute::class, new \DateInterval('PT1H'), $enabledGrantTypes, $grantTypeAccessTokenTTL);
42-
$this->assertGrantConfig(FakeGrantUndefinedAccessTokenTTLOnlyAutoconfigured::class, new \DateInterval('PT2H'), $enabledGrantTypes, $grantTypeAccessTokenTTL);
43-
4437
// TODO remove code bloc when bundle interface and configurator will be deleted
4538
$this->assertGrantConfig('fake_legacy_grant', new \DateInterval('PT5H'), $enabledGrantTypes, $grantTypeAccessTokenTTL, FakeLegacyGrant::class);
4639
}

tests/TestKernel.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeCredentialsRevoker;
1717
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrant;
1818
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantNullAccessTokenTTL;
19-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantNullAccessTokenTTLWithAttribute;
2019
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantUndefinedAccessTokenTTL;
21-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantUndefinedAccessTokenTTLOnlyAutoconfigured;
22-
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeGrantWithAttribute;
2320
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeLegacyGrant;
2421
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeRefreshTokenManager;
2522
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FixtureFactory;
@@ -252,21 +249,17 @@ private function configureCustomPersistenceServices(ContainerBuilder $container)
252249

253250
private function registerFakeGrant(ContainerBuilder $container): void
254251
{
255-
$container->register(FakeGrant::class)->setAutoconfigured(true)
256-
// tagged twice to test this case, only first one is used
252+
$container->register(FakeGrant::class)
253+
// tagged twice to test this case, last one win
257254
->addTag('league.oauth2_server.authorization_server.grant', ['accessTokenTTL' => 'PT5H'])
258255
->addTag('league.oauth2_server.authorization_server.grant', ['accessTokenTTL' => 'PT3H']);
259256

260-
$container->register(FakeGrantNullAccessTokenTTL::class)->setAutoconfigured(true)
257+
$container->register(FakeGrantNullAccessTokenTTL::class)
261258
->addTag('league.oauth2_server.authorization_server.grant', ['accessTokenTTL' => null]);
262259

263-
$container->register(FakeGrantUndefinedAccessTokenTTL::class)->setAutoconfigured(true)
260+
$container->register(FakeGrantUndefinedAccessTokenTTL::class)
264261
->addTag('league.oauth2_server.authorization_server.grant');
265262

266-
$container->register(FakeGrantWithAttribute::class)->setAutoconfigured(true);
267-
$container->register(FakeGrantNullAccessTokenTTLWithAttribute::class)->setAutoconfigured(true);
268-
$container->register(FakeGrantUndefinedAccessTokenTTLOnlyAutoconfigured::class)->setAutoconfigured(true);
269-
270263
// TODO remove line when bundle interface and configurator will be deleted
271264
$container->register(FakeLegacyGrant::class)->setAutoconfigured(true);
272265
}

0 commit comments

Comments
 (0)