diff --git a/README.md b/README.md index 4744ab3..441ab12 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,9 @@ This library also provides easy interface to make it easier to interact with [Az - `put($ref, $body, $accessToken)` - `delete($ref, $body, $accessToken)` - `patch($ref, $body, $accessToken)` -- `getObjects($tenant, $ref, $objects = [], $accessToken)` This is used for example for listing large amount of data - where you need to list all users for example - it automatically follows `odata.nextLink` until the end. +- `getObjects($tenant, $ref, $accessToken, $objects = [])` This is used for example for listing large amount of data - where you need to list all users for example - it automatically follows `odata.nextLink` until the end. - `$tenant` tenant has to be provided since the `odata.nextLink` doesn't contain it. - - `$objects` should be either an empty array or a set of data which will be included in the results + - `$objects` should be either an empty array or a set of data which will be included in the results *Please not that if you need to create a custom request, the method getAuthenticatedRequest and getResponse can still be used* @@ -113,4 +113,4 @@ We accept contributions via [Pull Requests on Github](https://github.com/thenetw If you find a bug or encounter any issue or have a problem/question with this library please create a [new issue](https://github.com/TheNetworg/oauth2-azure/issues). ## License -The MIT License (MIT). Please see [License File](https://github.com/thenetworg/oauth2-azure/blob/master/LICENSE) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](https://github.com/thenetworg/oauth2-azure/blob/master/LICENSE) for more information. diff --git a/src/Provider/Azure.php b/src/Provider/Azure.php index bfdca67..af69269 100644 --- a/src/Provider/Azure.php +++ b/src/Provider/Azure.php @@ -1,4 +1,5 @@ urlLogin.$this->tenant."/oauth2/authorize"; } - - public function getBaseAccessTokenUrl(array $params) { + + public function getBaseAccessTokenUrl(array $params) + { return $this->urlLogin.$this->tenant."/oauth2/token"; } - - protected function checkResponse(ResponseInterface $response, $data) { - if(isset($data['odata.error'])) { + + protected function checkResponse(ResponseInterface $response, $data) + { + if (isset($data['odata.error'])) { + if (isset($data['odata.error']['message'])) { + $message = $data['odata.error']['message']; + } else { + $message = $response->getReasonPhrase(); + } + throw new IdentityProviderException( - (isset($data['odata.error']['message']) ? $data['odata.error']['message'] : $response->getReasonPhrase()), + $message, $response->getStatusCode(), $response ); } } - - protected function getDefaultScopes() { + + protected function getDefaultScopes() + { return []; } - - protected function createResourceOwner(array $response, AccessToken $token) { + + protected function createResourceOwner(array $response, AccessToken $token) + { return new AzureResourceOwner($response); } - - public function getResourceOwnerDetailsUrl(AccessToken $token) { + + public function getResourceOwnerDetailsUrl(AccessToken $token) + { return "me"; } - - public function getObjects($tenant, $ref, $objects = [], $accessToken) { + + public function getObjects($tenant, $ref, $accessToken, $objects = []) + { $response = $this->request('GET', $tenant."/".$ref, $accessToken, []); - if($response) { - $values = $response->value; - foreach($values as $value) { - $objects[] = $value; - } - if(isset($response['odata.nextLink'])) { - $nextLink = $response['odata.nextLink']; - return $this->getObjects($tenant, $nextLink, $objects, $accessToken); - } - else { - return $objects; - } - } + + if ($response) { + $values = $response->value; + foreach ($values as $value) { + $objects[] = $value; + } + if (isset($response['odata.nextLink'])) { + $nextLink = $response['odata.nextLink']; + + return $this->getObjects($tenant, $nextLink, $accessToken, $objects); + } else { + return $objects; + } + } } - public function get($ref, $accessToken) { + + public function get($ref, $accessToken) + { $response = $this->request('get', $ref, $accessToken); + return $this->wrapResponse($response); } - public function post($ref, $body, $accessToken) { + + public function post($ref, $body, $accessToken) + { $response = $this->request('post', $ref, $accessToken, ['body' => $body]); + return $this->wrapResponse($response); } - public function put($ref, $body, $accessToken) { + + public function put($ref, $body, $accessToken) + { $response = $this->request('put', $ref, $accessToken, ['body' => $body]); + return $this->wrapResponse($response); } - public function delete($ref, $accessToken) { + + public function delete($ref, $accessToken) + { $response = $this->request('delete', $ref, $accessToken); + return $this->wrapResponse($response); } - public function patch($ref, $body, $accessToken) { + + public function patch($ref, $body, $accessToken) + { $response = $this->request('patch', $ref, $accessToken, ['body' => $body]); + return $this->wrapResponse($response); } - - private function request($method, $ref, $accessToken, $options = []) { + + private function request($method, $ref, $accessToken, $options = []) + { $url = $this->urlAPI.$ref; - - $url .= (strrpos($url, "?") === FALSE) ? "?" : "&"; + + $url .= (strrpos($url, "?") === false) ? "?" : "&"; $url .= "api-version=".$this->API_VERSION; - + $request = $this->getAuthenticatedRequest($method, $url, $accessToken, $options); $response = $this->getResponse($request); - + return $response; } - - private function wrapResponse($response) { - if(empty($response)) return null; - else if(isset($response['value'])) return $response['value']; - else return $response; + + private function wrapResponse($response) + { + if (empty($response)) { + return null; + } elseif (isset($response['value'])) { + return $response['value']; + } + + return $response; } - - public function getClientId() { + + public function getClientId() + { return $this->clientId; } -} \ No newline at end of file +}