Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created April 12, 2018 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kopiro/c00300889d7178202a1c2aa0263af93f to your computer and use it in GitHub Desktop.
Save kopiro/c00300889d7178202a1c2aa0263af93f to your computer and use it in GitHub Desktop.
Implicit Grant with Postmessage for Laravel Passport
<?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