Created
April 12, 2018 08:03
-
-
Save kopiro/c00300889d7178202a1c2aa0263af93f to your computer and use it in GitHub Desktop.
Implicit Grant with Postmessage for Laravel Passport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libraries\OAuth; | |
class ImplicitGrantWithPostmessage extends \League\OAuth2\Server\Grant\ImplicitGrant | |
{ | |
public function completeAuthorizationRequest(\League\OAuth2\Server\RequestTypes\AuthorizationRequest $authorizationRequest) { | |
$response = parent::completeAuthorizationRequest($authorizationRequest); | |
$reflectionClassResponse = new \ReflectionClass($response); | |
$reflectionProperty = $reflectionClassResponse->getProperty('redirectUri'); | |
$reflectionProperty->setAccessible(true); | |
$redirectUri = $reflectionProperty->getValue($response); | |
if (empty($redirectUri)) throw new \Exception('Invalid redirect'); | |
// Parse response | |
$redirectUriExploded = explode('#', $redirectUri, 2); | |
$redirectUriOne = current($redirectUriExploded); | |
$redirectUriTwo = end($redirectUriExploded); | |
if (empty($redirectUriOne)) throw new \Exception('Invalid redirect'); | |
$urlComponents = parse_url($redirectUriOne); | |
$domain = $urlComponents['scheme'] . '://' . $urlComponents['host']; | |
if (isset($urlComponents['port'])) $domain .= ':' . $urlComponents['port']; | |
parse_str($redirectUriTwo, $data); | |
$data = json_encode($data); | |
echo "<script> | |
if (window.opener != null) { | |
window.opener.postMessage($data, '$domain'); | |
window.close(); | |
} else { | |
window.location.href = '$redirectUri'; | |
} | |
</script>"; | |
exit; | |
} | |
public function getIdentifier() { | |
return 'token'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment