Skip to content

Commit f48eacc

Browse files
committed
feature #26 Allow env parameter for encryption_key_type (mtarld)
This PR was merged into the 0.1-dev branch. Discussion ---------- Allow env parameter for encryption_key_type Closes #12 Commits ------- 8fa05c1 Allow env parameter for encryption_key_type
2 parents fe81c0a + 8fa05c1 commit f48eacc

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
@@ -59,9 +59,9 @@ private function createAuthorizationServerNode(): NodeDefinition
5959
->isRequired()
6060
->cannotBeEmpty()
6161
->end()
62-
->enumNode('encryption_key_type')
63-
->info("The type of value of 'encryption_key'")
64-
->values(['plain', 'defuse'])
62+
->scalarNode('encryption_key_type')
63+
->info("The type of value of 'encryption_key'\nShould be either 'plain' or 'defuse'")
64+
->cannotBeEmpty()
6565
->defaultValue('plain')
6666
->end()
6767
->scalarNode('access_token_ttl')

src/DependencyInjection/LeagueOAuth2ServerExtension.php

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

119119
private function configureAuthorizationServer(ContainerBuilder $container, array $config): void
120120
{
121+
$container->setParameter('league.oauth2_server.encryption_key', $config['encryption_key']);
122+
$container->setParameter('league.oauth2_server.encryption_key.type', $config['encryption_key_type']);
123+
121124
$authorizationServer = $container
122125
->findDefinition(AuthorizationServer::class)
123126
->replaceArgument(3, new Definition(CryptKey::class, [
@@ -126,21 +129,6 @@ private function configureAuthorizationServer(ContainerBuilder $container, array
126129
false,
127130
]));
128131

129-
if ('plain' === $config['encryption_key_type']) {
130-
$authorizationServer->replaceArgument(4, $config['encryption_key']);
131-
} elseif ('defuse' === $config['encryption_key_type']) {
132-
if (!class_exists(Key::class)) {
133-
throw new \RuntimeException('You must install the "defuse/php-encryption" package to use "encryption_key_type: defuse".');
134-
}
135-
136-
$keyDefinition = (new Definition(Key::class))
137-
->setFactory([Key::class, 'loadFromAsciiSafeString'])
138-
->addArgument($config['encryption_key']);
139-
$container->setDefinition('league.oauth2_server.defuse_key', $keyDefinition);
140-
141-
$authorizationServer->replaceArgument(4, new Reference('league.oauth2_server.defuse_key'));
142-
}
143-
144132
if ($config['enable_client_credentials_grant']) {
145133
$authorizationServer->addMethodCall('enableGrantType', [
146134
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)