1414use League \OAuth2 \Server \RequestTypes \AuthorizationRequestInterface as OAuth2AuthorizationRequestInterface ;
1515use League \OAuth2 \Server \ResponseTypes \ResponseTypeInterface ;
1616use LogicException ;
17+ use Psr \Http \Message \ResponseInterface ;
1718use Psr \Http \Message \ServerRequestInterface ;
1819use SimpleSAML \Error \BadRequest ;
20+ use SimpleSAML \Module \oidc \Factories \Entities \PushedAuthorizationRequestEntityFactory ;
21+ use SimpleSAML \Module \oidc \Repositories \PushedAuthorizationRequestRepository ;
1922use SimpleSAML \Module \oidc \Server \Exceptions \OidcServerException ;
2023use SimpleSAML \Module \oidc \Server \Grants \Interfaces \AuthorizationValidatableWithRequestRules ;
24+ use SimpleSAML \Module \oidc \Server \RequestRules \Interfaces \ResultBagInterface ;
2125use SimpleSAML \Module \oidc \Server \RequestRules \RequestRulesManager ;
2226use SimpleSAML \Module \oidc \Server \RequestRules \Rules \ClientRedirectUriRule ;
2327use SimpleSAML \Module \oidc \Server \RequestRules \Rules \ClientRule ;
2731use SimpleSAML \Module \oidc \Server \RequestRules \Rules \ResponseModeRule ;
2832use SimpleSAML \Module \oidc \Server \RequestRules \Rules \StateRule ;
2933use SimpleSAML \Module \oidc \Server \RequestRules \Rules \UiLocalesRule ;
34+ use SimpleSAML \Module \oidc \Server \RequestTypes \AuthorizationRequest ;
3035use SimpleSAML \Module \oidc \Server \RequestTypes \LogoutRequest ;
3136use SimpleSAML \Module \oidc \Server \ResponseModes \QueryResponseMode ;
3237use SimpleSAML \Module \oidc \Services \LoggerService ;
@@ -61,6 +66,7 @@ public function __construct(
6166 ?ResponseTypeInterface $ responseType = null ,
6267 ?RequestRulesManager $ requestRulesManager = null ,
6368 protected readonly ?LoggerService $ loggerService = null ,
69+ protected readonly ?PushedAuthorizationRequestRepository $ pushedAuthorizationRequestRepository = null ,
6470 ) {
6571 parent ::__construct (
6672 $ clientRepository ,
@@ -153,7 +159,10 @@ public function validateAuthorizationRequest(ServerRequestInterface $request): O
153159 ),
154160 );
155161
156- return $ grantType ->validateAuthorizationRequestWithRequestRules ($ request , $ resultBag );
162+ $ authorizationRequest = $ grantType ->validateAuthorizationRequestWithRequestRules ($ request , $ resultBag );
163+ $ this ->bindPushedAuthorizationRequestUri ($ authorizationRequest , $ resultBag );
164+
165+ return $ authorizationRequest ;
157166 } else {
158167 $ this ->loggerService ?->debug(
159168 'AuthorizationServer: Grant type can NOT respond to ' .
@@ -171,6 +180,94 @@ public function validateAuthorizationRequest(ServerRequestInterface $request): O
171180 }
172181
173182
183+ /**
184+ * Consume the Pushed Authorization Request the request was made with, if any, before the grant issues the
185+ * authorization response.
186+ *
187+ * A PAR request_uri is one-time use (RFC 9126 section 4), but it can not be consumed when the request is
188+ * validated: the user agent comes back to the authorization endpoint with the same request_uri after the
189+ * login, and that request is validated again. RFC 9126 allows a request_uri to be used more than once that
190+ * way. It is consumed here instead, atomically, so one request_uri still yields at most one response, and a
191+ * second one, from another tab or a replay, is refused the way RequestUriRule refuses a consumed one.
192+ *
193+ * @inheritDoc
194+ * @throws \SimpleSAML\Error\BadRequest
195+ * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
196+ */
197+ public function completeAuthorizationRequest (
198+ OAuth2AuthorizationRequestInterface $ authRequest ,
199+ ResponseInterface $ response ,
200+ ): ResponseInterface {
201+ if ($ authRequest instanceof AuthorizationRequest) {
202+ $ this ->consumePushedAuthorizationRequest ($ authRequest );
203+ }
204+
205+ return parent ::completeAuthorizationRequest ($ authRequest , $ response );
206+ }
207+
208+
209+ /**
210+ * Carry the PAR request_uri, if the request was made with one, on the validated authorization request,
211+ * which is what survives the login and the authprocs. A request of another type could not carry it, and
212+ * its PAR could then never be consumed, so it is refused.
213+ *
214+ * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
215+ */
216+ protected function bindPushedAuthorizationRequestUri (
217+ OAuth2AuthorizationRequestInterface $ authorizationRequest ,
218+ ResultBagInterface $ resultBag ,
219+ ): void {
220+ $ requestUri = $ resultBag ->get (RequestUriRule::class)?->getValue();
221+
222+ if (
223+ !is_string ($ requestUri ) ||
224+ !str_starts_with ($ requestUri , PushedAuthorizationRequestEntityFactory::REQUEST_URI_PREFIX )
225+ ) {
226+ return ;
227+ }
228+
229+ if (!$ authorizationRequest instanceof AuthorizationRequest) {
230+ $ this ->loggerService ?->error(
231+ 'AuthorizationServer: pushed authorization request can not be tracked for one-time use. ' ,
232+ ['authorizationRequestType ' => $ authorizationRequest ::class],
233+ );
234+ throw OidcServerException ::serverError ('Pushed authorization request can not be tracked. ' );
235+ }
236+
237+ $ authorizationRequest ->setPushedAuthorizationRequestUri ($ requestUri );
238+ }
239+
240+
241+ /**
242+ * @throws \SimpleSAML\Error\BadRequest
243+ * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
244+ */
245+ protected function consumePushedAuthorizationRequest (AuthorizationRequest $ authorizationRequest ): void
246+ {
247+ $ requestUri = $ authorizationRequest ->getPushedAuthorizationRequestUri ();
248+
249+ if ($ requestUri === null ) {
250+ return ;
251+ }
252+
253+ if ($ this ->pushedAuthorizationRequestRepository === null ) {
254+ $ this ->loggerService ?->error(
255+ 'AuthorizationServer: no pushed authorization request repository to consume the request with. ' ,
256+ compact ('requestUri ' ),
257+ );
258+ throw OidcServerException ::serverError ('Pushed authorization request can not be consumed. ' );
259+ }
260+
261+ if (!$ this ->pushedAuthorizationRequestRepository ->consume ($ requestUri )) {
262+ $ this ->loggerService ?->warning(
263+ 'AuthorizationServer: pushed authorization request replay attempt. ' ,
264+ compact ('requestUri ' ),
265+ );
266+ throw new BadRequest ('AuthorizationServer: Pushed authorization request has already been used. ' );
267+ }
268+ }
269+
270+
174271 /**
175272 * @throws \Throwable
176273 * @throws \SimpleSAML\Error\BadRequest
0 commit comments