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 $response = $this->sendAccessTokenRequest($this->parAuthorizationURL, $body); 39 $status = $response->getStatusCode(); 40 $json = MessageUtil::decodeJSON($response, true); 41 42 // something went horribly wrong 43 if($status !== 200){ 44 print_r($json); 45 46 // @link https://datatracker.ietf.org/doc/html/rfc9126#section-2.3 47 if(isset($json['error'], $json['error_description'])){ 48 throw new ProviderException(sprintf('PAR error: "%s" (%s)', $json['error'], $json['error_description'])); 49 } 50 51 throw new ProviderException(sprintf('PAR request error: (HTTP/%s)', $status)); // @codeCoverageIgnore 52 } 53 54 $url = QueryUtil::merge($this->authorizationURL, $this->getParAuthorizationURLRequestParams($json)); 55 56 return $this->uriFactory->createUri($url); 57 } 58 59 /** 60 * Parses the response from the PAR request and returns the query parameters for the authorization URL 61 * 62 * @see \chillerlan\OAuth\Core\OAuth2Provider::getParRequestUri() 63 * 64 * @param array<string, string> $response 65 * @return array<string, string> 66 * 67 * @codeCoverageIgnore 68 */ 69 protected function getParAuthorizationURLRequestParams(array $response):array{ 70 71 if(!isset($response['request_uri'])){ 72 throw new ProviderException('PAR response error: "request_uri" missing'); 73 } 74 75 return [ 76 'client_id' => $this->options->key, 77 'request_uri' => $response['request_uri'], 78 ]; 79 } 80 81}