Skip to content

Commit

Permalink
Refactored auth URL variable principles by separating and adding a to…
Browse files Browse the repository at this point in the history
…ken URL variable. Potential breaking change for previous implementations.
  • Loading branch information
MindaugasKiznis committed Apr 10, 2018
1 parent f4d07ee commit 7d5f2a3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -47,12 +47,15 @@ Simple usage looks like:
// Set the variables
RESO\RESO::setClientId('YOUR_CLIENT_ID');
RESO\RESO::setClientSecret('YOUR_CLIENT_SECRET');
RESO\RESO::setAPIAuthUrl('https://op.api.crmls.org/identity/connect/');
RESO\RESO::setAPIAuthUrl('https://op.api.crmls.org/identity/connect/authorize');
RESO\RESO::setAPITokenUrl('https://op.api.crmls.org/identity/connect/token');
RESO\RESO::setAPIRequestUrl('https://h.api.crmls.org/RESO/OData/');
// Authorize user
$auth_code = RESO\OpenIDConnect::authorize('YOUR_USERNAME', 'YOUR_PASSWORD', 'https://openid.reso.org/', 'ODataApi');
// Get access token
RESO\RESO::setAccessToken(RESO\OpenIDConnect::requestAccessToken($auth_code, 'https://openid.reso.org/', 'ODataApi'));
// Set the Accept header (if needed)
RESO\Request::setAcceptType("json");
// Retrieve top 10 properties from the RESO API endpoint
$data = RESO\Request::request('Property?\$top=10', 'json', true);

Expand Down
3 changes: 3 additions & 0 deletions examples/cli-example/config.php
Expand Up @@ -3,6 +3,9 @@
// API authentication URL
$api_auth_url = "";

// API token URL
$api_token_url = "";

// API data request (web) URL
$api_request_url = "";

Expand Down
12 changes: 11 additions & 1 deletion examples/cli-example/example-cli.php
Expand Up @@ -14,6 +14,7 @@
RESO\RESO::setClientId($client_id);
RESO\RESO::setClientSecret($client_secret);
RESO\RESO::setAPIAuthUrl($api_auth_url);
RESO\RESO::setAPITokenUrl($api_token_url);
RESO\RESO::setAPIRequestUrl($api_request_url);

// Authorize user
Expand All @@ -22,8 +23,17 @@
// Get access token
RESO\RESO::setAccessToken(RESO\OpenIDConnect::requestAccessToken($auth_code, $redirect_uri, $scope));

// Set the Accept header (if needed)
RESO\Request::setAcceptType("json");

// Retrieve metadata from RESO API
//$data = RESO\Request::requestMetadata();
/*
$data = RESO\Request::requestMetadata();
// Print Metadata
echo "\nMetadata:\n\n";
print_r($data);
echo "\n\n";
*/

// Retrieve top 10 properties from the RESO API endpoint
$data = RESO\Request::request("Property?\$top=10", "json", true);
Expand Down
1 change: 1 addition & 0 deletions examples/web-callback-example/callback.php
Expand Up @@ -7,6 +7,7 @@
// Set variables
RESO\RESO::setClientId($client_id);
RESO\RESO::setClientSecret($client_secret);
RESO\RESO::setAPITokenUrl($api_token_url);
RESO\RESO::setAPIRequestUrl($api_request_url);

if($_GET['code']) {
Expand Down
3 changes: 3 additions & 0 deletions examples/web-callback-example/config.php
Expand Up @@ -3,6 +3,9 @@
// API authentication URL
$api_auth_url = "";

// API token URL
$api_token_url = "";

// API data request (web) URL
$api_request_url = "";

Expand Down
1 change: 1 addition & 0 deletions examples/web-callback-example/index.php
Expand Up @@ -8,6 +8,7 @@
RESO\RESO::setClientId($client_id);
RESO\RESO::setClientSecret($client_secret);
RESO\RESO::setAPIAuthUrl($api_auth_url);
RESO\RESO::setAPITokenUrl($api_token_url);

// Redirect user to login page
header("Location: ".RESO\OpenIDConnect::getLoginUrl($redirect_uri, $scope));
3 changes: 3 additions & 0 deletions examples/web-example/config.php
Expand Up @@ -3,6 +3,9 @@
// API authentication URL
$api_auth_url = "";

// API token URL
$api_token_url = "";

// API data request (web) URL
$api_request_url = "";

Expand Down
4 changes: 4 additions & 0 deletions examples/web-example/index.php
Expand Up @@ -9,8 +9,12 @@
RESO\RESO::setClientId($client_id);
RESO\RESO::setClientSecret($client_secret);
RESO\RESO::setAPIAuthUrl($api_auth_url);
RESO\RESO::setAPITokenUrl($api_token_url);
RESO\RESO::setAPIRequestUrl($api_request_url);

// Set the Accept header (if needed)
RESO\Request::setAcceptType("json");

// Print head layout
echo '<html>
<head>
Expand Down
22 changes: 6 additions & 16 deletions lib/OpenIDConnect.php
Expand Up @@ -30,9 +30,6 @@ public static function authorize($username, $password, $redirect_uri, $scope = "

$curl = new \RESO\HttpClient\CurlClient();

// Build auth request URL
$url = rtrim($api_auth_url, "/") . "/authorize";

// Authentication request parameters
$params = array(
"client_id" => $client_id,
Expand All @@ -42,7 +39,7 @@ public static function authorize($username, $password, $redirect_uri, $scope = "
);

// Request authentication
$response = $curl->request("get", $url, null, $params, false)[0];
$response = $curl->request("get", $api_auth_url, null, $params, false)[0];
$params = @Util\Util::extractFormParameters($response);

// Do login form POST
Expand Down Expand Up @@ -114,14 +111,12 @@ public static function requestAccessToken($auth_code, $redirect_uri, $scope = "O
\RESO\RESO::logMessage("Sending authorization request to retrieve access token.");

// Get variables
$api_auth_url = \RESO\RESO::getAPIAuthUrl();
$api_token_url = \RESO\RESO::getAPITokenUrl();
$client_id = \RESO\RESO::getClientId();
$client_secret = \RESO\RESO::getClientSecret();

$curl = new \RESO\HttpClient\CurlClient();

// Build token request URL
$url = rtrim($api_auth_url, "/") . "/token";
$headers = array(
'Authorization: Basic '.base64_encode($client_id.":".$client_secret)
);
Expand All @@ -132,7 +127,7 @@ public static function requestAccessToken($auth_code, $redirect_uri, $scope = "O
"code" => $auth_code
);

$response = json_decode($curl->request("post", $url, $headers, $params, false)[0], true);
$response = json_decode($curl->request("post", $api_token_url, $headers, $params, false)[0], true);
if(!$response || !is_array($response) || !isset($response["access_token"]))
throw new Error\Api("Failed to obtain access token.");

Expand All @@ -150,14 +145,12 @@ public static function requestRefreshToken()

// Get variables
$access_token = \RESO\RESO::getAccessToken();
$api_auth_url = \RESO\RESO::getAPIAuthUrl();
$api_token_url = \RESO\RESO::getAPITokenUrl();
$client_id = \RESO\RESO::getClientId();
$client_secret = \RESO\RESO::getClientSecret();

$curl = new \RESO\HttpClient\CurlClient();

// Build token request URL
$url = $api_auth_url . "token";
$headers = array(
'Authorization: Basic '.base64_encode($client_id.":".$client_secret)
);
Expand All @@ -166,7 +159,7 @@ public static function requestRefreshToken()
"refresh_token" => $access_token
);

$response = json_decode($curl->request("post", $url, $headers, $params, false)[0], true);
$response = json_decode($curl->request("post", $api_token_url, $headers, $params, false)[0], true);
if(!$response || !is_array($response) || !isset($response["refresh_token"]))
throw new Error\Api("Failed to refresh token.");

Expand All @@ -187,9 +180,6 @@ public static function getLoginUrl($redirect_uri, $scope = "ODataApi")
$api_auth_url = \RESO\RESO::getAPIAuthUrl();
$client_id = \RESO\RESO::getClientId();

// Build auth request URL
$url = $api_auth_url . "authorize";

// Authentication request parameters
$params = array(
"client_id" => $client_id,
Expand All @@ -198,6 +188,6 @@ public static function getLoginUrl($redirect_uri, $scope = "ODataApi")
"response_type" => "code"
);

return $url . '?' . http_build_query($params);
return $api_auth_url . '?' . http_build_query($params);
}
}
25 changes: 24 additions & 1 deletion lib/Reso.php
Expand Up @@ -21,9 +21,12 @@ class RESO
// @var string The RESO API access token.
public static $accessToken;

// @var string The base URL for RESO API Auth service.
// @var string The authentication / authorization URL for RESO API Auth service.
public static $apiAuthUrl = '';

// @var string The token request URL for RESO API Auth service.
public static $apiTokenUrl = '';

// @var string The base URL for RESO API Request service.
public static $apiRequestUrl = '';

Expand Down Expand Up @@ -124,6 +127,26 @@ public static function setAPIAuthUrl($apiAuthUrl)
self::$apiAuthUrl = $apiAuthUrl;
}

/**
* @return string The RESO API token endpoint URL.
*/
public static function getAPITokenUrl()
{
if(!self::$apiTokenUrl) throw new Error\Reso("API token endpoint URL is not set.");
return self::$apiTokenUrl;
}

/**
* Sets the RESO API token endpoint URL.
*
* @param string $apiTokenUrl
*/
public static function setAPITokenUrl($apiTokenUrl)
{
self::logMessage("Setting RESO API token URL to '".$apiTokenUrl."'.");
self::$apiTokenUrl = $apiTokenUrl;
}

/**
* @return string The RESO API request endpoint URL.
*/
Expand Down

0 comments on commit 7d5f2a3

Please sign in to comment.