Skip to main content

Dbal Storage for OAuth 2 Server

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· One min read
Stephan Hochdörfer
Head of IT Business Operations

Recently I experimented a bit with OAuth 2.0 Server because I wanted to protect the API backend for the unKonf website using OAuth2 - mostly to see how easily I can integrate it with our Adroit middleware. While integrating OAuth2 Server and Adroit was fairly trivial, I was missing a Doctrine storage provider for OAuth 2.0 Server. Since I could not even find a package on packagist I thought about writing one myself. You can find the bitexpert/oauth2-server-storage-dbal package on Github. It can be installed it via Composer:

composer.phar require bitexpert/oauth2-server-storage-dbal

To use it create a \Doctrine\DBAL\Connection instance or grab the connection instance from \Doctrine\ORM\EntityManager and simply pass it to the different storage classes:

/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = ...
$connection = $entityManager->getConnection();

$server = new \League\OAuth2\Server\AuthorizationServer();
$server->setSessionStorage(
new \bitExpert\\OAuth2\Server\Storage\Dbal\SessionStorage($connection)
);
$server->setAccessTokenStorage(
new \bitExpert\OAuth2\Server\Storage\Dbal\AccessTokenStorage($connection)
);
$server->setClientStorage(
new \bitExpert\OAuth2\Server\Storage\Dbal\ClientStorage($connection)
);
$server->setScopeStorage(
new \bitExpert\OAuth2\Server\Storage\Dbal\ScopeStorage($connection)
);

I have also included the required database schema in the scripts/setup.php file.