Skip to content

Commit

Permalink
Add secure access token support
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed Sep 22, 2020
1 parent a97e28f commit b92cc9d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"require": {
"php": ">=7.1.3",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
Expand Down
81 changes: 75 additions & 6 deletions src/LiveStream/LiveStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,66 @@ class LiveStream
];

/**
* API_KEY.
* API key.
*
* @var string
*/
private $apiKey;

/**
* Class Constructor
* Client ID.
*
* @var int
*/
private $clientId;

/**
* Scope.
*
* @var string
*/
private $scope;

/**
* Secure access token.
*
* @var array
*/
private $token;

/**
* Secure access token timestamp.
*
* @var int
*/
private $tokenTimestamp;

/**
* Class Constructor.
*
* When only an API key is provided key auth method is used. When an API
* key, client ID, and scope are provided secure token auth is used.
*
* @param string $apiKey
* @param int|null $clientId
* @param string|null $scope
* Valid scopes are: all, readonly, playback
*
* @see https://livestream.com/developers/docs/api/#authentication
*/
public function __construct(string $apiKey)
public function __construct(
string $apiKey,
?int $clientId = null,
?string $scope = null
)
{
$this->apiKey = $apiKey;
$this->clientId = $clientId;
$this->scope = $scope;

if ($this->clientId && $this->scope) {
$this->refreshToken();
}
}

/**
Expand Down Expand Up @@ -381,6 +427,20 @@ public function getLiveVideo(
return $video;
}

/**
* Refreshes a secure access token if invalid (5 minute life time).
*
* @see https://github.com/Livestream/livestream-api-samples/tree/master/php/secure-token-auth-sample
*/
private function refreshToken(): void
{
$now = round(microtime(true) * 1000);
if (!$this->tokenTimestamp || round(($now - $this->tokenTimestamp)/1000) > 300) {
$this->tokenTimestamp = (int) $now;
$this->token = hash_hmac('md5', "{$this->apiKey}:{$this->scope}:{$this->tokenTimestamp}", $this->apiKey);
}
}

/**
* CURL Request
*
Expand All @@ -398,15 +458,24 @@ private function request(

if (!$ch) throw new Exception("Could not initialize CURL.");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if ($this->token && $this->clientId && $this->tokenTimestamp) {
$query['timestamp'] = $this->tokenTimestamp;
$query['clientId'] = $this->clientId;
$query['token'] = $this->token;
}
else {
curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey . ':');
}

curl_setopt(
$ch,
CURLOPT_URL,
$this->get_base_url() . $endpoint . ($query ? '?' . http_build_query($query) : '')
);

curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey . ':');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if ($verb != 'get') {
if ($verb == 'post') curl_setopt($ch, CURLOPT_POST, true);
if ($verb == 'put') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
Expand Down

0 comments on commit b92cc9d

Please sign in to comment.