-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from plivo/jwt
add jwt helpers
- Loading branch information
Showing
7 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
use Plivo\Util\AccessToken; | ||
|
||
// using validFrom and lifetime | ||
$acctkn = new AccessToken("{authId}", "{authToken}", "{endpointUsername}", gmdate('U'), 3600, null); | ||
// grants(incoming:false, outgoing:true) | ||
$acctkn->addVoiceGrants(false, true); | ||
echo $acctkn->toJwt() . "\n"; | ||
|
||
// using validFrom and validTill, with custom uid | ||
$acctkn = new AccessToken("{authId}", "{authToken}", "{endpointUsername}", gmdate('U'), null, gmdate('U', mktime(23, 59, 59, 4, 29, 2020)), "{uid}"); | ||
// grants(incoming:true, outgoing:false) | ||
$acctkn->addVoiceGrants(true, false); | ||
echo $acctkn->toJwt() . "\n"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Plivo\Util; | ||
|
||
use Plivo\Authentication\BasicAuth; | ||
use Plivo\Exceptions\PlivoValidationException; | ||
use \Firebase\JWT\JWT; | ||
|
||
/** | ||
* Class jwt | ||
* @package Plivo\Util | ||
*/ | ||
class AccessToken | ||
{ | ||
protected $basicAuth; | ||
protected $username; | ||
protected $validFrom; | ||
protected $lifetime = 86400; | ||
protected $grants = []; | ||
protected $uid; | ||
|
||
/** | ||
* Instantiates a new AccessToken object. | ||
* | ||
* @param string|null $authId | ||
* @param string|null $authToken | ||
* @param string $username endpoint | ||
* @param integer|null $validFrom valid not before this epoch | ||
* @param integer|null $lifetime validity in seconds | ||
* @param integer|null $validTill validity expires at this epoch | ||
* @param null $uid | ||
*/ | ||
public function __construct( | ||
$authId = null, | ||
$authToken = null, | ||
$username = null, | ||
$validFrom = null, | ||
$lifetime = null, | ||
$validTill = null, | ||
$uid = null | ||
) | ||
{ | ||
$this->basicAuth = new BasicAuth($authId, $authToken); | ||
if ($username == null) { | ||
throw new PlivoValidationException("null username not allowed"); | ||
} | ||
$this->username = $username; | ||
$this->validFrom = intval($validFrom?:gmdate('U')); | ||
$this->lifetime = intval($lifetime?:86400); | ||
if ($lifetime != null) { | ||
if ($validTill != null) { | ||
throw new PlivoValidationException("use either lifetime or validTill"); | ||
} | ||
} else if ($validTill != null) { | ||
$this->lifetime = intval($validTill)-$this->validFrom; | ||
if ($this->lifetime < 180 || $this->lifetime > 86400) { | ||
throw new PlivoValidationException("lifetime out of [180, 86400]"); | ||
} | ||
} | ||
$this->uid = $uid?:$this->username."-".microtime(true); | ||
} | ||
/** | ||
* Adds voice calling permissions to the token | ||
* | ||
* @param bool $incoming | ||
* @param bool $outgoing | ||
*/ | ||
public function addVoiceGrants($incoming = false, $outgoing = false) | ||
{ | ||
$this->grants = array( | ||
"voice" => array( | ||
"incoming_allow" => $incoming, | ||
"outgoing_allow" => $outgoing | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns JWT | ||
* @returns string $jwt | ||
*/ | ||
public function toJwt() { | ||
$key = $this->basicAuth->getAuthToken(); | ||
$headers = array( | ||
"typ" => "JWT", | ||
"alg" => "HS256", | ||
"cty" => "plivo;v=1" | ||
); | ||
$payload = array( | ||
"jti" => $this->uid, | ||
"iss" => $this->basicAuth->getAuthId(), | ||
"sub" => $this->username, | ||
"nbf" => $this->validFrom, | ||
"exp" => $this->validFrom + $this->lifetime, | ||
"grants" => $this->grants | ||
); | ||
|
||
return JWT::encode($payload, $key, "HS256", null, $headers); | ||
} | ||
} |
This file contains 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
This file contains 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