Created
December 19, 2017 14:51
-
-
Save motin/ceb982efaf218c06b894792d5d8a6c84 to your computer and use it in GitHub Desktop.
Example of client class to perform auth0 management api requests in php. AppJson.php is available here: https://gist.github.com/motin/dc3762a64c7daadaa724d674b3e1d5f3
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 | |
/* | |
Example usage: | |
$this->auth0ManagementApiClient = new Auth0ManagementApiClient(); | |
$accessToken = $this->auth0ManagementApiClient->ensureAccessToken(); | |
if (!$accessToken) { | |
throw new Exception("Auth0 management access token empty"); | |
} | |
$auth0ClientsListing = $this->auth0ManagementApiClient->listAllClients(); | |
*/ | |
namespace api_clients; | |
use Auth0JwtBootstrap; | |
use AppJson; | |
class Auth0ManagementApiClient | |
{ | |
/** | |
* @var mixed | |
*/ | |
protected $auth0AppConfig; | |
/** | |
* @var | |
*/ | |
protected $accessToken; | |
/** | |
* Auth0ManagementApiClient constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->auth0AppConfig = [ | |
"domain" => "foo", | |
"id" => "foo", | |
"secret" => "foo", | |
"management_api_client_id" => "foo", | |
"management_api_client_secret" => "foo" | |
]; | |
} | |
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