Merge branch 'export_redirect_uri' into adaptation

This commit is contained in:
ErickSkrauch 2019-08-23 00:17:49 +03:00
commit 08e470e81a
2 changed files with 37 additions and 8 deletions

View File

@ -294,14 +294,9 @@ class OAuthServerException extends Exception
$payload = $this->getPayload(); $payload = $this->getPayload();
if ($this->redirectUri !== null) { $redirectUri = $this->getRedirectUri($useFragment);
if ($useFragment === true) { if ($redirectUri !== null) {
$this->redirectUri .= (strstr($this->redirectUri, '#') === false) ? '#' : '&'; return $response->withStatus(302)->withHeader('Location', $redirectUri);
} else {
$this->redirectUri .= (strstr($this->redirectUri, '?') === false) ? '?' : '&';
}
return $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload));
} }
foreach ($headers as $header => $content) { foreach ($headers as $header => $content) {
@ -359,6 +354,31 @@ class OAuthServerException extends Exception
return $this->redirectUri !== null; 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. * Returns the HTTP status code to send when the exceptions is output.
* *

View File

@ -71,6 +71,14 @@ class OAuthServerExceptionTest extends TestCase
$exceptionWithRedirect = OAuthServerException::accessDenied('some hint', 'https://example.com/error'); $exceptionWithRedirect = OAuthServerException::accessDenied('some hint', 'https://example.com/error');
$this->assertTrue($exceptionWithRedirect->hasRedirect()); $this->assertTrue($exceptionWithRedirect->hasRedirect());
$this->assertSame(
'https://example.com/error?error=access_denied&error_description=The+resource+owner+or+authorization+server+denied+the+request.&hint=some+hint&message=The+resource+owner+or+authorization+server+denied+the+request.',
$exceptionWithRedirect->getRedirectUri()
);
$this->assertSame(
'https://example.com/error#error=access_denied&error_description=The+resource+owner+or+authorization+server+denied+the+request.&hint=some+hint&message=The+resource+owner+or+authorization+server+denied+the+request.',
$exceptionWithRedirect->getRedirectUri(true)
);
} }
public function testDoesNotHaveRedirect() public function testDoesNotHaveRedirect()
@ -78,6 +86,7 @@ class OAuthServerExceptionTest extends TestCase
$exceptionWithoutRedirect = OAuthServerException::accessDenied('Some hint'); $exceptionWithoutRedirect = OAuthServerException::accessDenied('Some hint');
$this->assertFalse($exceptionWithoutRedirect->hasRedirect()); $this->assertFalse($exceptionWithoutRedirect->hasRedirect());
$this->assertNull($exceptionWithoutRedirect->getRedirectUri());
} }
public function testHasPrevious() public function testHasPrevious()