diff --git a/examples/GrantToken.php b/examples/GrantToken.php index 8609e66a..8eafd636 100644 --- a/examples/GrantToken.php +++ b/examples/GrantToken.php @@ -2,35 +2,38 @@ require_once __DIR__ . '/../vendor/autoload.php'; -use PubNub\Models\Access\Permissions; use PubNub\Models\Consumer\AccessManager\PNAccessManagerTokenResult; - $pnconfig = new \PubNub\PNConfiguration(); -$pnconfig->setPublishKey('my-publish-key'); -$pnconfig->setSubscribeKey('my-subscribe-key'); -$pnconfig->setSecretKey('my-secret-key'); +$pnconfig->setPublishKey(getenv("PUBLISH_PAM_KEY")); +$pnconfig->setSubscribeKey(getenv("SUBSCRIBE_PAM_KEY")); +$pnconfig->setSecretKey(getenv("SECRET_PAM_KEY")); +$pnconfig->setUuid('example-uuid'); $pubnub = new \PubNub\PubNub($pnconfig); try { $token = $pubnub->grantToken() ->ttl(30) - ->authorizedUuid('my-uuid') + ->authorizedUuid('example-uuid') ->addChannelResources([ 'my-channel' => ['read' => true] ]) ->sync(); - /** @var PNAccessManagerTokenResult */ - $parsedToken = $pubnub->parseToken($token); - $parsedToken->getTtl(); - $parsedToken->getChannelResource('my-channel') - ->hasRead(); + print("Token granted: $token\n"); + + /** @var PNAccessManagerTokenResult */ + $parsedToken = $pubnub->parseToken($token); + + $tokensTTL = $parsedToken->getTtl(); + $tokensMyChannelRead = $parsedToken->getChannelResource('my-channel')->hasRead(); + $tokensMyChannelWrite = $parsedToken->getChannelResource('my-channel')->hasWrite(); + + print("Token TTL: $tokensTTL\n"); + print("Token My Channel Read: " . (int)$tokensMyChannelRead . "\n"); + print("Token My Channel Write: " . (int)$tokensMyChannelWrite . "\n"); + } catch (\PubNub\Exceptions\PubNubServerException $e) { var_dump($e->getBody()); } - -var_dump( - $pubnub->parseToken($token)->toArray() -); die; diff --git a/src/PubNub/Endpoints/Access/GrantToken.php b/src/PubNub/Endpoints/Access/GrantToken.php index f10b18c7..fbe41a5e 100644 --- a/src/PubNub/Endpoints/Access/GrantToken.php +++ b/src/PubNub/Endpoints/Access/GrantToken.php @@ -237,18 +237,18 @@ public function buildPath() } /** - * @return PNAccessManagerGrantResult + * @return string */ - public function sync(): PNAccessManagerGrantResult + public function sync(): string { return parent::sync(); } /** * @param string $token - * @return : PNAccessManagerGrantResult + * @return : string */ - public function createResponse($response): PNAccessManagerGrantResult + public function createResponse($response): string { return $response['data']['token']; }