Skip to content

Commit

Permalink
feature #126 Allow custom table prefix (ajgarlag)
Browse files Browse the repository at this point in the history
This PR was merged into the 0.4-dev branch.

Discussion
----------

Allow custom table prefix

I need to configure the table name in order to store oauth2  data in a custom PostgreSQL schema, so I'd like to have the option to configure the table name prefix.

What do you think?

Commits
-------

c3fcb1a Allow custom table prefix
  • Loading branch information
chalasr committed Apr 23, 2023
2 parents dcdb89d + c3fcb1a commit b1036a3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ For implementation into Symfony projects, please see [bundle documentation](basi
# Name of the entity manager that you wish to use for managing clients and tokens.
entity_manager: default
# Table name prefix.
table_prefix: oauth2_
in_memory: ~
# Set a custom prefix that replaces the default 'ROLE_OAUTH2_' role prefix
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ private function createPersistenceNode(): NodeDefinition
->cannotBeEmpty()
->defaultValue('default')
->end()
->scalarNode('table_prefix')
->info('Table name prefix.')
->cannotBeEmpty()
->defaultValue('oauth2_')
->end()
->end()
->end()
// In-memory persistence
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/LeagueOAuth2ServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ private function configureDoctrinePersistence(ContainerBuilder $container, array
->findDefinition(Driver::class)
->replaceArgument(0, $config['client']['classname'])
->replaceArgument(1, $config['authorization_server']['persist_access_token'])
->replaceArgument(2, $persistenceConfig['table_prefix'])
;

$container->setParameter('league.oauth2_server.persistence.doctrine.enabled', true);
Expand Down
14 changes: 9 additions & 5 deletions src/Persistence/Mapping/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class Driver implements MappingDriver
/** @var bool */
private $persistAccessToken;

public function __construct(string $clientClass, bool $persistAccessToken)
/** @var string */
private $tablePrefix;

public function __construct(string $clientClass, bool $persistAccessToken, string $tablePrefix = 'oauth2_')
{
$this->clientClass = $clientClass;
$this->persistAccessToken = $persistAccessToken;
$this->tablePrefix = $tablePrefix;
}

public function loadMetadataForClass($className, ClassMetadata $metadata): void
Expand Down Expand Up @@ -97,7 +101,7 @@ private function buildAbstractClientMetadata(ClassMetadata $metadata): void
private function buildAccessTokenMetadata(ClassMetadata $metadata): void
{
(new ClassMetadataBuilder($metadata))
->setTable('oauth2_access_token')
->setTable($this->tablePrefix . 'access_token')
->createField('identifier', 'string')->makePrimaryKey()->length(80)->option('fixed', true)->build()
->addField('expiry', 'datetime_immutable')
->createField('userIdentifier', 'string')->length(128)->nullable(true)->build()
Expand All @@ -110,7 +114,7 @@ private function buildAccessTokenMetadata(ClassMetadata $metadata): void
private function buildAuthorizationCodeMetadata(ClassMetadata $metadata): void
{
(new ClassMetadataBuilder($metadata))
->setTable('oauth2_authorization_code')
->setTable($this->tablePrefix . 'authorization_code')
->createField('identifier', 'string')->makePrimaryKey()->length(80)->option('fixed', true)->build()
->addField('expiry', 'datetime_immutable')
->createField('userIdentifier', 'string')->length(128)->nullable(true)->build()
Expand All @@ -123,15 +127,15 @@ private function buildAuthorizationCodeMetadata(ClassMetadata $metadata): void
private function buildClientMetadata(ClassMetadata $metadata): void
{
(new ClassMetadataBuilder($metadata))
->setTable('oauth2_client')
->setTable($this->tablePrefix . 'client')
->createField('identifier', 'string')->makePrimaryKey()->length(32)->build()
;
}

private function buildRefreshTokenMetadata(ClassMetadata $metadata): void
{
$classMetadataBuilder = (new ClassMetadataBuilder($metadata))
->setTable('oauth2_refresh_token')
->setTable($this->tablePrefix . 'refresh_token')
->createField('identifier', 'string')->makePrimaryKey()->length(80)->option('fixed', true)->build()
->addField('expiry', 'datetime_immutable')
->addField('revoked', 'boolean')
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/storage/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
->args([
null,
null,
null,
])
->alias(Driver::class, 'league.oauth2_server.persistence.driver')

Expand Down

0 comments on commit b1036a3

Please sign in to comment.