From 3684a76aded7d27de22fea313c0cae6b73d34e32 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 23 Aug 2019 00:00:15 +0300 Subject: [PATCH] Add getter for the OAuthServerException::redirectUri param and make it public --- src/Exception/OAuthServerException.php | 36 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Exception/OAuthServerException.php b/src/Exception/OAuthServerException.php index 8e628baa..60e42a6b 100644 --- a/src/Exception/OAuthServerException.php +++ b/src/Exception/OAuthServerException.php @@ -294,14 +294,9 @@ class OAuthServerException extends Exception $payload = $this->getPayload(); - if ($this->redirectUri !== null) { - if ($useFragment === true) { - $this->redirectUri .= (strstr($this->redirectUri, '#') === false) ? '#' : '&'; - } else { - $this->redirectUri .= (strstr($this->redirectUri, '?') === false) ? '?' : '&'; - } - - return $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload)); + $redirectUri = $this->getRedirectUri($useFragment); + if ($redirectUri !== null) { + return $response->withStatus(302)->withHeader('Location', $redirectUri); } foreach ($headers as $header => $content) { @@ -359,6 +354,31 @@ class OAuthServerException extends Exception return $this->redirectUri !== null; } + /** + * Returns the redirectUri with all necessary args. + * + * Null will be returned if the exception doesn't contain the redirectUri. + * + * @param bool $useFragment True if errors should be in the URI fragment instead of query string + * + * @return string|null + */ + public function getRedirectUri(bool $useFragment = false): ?string + { + if ($this->redirectUri === null) { + return null; + } + + $redirectUri = $this->redirectUri; + if ($useFragment) { + $redirectUri .= strpos($this->redirectUri, '#') === false ? '#' : '&'; + } else { + $redirectUri .= strpos($this->redirectUri, '?') === false ? '?' : '&'; + } + + return $redirectUri . http_build_query($this->getPayload()); + } + /** * Returns the HTTP status code to send when the exceptions is output. *