public function get(Request $request, Application $app)
 {
     /** @var MyBBUser $identity */
     $identity = $app['security']->getToken()->getUser();
     $deviceId = $request->get('fingerprint');
     $appName = $app['config']['app']['name'];
     $token = new \Services_Twilio_AccessToken($app['config']['twilio']['accountSid'], $app['config']['twilio']['apiKey'], $app['config']['twilio']['apiSecret'], self::$ttl, $identity->getUsername());
     $impGrant = new \Services_Twilio_Auth_IpMessagingGrant();
     $impGrant->setServiceSid($app['config']['twilio']['IPMServiceSid']);
     $impGrant->setEndpointId(sprintf('%s:%s:%s', $appName, $identity->getUsername(), $deviceId));
     $token->addGrant($impGrant);
     return new JsonResponse(['identity' => $identity->getUsername(), 'token' => $token->toJWT()]);
 }
 function testIpMessagingGrant()
 {
     $scat = new Services_Twilio_AccessToken(self::ACCOUNT_SID, self::SIGNING_KEY_SID, 'secret');
     $grant = new Services_Twilio_Auth_IpMessagingGrant();
     $grant->setEndpointId("EP123");
     $grant->setServiceSid("IS123");
     $scat->addGrant($grant);
     $token = $scat->toJWT();
     $this->assertNotNull($token);
     $payload = JWT::decode($token, 'secret');
     $this->validateClaims($payload);
     $grants = json_decode(json_encode($payload->grants), true);
     $this->assertEquals(1, count($grants));
     $this->assertArrayHasKey("ip_messaging", $grants);
     $this->assertEquals("EP123", $grants['ip_messaging']['endpoint_id']);
     $this->assertEquals("IS123", $grants['ip_messaging']['service_sid']);
 }
Exemple #3
0
<?php

require_once './twilio-php/Services/Twilio.php';
require_once './randos.php';
require_once './config.php';
// An identifier for your app - can be anything you'd like
$appName = 'TwilioChatDemo';
// choose a random username for the connecting user
$identity = randomUsername();
// A device ID is passed as a query string parameter to this script
$deviceId = $_GET['device'];
// The endpoint ID is a combination of the above
$endpointId = $appName . ':' . $identity . ':' . $deviceId;
// Create access token, which we will serialize and send to the client
$token = new Services_Twilio_AccessToken($TWILIO_ACCOUNT_SID, $TWILIO_API_KEY, $TWILIO_API_SECRET, 3600, $identity);
// Create IP Messaging grant
$ipmGrant = new Services_Twilio_Auth_IpMessagingGrant();
$ipmGrant->setServiceSid($TWILIO_IPM_SERVICE_SID);
$ipmGrant->setEndpointId($endpointId);
// Add grant to token
$token->addGrant($ipmGrant);
// return serialized token and the user's randomly generated ID
echo json_encode(array('identity' => $identity, 'token' => $token->toJWT()));
<?php

require_once './twilio-php/Services/Twilio.php';
// Required for all Twilio access tokens
$twilioAccountSid = 'ACxxxxxxxxxxxx';
$twilioApiKey = 'SKxxxxxxxxxxxx';
$twilioApiSecret = 'xxxxxxxxxxxxxx';
// Required for IP messaging grant
$ipmServiceSid = 'ISxxxxxxxxxxxx';
$appName = 'HipFlowSlackDockRC';
$identity = '*****@*****.**';
$deviceId = 'someiosdevice';
$endpointId = $appName . ':' . $identity . ':' . $deviceId;
// Create access token
$token = new Services_Twilio_AccessToken($twilioAccountSid, $twilioApiKey, $twilioApiSecret, 3600, $identity);
// Create IP Messaging grant
$ipmGrant = new Services_Twilio_Auth_IpMessagingGrant();
$ipmGrant->setServiceSid($ipmServiceSid);
$ipmGrant->setEndpointId($endpointId);
// Add grant to token
$token->addGrant($ipmGrant);
// render token to string
echo $token->toJWT();