friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Trait PARTrait
4 *
5 * @created 19.09.2024
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2024 smiley
8 * @license MIT
9 */
10declare(strict_types=1);
11
12namespace chillerlan\OAuth\Core;
13
14use chillerlan\HTTP\Utils\MessageUtil;
15use chillerlan\HTTP\Utils\QueryUtil;
16use chillerlan\OAuth\Providers\ProviderException;
17use Psr\Http\Message\UriInterface;
18use function sprintf;
19
20/**
21 * Implements PAR (Pushed Authorization Requests) functionality
22 *
23 * @see \chillerlan\OAuth\Core\PAR
24 */
25trait PARTrait{
26
27 /**
28 * implements PAR::getParRequestUri()
29 *
30 * @see \chillerlan\OAuth\Core\PAR::getParRequestUri()
31 * @see \chillerlan\OAuth\Core\OAuth2Provider::getAuthorizationURL()
32 *
33 * @param array<string, string> $body
34 */
35 public function getParRequestUri(array $body):UriInterface{
36 // send the request with the same method and parameters as the token requests
37 // @link https://datatracker.ietf.org/doc/html/rfc9126#name-request
38 print_r($this->parAuthorizationURL);
39 $response = $this->sendAccessTokenRequest($this->parAuthorizationURL, $body);
40 $status = $response->getStatusCode();
41 $json = MessageUtil::decodeJSON($response, true);
42 print_r($body);
43 print_r($json);
44
45 // something went horribly wrong
46 if($status !== 200){
47
48 // @link https://datatracker.ietf.org/doc/html/rfc9126#section-2.3
49 if(isset($json['error'], $json['error_description'])){
50 throw new ProviderException(sprintf('PAR error: "%s" (%s)', $json['error'], $json['error_description']));
51 }
52
53 throw new ProviderException(sprintf('PAR request error: (HTTP/%s)', $status)); // @codeCoverageIgnore
54 }
55
56 $url = QueryUtil::merge($this->authorizationURL, $this->getParAuthorizationURLRequestParams($json));
57
58 return $this->uriFactory->createUri($url);
59 }
60
61 /**
62 * Parses the response from the PAR request and returns the query parameters for the authorization URL
63 *
64 * @see \chillerlan\OAuth\Core\OAuth2Provider::getParRequestUri()
65 *
66 * @param array<string, string> $response
67 * @return array<string, string>
68 *
69 * @codeCoverageIgnore
70 */
71 protected function getParAuthorizationURLRequestParams(array $response):array{
72
73 if(!isset($response['request_uri'])){
74 throw new ProviderException('PAR response error: "request_uri" missing');
75 }
76
77 return [
78 'client_id' => $this->options->key,
79 'request_uri' => $response['request_uri'],
80 ];
81 }
82
83}