Skip to content

Commit 46df3e4

Browse files
authored
adds additional documentation for jwt-auth (#116)
1 parent 73f3d8a commit 46df3e4

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

plugins/jwt-auth/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,62 @@ public function services(ContainerInterface $container): void
8989

9090
You will need to configure [CakePHP Authentication](https://book.cakephp.org/authentication/2/en/index.html) to
9191
use this library. There are several ways to do this documented in the quick start. See the
92-
[mixerapi demo](https://github.com/mixerapi/demo) for an exampe.
92+
[mixerapi demo](https://github.com/mixerapi/demo) for a complete example.
9393

9494
Be sure to load the
9595
[CakePHP Authentication.Component](https://book.cakephp.org/authentication/2/en/authentication-component.html)
9696
(generally in your AppController).
9797

98+
Here is an example that supports both HMAC and RSA with form and password based authentication. However way you
99+
implement authentication, **it is advised to use** `\MixerApi\JwtAuth\Configuration\Configuration` to pull values from
100+
your `MixerApi.JwtAuth` configuration file `config/mixerapi_jwtauth.php`. This will validate your configuration before
101+
applying it to your applications authentication.
102+
103+
```php
104+
# in src/Application.php
105+
106+
public function getAuthenticationService(ServerRequestInterface $request): \Authentication\AuthenticationServiceInterface
107+
{
108+
$fields = [
109+
\Authentication\Identifier\IdentifierInterface::CREDENTIAL_USERNAME => 'email',
110+
\Authentication\Identifier\IdentifierInterface::CREDENTIAL_PASSWORD => 'password',
111+
];
112+
113+
$config = new \MixerApi\JwtAuth\Configuration\Configuration();
114+
$service = new \Authentication\AuthenticationService();
115+
116+
$service->loadAuthenticator('Authentication.Form', [
117+
'fields' => $fields,
118+
'loginUrl' => '/admin/auth/login'
119+
]);
120+
121+
$service->loadIdentifier('Authentication.JwtSubject');
122+
123+
if (str_starts_with(haystack: $config->getAlg(), needle: 'HS')) {
124+
$service->loadAuthenticator('Authentication.Jwt', [
125+
'secretKey' => $config->getSecret(),
126+
'algorithm' => $config->getAlg(),
127+
]);
128+
} else if (str_starts_with(haystack: $config->getAlg(), needle: 'RS')) {
129+
$jsonKeySet = \Cake\Cache\Cache::remember('jwkset', function() {
130+
return json_encode((new \MixerApi\JwtAuth\Jwk\JwkSet)->getKeySet());
131+
});
132+
133+
/*
134+
* Caching is optional, you may also set the jwks key to the return value of (new JwkSet)->getKeySet()
135+
*/
136+
$service->loadAuthenticator('Authentication.Jwt', [
137+
'jwks' => json_decode($jsonKeySet, true),
138+
'algorithm' => $config->getAlg(),
139+
]);
140+
}
141+
142+
$service->loadIdentifier('Authentication.Password', ['fields' => $fields]);
143+
144+
return $service;
145+
}
146+
```
147+
98148
## Defining your JWT
99149

100150
On your User entity implement `JwtEntityInterface`. This will be used to generate the JWT, example:

0 commit comments

Comments
 (0)