Skip to content

Commit 9fdbce4

Browse files
committed
Add factory to create an Authorization Server.
1 parent 3b5e5b0 commit 9fdbce4

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Auth\Factory;
4+
5+
use League\OAuth2\Server\AuthorizationServer;
6+
use Pdsinterop\Solid\Auth\Config;
7+
use Pdsinterop\Solid\Auth\Enum\Repository;
8+
use Pdsinterop\Solid\Auth\Repository\Client;
9+
10+
class AuthorizationServerFactory
11+
{
12+
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
13+
14+
/** @var Config */
15+
private $config;
16+
17+
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
18+
19+
final public function __construct(Config $config)
20+
{
21+
$this->config = $config;
22+
}
23+
24+
final public function create() : AuthorizationServer
25+
{
26+
$config = $this->config;
27+
28+
$clientIdentifier = $config->getClient()->getIdentifier();
29+
$clientSecret = $config->getClient()->getSecret();
30+
$expiration = $config->getExpiration();
31+
$grantTypes = $config->getGrantTypes();
32+
$keys = $config->getKeys();
33+
34+
$repositoryFactory = new RepositoryFactory([
35+
Repository::CLIENT => new Client($clientIdentifier, $clientSecret, '',$grantTypes, []),
36+
]);
37+
38+
$server = new AuthorizationServer(
39+
$repositoryFactory->createClientRepository(),
40+
$repositoryFactory->createAccessTokenRepository(),
41+
$repositoryFactory->createScopeRepository(),
42+
$keys->getPrivateKey(),
43+
$keys->getEncryptionKey()
44+
);
45+
46+
$grantTypeFactory = new GrantTypeFactory($expiration, $repositoryFactory);
47+
48+
array_walk($grantTypes, static function ($grantType) use ($expiration, $grantTypeFactory, $server) {
49+
$grant = $grantTypeFactory->createGrantType($grantType);
50+
51+
$server->enableGrantType(
52+
$grant,
53+
$expiration->forAccessToken()
54+
);
55+
});
56+
57+
return $server;
58+
}
59+
}

0 commit comments

Comments
 (0)