Skip to content

Commit

Permalink
Merge pull request #5 from stevenmaguire/add-psr2-compliance
Browse files Browse the repository at this point in the history
Add psr2 compliance
  • Loading branch information
hajekj committed Nov 17, 2015
2 parents c04b54d + 9ed6fe2 commit b00a240
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 60 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand All @@ -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.
The MIT License (MIT). Please see [License File](https://github.com/thenetworg/oauth2-azure/blob/master/LICENSE) for more information.
147 changes: 94 additions & 53 deletions src/Provider/Azure.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace TheNetworg\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
Expand All @@ -7,100 +8,140 @@
use League\OAuth2\Client\Token\AccessToken;
use Psr\Http\Message\ResponseInterface;

class Azure extends AbstractProvider {
class Azure extends AbstractProvider
{
use BearerAuthorizationTrait;

public $urlLogin = "https://login.microsoftonline.com/";

public $urlLogin = "https://login.microsoftonline.com/";

public $tenant = "common";
public $urlAPI = "https://graph.windows.net/";
public $API_VERSION = "1.6";

public function getBaseAuthorizationUrl() {

public $urlAPI = "https://graph.windows.net/";

public $API_VERSION = "1.6";

public function getBaseAuthorizationUrl()
{
return $this->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;
}
}
}
25 changes: 21 additions & 4 deletions src/Provider/AzureResourceOwner.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<?php

namespace TheNetworg\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

class AzureResourceOwner implements ResourceOwnerInterface
{
/**
* Response payload
*
* @var array
*/
protected $response;

public function __construct($response = []) {
/**
* Creates new azure resource owner.
*
* @param array $response
*/
public function __construct($response = [])
{
$this->response = $response;
}

public function getId() {

/**
* Retrieves id of azure resource owner.
*
* @return string|null
*/
public function getId()
{
return $this->response['objectId'] ?: null;
}
}

0 comments on commit b00a240

Please sign in to comment.