Skip to content

Commit ad81602

Browse files
committed
Service builders and changes for v0.2.0
1 parent 79d24d5 commit ad81602

33 files changed

+420
-230
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ matrix:
3232
- php: 7.3
3333
env:
3434
- DEPS=latest
35-
- php: 7.4snapshot
35+
- php: 7.4
3636
env:
3737
- DEPS=lowest
38-
- php: 7.4snapshot
38+
- php: 7.4
3939
env:
4040
- DEPS=latest
4141
- php: nightly
@@ -46,8 +46,6 @@ matrix:
4646
- DEPS=latest
4747

4848
allow_failures:
49-
- php: 7.4snapshot
50-
- php: 7.4snapshot
5149
- php: nightly
5250
- php: nightly
5351

README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ If you need other algorithms you should install it manually.
5656

5757
For a basic usage you shouldn't require any other dependency package.
5858

59+
Every builder have methods to customize instances with other dependencies.
60+
5961
```php
6062

6163
use Facile\OpenIDClient\Client\ClientBuilder;
6264
use Facile\OpenIDClient\Issuer\IssuerBuilder;
6365
use Facile\OpenIDClient\Client\Metadata\ClientMetadata;
64-
use Facile\OpenIDClient\Service\AuthorizationService;
65-
use Facile\OpenIDClient\Service\UserinfoService;
66+
use Facile\OpenIDClient\Service\Builder\AuthorizationServiceBuilder;
67+
use Facile\OpenIDClient\Service\Builder\UserInfoServiceBuilder;
6668
use Psr\Http\Message\ServerRequestInterface;
6769

6870
$issuer = (new IssuerBuilder())
6971
->build('https://example.com/.well-known/openid-configuration');
7072
$clientMetadata = ClientMetadata::fromArray([
71-
'client_id' => 'client-id',
73+
'client_id' => 'client-id',
74+
'client_secret' => 'my-client-secret',
75+
'token_endpoint_auth_method' => 'client_secret_basic', // the auth method tor the token endpoint
7276
'redirect_uris' => [
7377
'https://my-rp.com/callback',
7478
],
@@ -80,7 +84,7 @@ $client = (new ClientBuilder())
8084

8185
// Authorization
8286

83-
$authorizationService = new AuthorizationService();
87+
$authorizationService = (new AuthorizationServiceBuilder())->build();
8488
$redirectAuthorizationUri = $authorizationService->getAuthorizationUri(
8589
$client,
8690
['login_hint' => 'user_username'] // custom params
@@ -107,20 +111,22 @@ $tokenSet = $authorizationService->refresh($client, $tokenSet->getRefreshToken()
107111

108112

109113
// Get user info
110-
111-
$userinfoService = new UserinfoService();
112-
$userinfo = $userinfoService->getUserInfo($client, $tokenSet);
114+
$userInfoService = (new UserInfoServiceBuilder())->build();
115+
$userInfo = $userInfoService->getUserInfo($client, $tokenSet);
113116

114117
```
115118

116119

117120
## Client registration
118121

122+
See [OpenID Connect Dynamic Client Registration 1.0](https://openid.net/specs/openid-connect-registration-1_0.html)
123+
and [RFC7591 OAuth 2.0 Dynamic Client Registration Protocol](https://tools.ietf.org/html/rfc7591).
124+
119125
```php
120126

121-
use Facile\OpenIDClient\Service\RegistrationService;
127+
use Facile\OpenIDClient\Service\Builder\RegistrationServiceBuilder;
122128

123-
$registration = new RegistrationService();
129+
$registration = (new RegistrationServiceBuilder())->build();
124130

125131
// registration
126132
$metadata = $registration->register(
@@ -152,29 +158,33 @@ $registration->delete($metadata['registration_client_uri'], $metadata['registrat
152158

153159
## Token Introspection
154160

161+
See [RFC7662 - OAuth 2.0 Token Introspection](https://tools.ietf.org/html/rfc7662).
162+
155163
```php
156-
use Facile\OpenIDClient\Service\IntrospectionService;
164+
use Facile\OpenIDClient\Service\Builder\IntrospectionServiceBuilder;
157165

158-
$service = new IntrospectionService();
166+
$service = (new IntrospectionServiceBuilder())->build();
159167

160168
$params = $service->introspect($client, $token);
161169
```
162170

163171

164172
## Token Revocation
165173

174+
See [RFC7009 - OAuth 2.0 Token Revocation](https://tools.ietf.org/html/rfc7009).
175+
166176
```php
167-
use Facile\OpenIDClient\Service\RevocationService;
177+
use Facile\OpenIDClient\Service\Builder\RevocationServiceBuilder;
168178

169-
$service = new RevocationService();
179+
$service = (new RevocationServiceBuilder())->build();
170180

171181
$params = $service->revoke($client, $token);
172182
```
173183

174184

175185
## Request Object
176186

177-
You can create a request object authorization request with the
187+
You can create a [request object](https://openid.net/specs/openid-connect-core-1_0.html#RequestUriParameter) authorization request with the
178188
`Facile\OpenIDClient\RequestObject\RequestObjectFactory` class.
179189

180190
This will create a signed (and optionally encrypted) JWT token based on
@@ -184,7 +194,7 @@ your client metadata.
184194
use Facile\OpenIDClient\RequestObject\RequestObjectFactory;
185195

186196
$factory = new RequestObjectFactory();
187-
$requestObject = $factory->create($client, [/* custom params to include in the JWT*/]);
197+
$requestObject = $factory->create($client, [/* custom claims to include in the JWT*/]);
188198
```
189199

190200
Then you can use it to create the AuthRequest:
@@ -202,18 +212,18 @@ $authRequest = AuthRequest::fromParams([
202212

203213
## Aggregated and Distributed Claims
204214

205-
The library can handle aggregated and distributed claims:
215+
The library can handle [aggregated and distributed claims](https://openid.net/specs/openid-connect-core-1_0.html#AggregatedDistributedClaims):
206216

207217
```php
208218
use Facile\OpenIDClient\Claims\AggregateParser;
209219
use Facile\OpenIDClient\Claims\DistributedParser;
210220

211221
$aggregatedParser = new AggregateParser();
212222

213-
$claims = $aggregatedParser->unpack($client, $userinfo);
223+
$claims = $aggregatedParser->unpack($client, $userInfo);
214224

215225
$distributedParser = new DistributedParser();
216-
$claims = $distributedParser->fetch($client, $userinfo);
226+
$claims = $distributedParser->fetch($client, $userInfo);
217227
````
218228

219229

@@ -224,7 +234,7 @@ There are some middlewares and handles available:
224234
### SessionCookieMiddleware
225235

226236
This middleware should always be on top of middlewares chain to provide
227-
a cookie session for `state` and `nonce` parameters.
237+
a session for `state` and `nonce` parameters.
228238

229239
To use it you should install the `dflydev/fig-cookies` package:
230240

@@ -234,8 +244,11 @@ $ composer require "dflydev/fig-cookies:^2.0"
234244
235245
```php
236246
use Facile\OpenIDClient\Middleware\SessionCookieMiddleware;
247+
use Psr\SimpleCache\CacheInterface;
237248
238-
$middleware = new SessionCookieMiddleware();
249+
// Use your PSR-16 simple-cache implementation to persist sessions
250+
/** @var CacheInterface $cache */
251+
$middleware = new SessionCookieMiddleware($cache/* , $cookieName = "openid", $ttl = 300 */);
239252
```
240253

241254
The middleware provides a `Facile\OpenIDClient\Session\AuthSessionInterface`
@@ -314,24 +327,26 @@ with user infos as array.
314327

315328
```php
316329
use Facile\OpenIDClient\Middleware\UserInfoMiddleware;
317-
use Facile\OpenIDClient\Service\UserinfoService;
330+
use Facile\OpenIDClient\Service\UserInfoService;
318331

319-
/** @var UserinfoService $userinfoService */
320-
$userinfoService = $container->get(UserinfoService::class);
321-
$middleware = new UserInfoMiddleware($userinfoService);
332+
/** @var UserInfoService $userInfoService */
333+
$userInfoService = $container->get(UserInfoService::class);
334+
$middleware = new UserInfoMiddleware($userInfoService);
322335
```
323336

324337

325338
## Performance improvements for production environment
326339

340+
It's important to use a cache to avoid to fetch issuer configuration and keys on every request.
341+
327342
```php
328343
use Psr\SimpleCache\CacheInterface;
329344
use Facile\OpenIDClient\Issuer\IssuerBuilder;
330345
use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder;
331346
use Facile\JoseVerifier\JWK\JwksProviderBuilder;
332347

333348
/** @var CacheInterface $cache */
334-
$cache = $container->get('my-cache-implementation');
349+
$cache = $container->get(CacheInterface::class); // get your simple-cache implementation
335350

336351
$metadataProviderBuilder = (new MetadataProviderBuilder())
337352
->setCache($cache)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"web-token/jwt-signature-algorithm-experimental": "^2.0",
9595
"web-token/jwt-signature-algorithm-hmac": "^2.0",
9696
"web-token/jwt-signature-algorithm-none": "^2.0",
97-
"web-token/jwt-signature-algorithm-rsa": "^2.0",
9897
"laminas/laminas-config-aggregator": "^1.1",
9998
"laminas/laminas-servicemanager": "^3.4",
10099
"laminas/laminas-component-installer": "^2.1",
@@ -105,6 +104,7 @@
105104
"symfony/console": "^4.3 || ^5.0"
106105
},
107106
"suggest": {
107+
"facile-it/php-oauth2-http-client": "To use a PSR HTTP client to interact with OAuth2/OpenID protected APIs",
108108
"web-token/jwt-signature-algorithm-hmac": "To use the client_secret_jwt auth method and symmetric key signature",
109109
"dflydev/fig-cookies": "To use the SessionCookieMiddleware"
110110
},

0 commit comments

Comments
 (0)