Skip to content

Commit 8fa05c1

Browse files
committed
Allow env parameter for encryption_key_type
1 parent 534714c commit 8fa05c1

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\Bundle\OAuth2ServerBundle\DependencyInjection\CompilerPass;
6+
7+
use Defuse\Crypto\Key;
8+
use League\OAuth2\Server\AuthorizationServer;
9+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10+
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\DependencyInjection\Definition;
12+
use Symfony\Component\DependencyInjection\Reference;
13+
14+
/**
15+
* @author Mathias Arlaud <[email protected]>
16+
*/
17+
class EncryptionKeyPass implements CompilerPassInterface
18+
{
19+
public function process(ContainerBuilder $container): void
20+
{
21+
$encryptionKey = $container->getParameter('league.oauth2_server.encryption_key');
22+
$encryptionKeyType = $container->resolveEnvPlaceholders($container->getParameter('league.oauth2_server.encryption_key.type'), true);
23+
$authorizationServer = $container->findDefinition(AuthorizationServer::class);
24+
25+
if ('plain' === $encryptionKeyType) {
26+
$authorizationServer->replaceArgument(4, $encryptionKey);
27+
28+
return;
29+
}
30+
31+
if ('defuse' === $encryptionKeyType) {
32+
if (!class_exists(Key::class)) {
33+
throw new \RuntimeException('You must install the "defuse/php-encryption" package to use "encryption_key_type: defuse".');
34+
}
35+
36+
$keyDefinition = (new Definition(Key::class))
37+
->setFactory([Key::class, 'loadFromAsciiSafeString'])
38+
->addArgument($encryptionKey);
39+
40+
$container->setDefinition('league.oauth2_server.defuse_key', $keyDefinition);
41+
42+
$authorizationServer->replaceArgument(4, new Reference('league.oauth2_server.defuse_key'));
43+
44+
return;
45+
}
46+
47+
throw new \RuntimeException(sprintf('The value "%s" is not allowed for path "league_oauth2_server.authorization_server.encryption_key_type". Permissible values: "plain", "defuse"', $encryptionKeyType));
48+
}
49+
}

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ private function createAuthorizationServerNode(): NodeDefinition
6363
->isRequired()
6464
->cannotBeEmpty()
6565
->end()
66-
->enumNode('encryption_key_type')
67-
->info("The type of value of 'encryption_key'")
68-
->values(['plain', 'defuse'])
66+
->scalarNode('encryption_key_type')
67+
->info("The type of value of 'encryption_key'\nShould be either 'plain' or 'defuse'")
68+
->cannotBeEmpty()
6969
->defaultValue('plain')
7070
->end()
7171
->scalarNode('access_token_ttl')

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ private function assertRequiredBundlesAreEnabled(ContainerBuilder $container): v
127127

128128
private function configureAuthorizationServer(ContainerBuilder $container, array $config): void
129129
{
130+
$container->setParameter('league.oauth2_server.encryption_key', $config['encryption_key']);
131+
$container->setParameter('league.oauth2_server.encryption_key.type', $config['encryption_key_type']);
132+
130133
$authorizationServer = $container
131134
->findDefinition(AuthorizationServer::class)
132135
->replaceArgument(3, new Definition(CryptKey::class, [
@@ -135,21 +138,6 @@ private function configureAuthorizationServer(ContainerBuilder $container, array
135138
false,
136139
]));
137140

138-
if ('plain' === $config['encryption_key_type']) {
139-
$authorizationServer->replaceArgument(4, $config['encryption_key']);
140-
} elseif ('defuse' === $config['encryption_key_type']) {
141-
if (!class_exists(Key::class)) {
142-
throw new \RuntimeException('You must install the "defuse/php-encryption" package to use "encryption_key_type: defuse".');
143-
}
144-
145-
$keyDefinition = (new Definition(Key::class))
146-
->setFactory([Key::class, 'loadFromAsciiSafeString'])
147-
->addArgument($config['encryption_key']);
148-
$container->setDefinition('league.oauth2_server.defuse_key', $keyDefinition);
149-
150-
$authorizationServer->replaceArgument(4, new Reference('league.oauth2_server.defuse_key'));
151-
}
152-
153141
if ($config['enable_client_credentials_grant']) {
154142
$authorizationServer->addMethodCall('enableGrantType', [
155143
new Reference(ClientCredentialsGrant::class),

src/LeagueOAuth2ServerBundle.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace League\Bundle\OAuth2ServerBundle;
66

77
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
8+
use League\Bundle\OAuth2ServerBundle\DependencyInjection\CompilerPass\EncryptionKeyPass;
89
use League\Bundle\OAuth2ServerBundle\DependencyInjection\LeagueOAuth2ServerExtension;
910
use League\Bundle\OAuth2ServerBundle\DependencyInjection\Security\OAuth2Factory;
1011
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
@@ -60,5 +61,7 @@ private function configureDoctrineExtension(ContainerBuilder $container): void
6061
]
6162
)
6263
);
64+
65+
$container->addCompilerPass(new EncryptionKeyPass());
6366
}
6467
}

0 commit comments

Comments
 (0)