Skip to content

Instantly share code, notes, and snippets.

@Maks3w
Created April 30, 2015 22:12
Show Gist options
  • Save Maks3w/dac2dc10bbbea13e507d to your computer and use it in GitHub Desktop.
Save Maks3w/dac2dc10bbbea13e507d to your computer and use it in GitHub Desktop.
ZF2 OAuth2 with JWT AcessToken response type
<?php
namespace FooOAuth2\Factory;
use FooOAuth2\Storage\ClientCredentialsStorage;
use OAuth2\GrantType\ClientCredentials;
use OAuth2\GrantType\UserCredentials;
use OAuth2\ResponseType\JwtAccessToken as JwtAccessTokenResponseType;
use OAuth2\Server as OAuth2Server;
use OAuth2\Storage\JwtAccessToken as JwtAccessTokenStorage;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZF\OAuth2\Controller\Exception\RuntimeException;
class OAuth2ServerFactoryCryptoToken implements FactoryInterface
{
public function createService(ServiceLocatorInterface $services)
{
$config = $services->get('Config');
if (!isset($config['zf-oauth2']['storage']) || empty($config['zf-oauth2']['storage'])) {
throw new RuntimeException(
'The storage configuration [\'zf-oauth2\'][\'storage\'] for OAuth2 is missing'
);
}
/** @var \OAuth2\Storage\PublicKeyInterface|\OAuth2\Storage\ScopeInterface $storage */
$storage = $services->get($config['zf-oauth2']['storage']);
$options = isset($config['zf-oauth2']['options']) ? $config['zf-oauth2']['options'] : array();
/** @var \OAuth2\Server $server */
$server = new OAuth2Server($storage, $options);
$jwtAccessStorage = new JwtAccessTokenStorage($storage);
$server->addStorage($jwtAccessStorage, 'access_token');
$jwtAccessResponseType = new JwtAccessTokenResponseType($storage, null, null, $options);
$server->addResponseType($jwtAccessResponseType);
$server->addGrantType(new UserCredentials($services->get('FooOAuth2\Factory\UserCredentialsStorageFactory')));
/** @var ClientCredentialsStorage $clientCredentialsStorage */
$clientCredentialsStorage = $services->get('FooOAuth2\Factory\ClientCredentialsStorageFactory');
$server->addGrantType(new ClientCredentials($clientCredentialsStorage, $options));
$server->addStorage($clientCredentialsStorage);
return $server;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment