|
6 | 6 | use Contributte\Tester\Environment; |
7 | 7 | use Contributte\Tester\Toolkit; |
8 | 8 | use League\OAuth2\Server\AuthorizationServer; |
| 9 | +use League\OAuth2\Server\Grant\AuthCodeGrant; |
| 10 | +use League\OAuth2\Server\Grant\ClientCredentialsGrant; |
| 11 | +use League\OAuth2\Server\Grant\ImplicitGrant; |
| 12 | +use League\OAuth2\Server\Grant\PasswordGrant; |
| 13 | +use League\OAuth2\Server\Grant\RefreshTokenGrant; |
9 | 14 | use League\OAuth2\Server\ResourceServer; |
10 | 15 | use Nette\DI\Compiler; |
11 | 16 | use Nette\DI\Container; |
|
15 | 20 |
|
16 | 21 | require_once __DIR__ . '/../../bootstrap.php'; |
17 | 22 |
|
| 23 | +// Test basic configuration without grants |
18 | 24 | Toolkit::test(function (): void { |
19 | 25 | $loader = new ContainerLoader(Environment::getTmpDir(), true); |
20 | 26 | $class = $loader->load(function (Compiler $compiler): void { |
|
23 | 29 | oauth2.server: |
24 | 30 | encryptionKey: "fake" |
25 | 31 | privateKey: |
26 | | - path: "../../fixtures/keys/private.key" |
| 32 | + path: "../../Fixtures/keys/private.key" |
27 | 33 | passPhrase: "foo" |
28 | 34 | permissionCheck: false |
29 | 35 | publicKey: |
30 | | - path: "../../fixtures/keys/public.key" |
| 36 | + path: "../../Fixtures/keys/public.key" |
31 | 37 | permissionCheck: false |
32 | 38 |
|
33 | 39 | services: |
|
47 | 53 | Assert::count(1, $container->findByType(ResourceServer::class)); |
48 | 54 | }); |
49 | 55 |
|
| 56 | +// Test encryption key as Statement |
50 | 57 | Toolkit::test(function (): void { |
51 | 58 | $loader = new ContainerLoader(Environment::getTmpDir(), true); |
52 | 59 | $class = $loader->load(function (Compiler $compiler): void { |
|
55 | 62 | oauth2.server: |
56 | 63 | encryptionKey: Defuse\Crypto\Key::loadFromAsciiSafeString("fake") |
57 | 64 | privateKey: |
58 | | - path: "../../fixtures/keys/private.key" |
| 65 | + path: "../../Fixtures/keys/private.key" |
59 | 66 | passPhrase: "foo" |
60 | 67 | permissionCheck: false |
61 | 68 | publicKey: |
62 | | - path: "../../fixtures/keys/public.key" |
| 69 | + path: "../../Fixtures/keys/public.key" |
63 | 70 | permissionCheck: false |
64 | 71 |
|
65 | 72 | services: |
|
78 | 85 | Assert::count(1, $container->findByType(AuthorizationServer::class)); |
79 | 86 | Assert::count(1, $container->findByType(ResourceServer::class)); |
80 | 87 | }); |
| 88 | + |
| 89 | +// Test grants with default TTL |
| 90 | +Toolkit::test(function (): void { |
| 91 | + $loader = new ContainerLoader(Environment::getTmpDir(), true); |
| 92 | + $class = $loader->load(function (Compiler $compiler): void { |
| 93 | + $compiler->addExtension('oauth2.server', new OAuth2ServerExtension()); |
| 94 | + $compiler->loadConfig(FileMock::create(' |
| 95 | + oauth2.server: |
| 96 | + encryptionKey: "fake" |
| 97 | + privateKey: |
| 98 | + path: "../../Fixtures/keys/private.key" |
| 99 | + passPhrase: "foo" |
| 100 | + permissionCheck: false |
| 101 | + publicKey: |
| 102 | + path: "../../Fixtures/keys/public.key" |
| 103 | + permissionCheck: false |
| 104 | + grants: |
| 105 | + clientCredentials: [] |
| 106 | + password: [] |
| 107 | + refreshToken: [] |
| 108 | +
|
| 109 | + services: |
| 110 | + - Tests\Fixtures\Repositories\ClientRepository |
| 111 | + - Tests\Fixtures\Repositories\AccessTokenRepository |
| 112 | + - Tests\Fixtures\Repositories\ScopeRepository |
| 113 | + - Tests\Fixtures\Repositories\AuthCodeRepository |
| 114 | + - Tests\Fixtures\Repositories\RefreshTokenRepository |
| 115 | + - Tests\Fixtures\Repositories\UserRepository |
| 116 | + ', 'neon')); |
| 117 | + }, [getmypid(), 3]); |
| 118 | + |
| 119 | + /** @var Container $container */ |
| 120 | + $container = new $class(); |
| 121 | + |
| 122 | + Assert::count(1, $container->findByType(AuthorizationServer::class)); |
| 123 | + Assert::count(1, $container->findByType(ClientCredentialsGrant::class)); |
| 124 | + Assert::count(1, $container->findByType(PasswordGrant::class)); |
| 125 | + Assert::count(1, $container->findByType(RefreshTokenGrant::class)); |
| 126 | +}); |
| 127 | + |
| 128 | +// Test grants with custom TTL (issue #14) |
| 129 | +Toolkit::test(function (): void { |
| 130 | + $loader = new ContainerLoader(Environment::getTmpDir(), true); |
| 131 | + $class = $loader->load(function (Compiler $compiler): void { |
| 132 | + $compiler->addExtension('oauth2.server', new OAuth2ServerExtension()); |
| 133 | + $compiler->loadConfig(FileMock::create(' |
| 134 | + oauth2.server: |
| 135 | + encryptionKey: "fake" |
| 136 | + privateKey: |
| 137 | + path: "../../Fixtures/keys/private.key" |
| 138 | + passPhrase: "foo" |
| 139 | + permissionCheck: false |
| 140 | + publicKey: |
| 141 | + path: "../../Fixtures/keys/public.key" |
| 142 | + permissionCheck: false |
| 143 | + grants: |
| 144 | + clientCredentials: |
| 145 | + ttl: PT30M |
| 146 | + password: |
| 147 | + ttl: PT1H |
| 148 | + refreshToken: |
| 149 | + ttl: P7D |
| 150 | +
|
| 151 | + services: |
| 152 | + - Tests\Fixtures\Repositories\ClientRepository |
| 153 | + - Tests\Fixtures\Repositories\AccessTokenRepository |
| 154 | + - Tests\Fixtures\Repositories\ScopeRepository |
| 155 | + - Tests\Fixtures\Repositories\AuthCodeRepository |
| 156 | + - Tests\Fixtures\Repositories\RefreshTokenRepository |
| 157 | + - Tests\Fixtures\Repositories\UserRepository |
| 158 | + ', 'neon')); |
| 159 | + }, [getmypid(), 4]); |
| 160 | + |
| 161 | + /** @var Container $container */ |
| 162 | + $container = new $class(); |
| 163 | + |
| 164 | + Assert::count(1, $container->findByType(AuthorizationServer::class)); |
| 165 | + Assert::count(1, $container->findByType(ClientCredentialsGrant::class)); |
| 166 | + Assert::count(1, $container->findByType(PasswordGrant::class)); |
| 167 | + Assert::count(1, $container->findByType(RefreshTokenGrant::class)); |
| 168 | +}); |
| 169 | + |
| 170 | +// Test authCode grant with all options |
| 171 | +Toolkit::test(function (): void { |
| 172 | + $loader = new ContainerLoader(Environment::getTmpDir(), true); |
| 173 | + $class = $loader->load(function (Compiler $compiler): void { |
| 174 | + $compiler->addExtension('oauth2.server', new OAuth2ServerExtension()); |
| 175 | + $compiler->loadConfig(FileMock::create(' |
| 176 | + oauth2.server: |
| 177 | + encryptionKey: "fake" |
| 178 | + privateKey: |
| 179 | + path: "../../Fixtures/keys/private.key" |
| 180 | + passPhrase: "foo" |
| 181 | + permissionCheck: false |
| 182 | + publicKey: |
| 183 | + path: "../../Fixtures/keys/public.key" |
| 184 | + permissionCheck: false |
| 185 | + grants: |
| 186 | + authCode: |
| 187 | + ttl: PT1H |
| 188 | + authCodeTTL: PT5M |
| 189 | + codeExchangeProof: true |
| 190 | +
|
| 191 | + services: |
| 192 | + - Tests\Fixtures\Repositories\ClientRepository |
| 193 | + - Tests\Fixtures\Repositories\AccessTokenRepository |
| 194 | + - Tests\Fixtures\Repositories\ScopeRepository |
| 195 | + - Tests\Fixtures\Repositories\AuthCodeRepository |
| 196 | + - Tests\Fixtures\Repositories\RefreshTokenRepository |
| 197 | + - Tests\Fixtures\Repositories\UserRepository |
| 198 | + ', 'neon')); |
| 199 | + }, [getmypid(), 5]); |
| 200 | + |
| 201 | + /** @var Container $container */ |
| 202 | + $container = new $class(); |
| 203 | + |
| 204 | + Assert::count(1, $container->findByType(AuthorizationServer::class)); |
| 205 | + Assert::count(1, $container->findByType(AuthCodeGrant::class)); |
| 206 | +}); |
| 207 | + |
| 208 | +// Test implicit grant with accessTokenTTL |
| 209 | +Toolkit::test(function (): void { |
| 210 | + $loader = new ContainerLoader(Environment::getTmpDir(), true); |
| 211 | + $class = $loader->load(function (Compiler $compiler): void { |
| 212 | + $compiler->addExtension('oauth2.server', new OAuth2ServerExtension()); |
| 213 | + $compiler->loadConfig(FileMock::create(' |
| 214 | + oauth2.server: |
| 215 | + encryptionKey: "fake" |
| 216 | + privateKey: |
| 217 | + path: "../../Fixtures/keys/private.key" |
| 218 | + passPhrase: "foo" |
| 219 | + permissionCheck: false |
| 220 | + publicKey: |
| 221 | + path: "../../Fixtures/keys/public.key" |
| 222 | + permissionCheck: false |
| 223 | + grants: |
| 224 | + implicit: |
| 225 | + ttl: PT2H |
| 226 | + accessTokenTTL: PT15M |
| 227 | +
|
| 228 | + services: |
| 229 | + - Tests\Fixtures\Repositories\ClientRepository |
| 230 | + - Tests\Fixtures\Repositories\AccessTokenRepository |
| 231 | + - Tests\Fixtures\Repositories\ScopeRepository |
| 232 | + - Tests\Fixtures\Repositories\AuthCodeRepository |
| 233 | + - Tests\Fixtures\Repositories\RefreshTokenRepository |
| 234 | + - Tests\Fixtures\Repositories\UserRepository |
| 235 | + ', 'neon')); |
| 236 | + }, [getmypid(), 6]); |
| 237 | + |
| 238 | + /** @var Container $container */ |
| 239 | + $container = new $class(); |
| 240 | + |
| 241 | + Assert::count(1, $container->findByType(AuthorizationServer::class)); |
| 242 | + Assert::count(1, $container->findByType(ImplicitGrant::class)); |
| 243 | +}); |
0 commit comments