Created
December 19, 2017 14:45
-
-
Save motin/927f87f4623273ae83aaed9ab93099e8 to your computer and use it in GitHub Desktop.
Example usage:
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
<?php | |
namespace api_clients; | |
use Auth0JwtBootstrap; | |
use AppJson; | |
class Auth0ManagementApiClient | |
{ | |
/** | |
* The reference to the auth0 tenant that this api client instance should target. | |
* The references are the ones found in the AUTH0_APPS config variable. | |
* @var | |
*/ | |
protected $auth0TenantReference; | |
/** | |
* @var mixed | |
*/ | |
protected $auth0AppConfig; | |
/** | |
* @var | |
*/ | |
protected $accessToken; | |
/** | |
* Auth0ManagementApiClient constructor. | |
* @param $auth0TenantReference | |
*/ | |
public function __construct($auth0TenantReference) | |
{ | |
$this->auth0TenantReference = $auth0TenantReference; | |
$this->auth0AppConfig = Auth0JwtBootstrap::getConfigForSpecificAuth0TenantReference( | |
$this->auth0TenantReference | |
); | |
} | |
public function ensureAccessToken() | |
{ | |
if (empty($this->accessToken)) { | |
$this->accessToken = $this->requestAccessToken(); | |
} | |
return $this->accessToken; | |
} | |
protected function curlRequest($curl) | |
{ | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
throw new Auth0ManagementApiRequestException("cURL Error #:" . $err); | |
} | |
$parsedResponse = AppJson::decode($response); | |
if (!empty($parsedResponse->error)) { | |
throw new Auth0ManagementApiRequestException("Error Response:" . $response); | |
} | |
return $parsedResponse; | |
} | |
public function requestAccessToken() | |
{ | |
$endpoint = "oauth/token"; | |
$curl = curl_init(); | |
curl_setopt_array( | |
$curl, | |
array( | |
CURLOPT_URL => "https://{$this->auth0AppConfig["domain"]}/$endpoint", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => "{\"grant_type\":\"client_credentials\",\"client_id\": \"{$this->auth0AppConfig["management_api_client_id"]}\",\"client_secret\": \"{$this->auth0AppConfig["management_api_client_secret"]}\",\"audience\": \"https://{$this->auth0AppConfig["domain"]}/api/v2/\"}", | |
CURLOPT_HTTPHEADER => array( | |
"content-type: application/json" | |
), | |
) | |
); | |
return $this->curlRequest($curl); | |
} | |
public function listAllClients() | |
{ | |
$endpoint = "api/v2/clients"; | |
$curl = curl_init(); | |
curl_setopt_array( | |
$curl, | |
array( | |
CURLOPT_URL => "https://{$this->auth0AppConfig["domain"]}/$endpoint", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"authorization: Bearer {$this->accessToken->access_token}", | |
"content-type: application/json" | |
), | |
) | |
); | |
return $this->curlRequest($curl); | |
} | |
public function getUserAttributes($auth0UserId) | |
{ | |
$endpoint = "api/v2/users/$auth0UserId"; | |
$curl = curl_init(); | |
curl_setopt_array( | |
$curl, | |
array( | |
CURLOPT_URL => "https://{$this->auth0AppConfig["domain"]}/$endpoint", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"authorization: Bearer {$this->accessToken->access_token}", | |
"content-type: application/json" | |
), | |
) | |
); | |
return $this->curlRequest($curl); | |
} | |
} | |
class Auth0ManagementApiRequestException extends \Exception | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment