diff --git a/OPSkinsOAuth.php b/OPSkinsOAuth.php index 62b563d..2cb2c1a 100755 --- a/OPSkinsOAuth.php +++ b/OPSkinsOAuth.php @@ -1,37 +1,26 @@ \OPSkinsOAuthSettings::$siteName, @@ -77,7 +66,7 @@ public function createOAuthClient() { \OPSkinsCurl::checkJsonError(); - if (!$data['status']){ + if (!$data['status']) { throw new Exception("Bad status from OPSkins API"); } @@ -85,8 +74,6 @@ public function createOAuthClient() { $output['client']['secret'] = $output['secret']; return OPSkinsClient::storeNewClient($output['client']); - - } /** @@ -94,15 +81,15 @@ public function createOAuthClient() { * @return bool * @throws Exception */ - public function deleteOAuthClient(\OPSkinsClient &$client){ - + public function deleteOAuthClient(\OPSkinsClient &$client) + { $url = \OPSkinsOAuthSettings::$opskinsAPIUrl . 'IOAuth/DeleteClient/v1/'; $curl = new OPSkinsCurl($url, INPUT_POST , $client->client_id); $curl->setAuth( \OPSkinsOAuthSettings::$opskinsAPIKey . ':'); $data = json_decode($curl->exec(), true); - if (!$data['status']){ + if (!$data['status']) { throw new Exception("Bad status from OPSkins API"); } @@ -113,15 +100,15 @@ public function deleteOAuthClient(\OPSkinsClient &$client){ * @return mixed * @throws Exception */ - public function getOwnedClientList(){ - + public function getOwnedClientList() + { $url = \OPSkinsOAuthSettings::$opskinsAPIUrl . 'IOAuth/GetOwnedClientList/v1/'; $curl = new OPSkinsCurl($url); $curl->setAuth( \OPSkinsOAuthSettings::$opskinsAPIKey . ':'); $data = json_decode($curl->exec(), true); - if (!$data['status']){ + if (!$data['status']) { throw new Exception("Bad status from OPSkins API"); } @@ -133,15 +120,15 @@ public function getOwnedClientList(){ * @return bool|OPSkinsClient * @throws Exception */ - public function resetClientSecret(\OPSkinsClient &$client){ - + public function resetClientSecret(\OPSkinsClient &$client) + { $url = \OPSkinsOAuthSettings::$opskinsAPIUrl . 'IOAuth/ResetClientSecret/v1/'; $curl = new OPSkinsCurl($url, INPUT_POST , $client->client_id); $curl->setAuth( \OPSkinsOAuthSettings::$opskinsAPIKey . ':'); $data = json_decode($curl->exec(), true); - if (!$data['status']){ + if (!$data['status']) { throw new Exception("Bad status from OPSkins API"); } @@ -158,18 +145,18 @@ public function resetClientSecret(\OPSkinsClient &$client){ * @return bool|OPSkinsClient * @throws Exception */ - public function updateClient(\OPSkinsClient &$client, $name = '', $redirect_uri = ''){ - - if(empty($redirect_uri) && empty($name)){ + public function updateClient(\OPSkinsClient &$client, $name = '', $redirect_uri = '') + { + if (empty($redirect_uri) && empty($name)) { throw new Exception("Either redirect_uri or name must be set."); } $input = []; $input['client_id'] = $client->client_id; - if(!empty($name)){ + if (!empty($name)) { $input['redirect_uri'] = $redirect_uri; } - if(!empty($name)){ + if (!empty($name)) { $input['name'] = $name; } @@ -179,7 +166,7 @@ public function updateClient(\OPSkinsClient &$client, $name = '', $redirect_uri $curl->setAuth( \OPSkinsOAuthSettings::$opskinsAPIKey . ':'); $data = json_decode($curl->exec(), true); - if (!$data['status']){ + if (!$data['status']) { throw new Exception("Bad status from OPSkins API"); } @@ -187,7 +174,6 @@ public function updateClient(\OPSkinsClient &$client, $name = '', $redirect_uri $output['client']['secret'] = $client->secret; return OPSkinsClient::storeNewClient($output['client']); - } /** @@ -196,10 +182,10 @@ public function updateClient(\OPSkinsClient &$client, $name = '', $redirect_uri * @return string * @throws Exception */ - public function getAuthUrl(\OPSkinsClient &$client, $scopes = ['identity']){ - - foreach ($scopes as $scope){ - if(!in_array($scope, self::$allowedScopes)){ + public function getAuthUrl(\OPSkinsClient &$client, $scopes = ['identity']) + { + foreach ($scopes as $scope) { + if (!in_array($scope, self::$allowedScopes)) { throw new Exception("Invalid Scope selected"); } } @@ -207,18 +193,18 @@ public function getAuthUrl(\OPSkinsClient &$client, $scopes = ['identity']){ $state = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(10/strlen($x)) )),1,10); // save state - if(is_file(\OPSkinsOAuthSettings::$stateMappingFile)) { + if (is_file(\OPSkinsOAuthSettings::$stateMappingFile)) { $state_map = file_get_contents(\OPSkinsOAuthSettings::$stateMappingFile); } - if (empty($state_map)){ + + if (empty($state_map)) { file_put_contents(\OPSkinsOAuthSettings::$stateMappingFile, json_encode([$state => $client->client_id])); - }else{ + } else { $state_map = json_decode($state_map, true); $state_map[$state] = $client->client_id; file_put_contents(\OPSkinsOAuthSettings::$stateMappingFile, json_encode($state_map)); } - $input = [ 'client_id' => $client->client_id, 'response_type' => 'code', @@ -236,19 +222,21 @@ public function getAuthUrl(\OPSkinsClient &$client, $scopes = ['identity']){ * @return OPSkinsClient * @throws Exception */ - public function verifyReturn($state, $code){ - if(empty($state)){ + public function verifyReturn($state, $code) + { + if (empty($state)) { throw new Exception("State is empty"); } $state_map = file_get_contents(\OPSkinsOAuthSettings::$stateMappingFile); - if(empty($state_map)){ + if (empty($state_map)) { throw new Exception("state map is empty"); } + $state_map = json_decode($state_map, true); - if(empty($state_map[$state]) || !empty($_GET['error'])){ + if (empty($state_map[$state]) || !empty($_GET['error'])) { throw new Exception("unable to locate mapping"); } @@ -262,7 +250,6 @@ public function verifyReturn($state, $code){ file_put_contents(\OPSkinsOAuthSettings::$stateMappingFile, json_encode($state_map)); return $client; - } /** @@ -273,7 +260,7 @@ public function verifyReturn($state, $code){ public function getBearerToken(\OPSkinsClient &$client){ $url = 'https://oauth.opskins.com/v1/access_token'; - if(empty($client->authCode)){ + if (empty($client->authCode)) { throw new Exception("No auth code for client"); } @@ -296,8 +283,8 @@ public function getBearerToken(\OPSkinsClient &$client){ * @param OPSkinsClient $client * @return mixed */ - public function testAuthed(\OPSkinsClient $client){ - + public function testAuthed(\OPSkinsClient $client) + { $url = \OPSkinsOAuthSettings::$opskinsAPIUrl . 'ITest/TestAuthed/v1/'; $curl = new OPSkinsCurl($url); @@ -305,16 +292,14 @@ public function testAuthed(\OPSkinsClient $client){ $data = json_decode($curl->exec(), true); return $data; - } - } /** * Class OPSkinsCurl */ -class OPSkinsCurl { - +class OPSkinsCurl +{ public $ch; public function __construct($url, $method = INPUT_GET, $fields = []) @@ -323,7 +308,7 @@ public function __construct($url, $method = INPUT_GET, $fields = []) curl_setopt($this->ch, CURLOPT_URL, $url); curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'); - if( $method == INPUT_POST) { + if ($method == INPUT_POST) { if (is_array($fields) && !empty($fields)) { curl_setopt($this->ch, CURLOPT_POST, true); } else { @@ -331,7 +316,7 @@ public function __construct($url, $method = INPUT_GET, $fields = []) } } - if(!empty($fields)) { + if (!empty($fields)) { if (is_array($fields)) { $curlFields = http_build_query($fields); @@ -351,14 +336,16 @@ public function __construct($url, $method = INPUT_GET, $fields = []) /** * @param $authString */ - public function setAuth($authString){ + public function setAuth($authString) + { curl_setopt($this->ch, CURLOPT_USERPWD, $authString); } /** * @param $bearerToken */ - public function setBearer($bearerToken){ + public function setBearer($bearerToken) + { curl_setopt($this->ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $bearerToken"]); } @@ -366,7 +353,8 @@ public function setBearer($bearerToken){ * @return bool|string * @throws Exception */ - public function exec(){ + public function exec() + { $result = curl_exec($this->ch); // Parse out the headers @@ -374,30 +362,26 @@ public function exec(){ $headers = substr($result, 0, $headersize - 4); $responseBody = substr($result, $headersize); - - if(curl_getinfo($this->ch, CURLINFO_HTTP_CODE) != 200){ + if (curl_getinfo($this->ch, CURLINFO_HTTP_CODE) != 200) { throw new Exception("HTTP error " . curl_getinfo($this->ch, CURLINFO_HTTP_CODE) . " $responseBody"); - } return $responseBody; } - public static function checkJsonError(){ + public static function checkJsonError() { if (json_last_error()) { throw new Exception(function_exists('json_last_error_msg') ? json_last_error_msg() : 'JSON error ' . json_last_error(), json_last_error()); } } - - } /** * Class OPSkinsClient */ -class OPSkinsClient { - +class OPSkinsClient +{ public $secret; public $client_id; public $name; @@ -426,37 +410,37 @@ public function __construct($client_id) * @param $client_arr * @return bool|OPSkinsClient */ - public static function storeNewClient($client_arr){ + public static function storeNewClient($client_arr) + { $client = new self($client_arr['client_id']); foreach ($client_arr as $key => $val){ - if( !in_array($key, self::$requiredFields) ){ + if (!in_array($key, self::$requiredFields)) { continue; } + $client->{$key} = $val; - } - if($client->storeClient()){ + if ($client->storeClient()) { return $client; } return false; } - /** * @return bool|mixed */ - public function getClientList(){ - - if(!is_file(\OPSkinsOAuthSettings::$clientsFileLocation)){ + public function getClientList() + { + if (!is_file(\OPSkinsOAuthSettings::$clientsFileLocation)) { return false; } $contents = file_get_contents(\OPSkinsOAuthSettings::$clientsFileLocation); - if (empty($contents)){ + if (empty($contents)) { return false; } @@ -464,24 +448,25 @@ public function getClientList(){ \OPSkinsCurl::checkJsonError(); - if(empty($client_list)){ + if (empty($client_list)) { return false; } return $client_list; } - /** * @return $this * @throws Exception */ - public function loadClient(){ + public function loadClient() + { $client_list = $this->getClientList(); - if(empty( $client_list->{$this->client_id} )){ + if (empty($client_list->{$this->client_id})) { throw new Exception("Client id is missing"); } + /** @var $client OPSkinsOAuth*/ $client = $client_list->{$this->client_id}; @@ -493,10 +478,12 @@ public function loadClient(){ $this->redirect_uri = $client->redirect_uri; $this->time_created = $client->time_created; $this->has_secret = $client->has_secret; - if(!empty($client->authCode)){ + + if (!empty($client->authCode)) { $this->authCode = $client->authCode; } - if(!empty($client->bearerToken)){ + + if (!empty($client->bearerToken)) { $this->bearerToken = $client->bearerToken; } @@ -507,13 +494,13 @@ public function loadClient(){ * @return bool * @throws Exception */ - public function storeClient(){ - + public function storeClient() + { $client_list = $this->getClientList(); $client_list->{$this->client_id} = $this; - if( !$this->verifyNonEmpty() ){ + if (!$this->verifyNonEmpty()) { throw new Exception("missing data"); } @@ -525,13 +512,14 @@ public function storeClient(){ /** * @return bool */ - protected function verifyNonEmpty(){ - foreach (self::$requiredFields as $field){ - if ( empty($this->{$field}) ){ + protected function verifyNonEmpty() + { + foreach (self::$requiredFields as $field) { + if (empty($this->{$field})) { return false; } } + return true; } - -} \ No newline at end of file +}