-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Barnetik/master
Added Azure Token Provider
- Loading branch information
Showing
4 changed files
with
438 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace MatthiasNoback\MicrosoftOAuth; | ||
|
||
use Buzz\Browser; | ||
use MatthiasNoback\Exception\RequestFailedException; | ||
use MatthiasNoback\Exception\InvalidResponseException; | ||
|
||
class AzureTokenProvider implements AccessTokenProviderInterface | ||
{ | ||
const AUTH_URL = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'; | ||
|
||
/** | ||
* @var \Buzz\Browser | ||
*/ | ||
private $browser; | ||
|
||
/** | ||
* @var string The client id of your application | ||
*/ | ||
private $azureKey; | ||
|
||
/** | ||
* @var AccessTokenCacheInterface|null | ||
*/ | ||
private $accessTokenCache; | ||
|
||
/** | ||
* The AccessTokenProvider requires a Buzz browser instance, and both a client id | ||
* and a client secret. You can obtain these by registering your application | ||
* at https://datamarket.azure.com/developer/applications | ||
* | ||
* @param \Buzz\Browser $browser The browser to use for fetching access tokens | ||
* @param string $azureKey The azure key for Translator service | ||
*/ | ||
public function __construct(Browser $browser, $azureKey) | ||
{ | ||
$this->browser = $browser; | ||
$this->azureKey = $azureKey; | ||
} | ||
|
||
public function setCache(AccessTokenCacheInterface $accessTokenCache) | ||
{ | ||
$this->accessTokenCache = $accessTokenCache; | ||
} | ||
|
||
public function getAccessToken($scope, $grantType) | ||
{ | ||
if ($this->accessTokenCache !== null && $this->accessTokenCache->has($scope, $grantType)) { | ||
return $this->accessTokenCache->get($scope, $grantType); | ||
} | ||
|
||
$accessToken = $this->authorize($scope, $grantType); | ||
|
||
if ($this->accessTokenCache !== null) { | ||
$this->accessTokenCache->set($scope, $grantType, $accessToken); | ||
} | ||
|
||
return $accessToken; | ||
} | ||
|
||
private function authorize($scope, $grantType) | ||
{ | ||
try { | ||
$response = $this->browser->post( | ||
self::AUTH_URL . "?Subscription-Key=" . urlencode($this->azureKey), | ||
array('Content-Length' => 0) | ||
); | ||
} | ||
catch (\Exception $previous) { | ||
throw new RequestFailedException(sprintf( | ||
'Request failed: %s', | ||
$previous->getMessage() | ||
), null, $previous); | ||
} | ||
|
||
if (!$response->isSuccessful()) { | ||
throw new RequestFailedException(sprintf( | ||
'Call to Auth server failed, %d: %s', | ||
$response->getStatusCode(), | ||
$response->getReasonPhrase() | ||
)); | ||
} | ||
|
||
/* @var $response \Buzz\Message\Response */ | ||
return $response->getContent(); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
tests/MatthiasNoback/MicrosoftOAuth/AzureTokenProviderTest.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
namespace MatthiasNoback\Tests\MicrosoftOAuth; | ||
|
||
use MatthiasNoback\MicrosoftOAuth\AzureTokenProvider; | ||
|
||
class AzuresTokenProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGetTokenWithoutCache() | ||
{ | ||
$azureKey = 'azureKey'; | ||
$accessToken = 'accessToken'; | ||
$scope = 'theScope'; | ||
$grantType = 'theGrantType'; | ||
|
||
$response = $this->createMockResponse($accessToken); | ||
|
||
$browser = $this->createMockBrowser(); | ||
$browser | ||
->expects($this->once()) | ||
->method('post') | ||
->with( | ||
'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=' . $azureKey, | ||
array('Content-Length' => 0) | ||
) | ||
->will($this->returnValue($response)); | ||
$accessTokenProvider = new AzureTokenProvider($browser, $azureKey); | ||
|
||
$actualAccessToken = $accessTokenProvider->getAccessToken($scope, $grantType); | ||
|
||
$this->assertSame($accessToken, $actualAccessToken); | ||
} | ||
|
||
public function testGetTokenWithCacheMiss() | ||
{ | ||
$scope = 'theScope'; | ||
$grantType = 'theGrantType'; | ||
|
||
$accessToken = 'accessToken'; | ||
|
||
// the cache does not contain an access token yet | ||
$accessTokenCache = $this->createMockAccessTokenCache(); | ||
$accessTokenCache | ||
->expects($this->once()) | ||
->method('has') | ||
->with($scope, $grantType) | ||
->will($this->returnValue(false)); | ||
|
||
// the browser will used to fetch a fresh access token | ||
$response = $this->createMockResponse($accessToken); | ||
$browser = $this->createMockBrowser(); | ||
$browser | ||
->expects($this->once()) | ||
->method('post') | ||
->will($this->returnValue($response)); | ||
|
||
// finally, the access token should be stored in the cache | ||
$accessTokenCache | ||
->expects($this->once()) | ||
->method('set') | ||
->with($scope, $grantType, $accessToken); | ||
|
||
|
||
$accessTokenProvider = new AzureTokenProvider($browser, 'azureKey'); | ||
$accessTokenProvider->setCache($accessTokenCache); | ||
|
||
$actualAccessToken = $accessTokenProvider->getAccessToken($scope, $grantType); | ||
|
||
$this->assertSame($accessToken, $actualAccessToken); | ||
} | ||
|
||
public function testGetTokenWithCacheHit() | ||
{ | ||
$scope = 'theScope'; | ||
$grantType = 'theGrantType'; | ||
|
||
$accessToken = 'accessToken'; | ||
|
||
// the cache already contains an access token | ||
$accessTokenCache = $this->createMockAccessTokenCache(); | ||
$accessTokenCache | ||
->expects($this->once()) | ||
->method('has') | ||
->with($scope, $grantType) | ||
->will($this->returnValue(true)); | ||
|
||
// the browser should not be used | ||
$browser = $this->createMockBrowser(); | ||
$browser | ||
->expects($this->never()) | ||
->method('post'); | ||
|
||
// the access token will be retrieved from the cache | ||
$accessTokenCache | ||
->expects($this->once()) | ||
->method('get') | ||
->with($scope, $grantType) | ||
->will($this->returnValue($accessToken)); | ||
|
||
$accessTokenProvider = new AzureTokenProvider($browser, 'azureKey'); | ||
$accessTokenProvider->setCache($accessTokenCache); | ||
|
||
$accessTokenProvider->getAccessToken($scope, $grantType); | ||
} | ||
|
||
private function createMockBrowser() | ||
{ | ||
return $this | ||
->getMockBuilder('Buzz\\Browser') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
|
||
private function createMockResponse($content) | ||
{ | ||
$response = $this->getMock('Buzz\Message\Response'); | ||
$response | ||
->expects($this->any()) | ||
->method('getContent') | ||
->will($this->returnValue($content)); | ||
|
||
$response | ||
->expects($this->any()) | ||
->method('isSuccessful') | ||
->will($this->returnValue(true)); | ||
|
||
return $response; | ||
} | ||
|
||
private function createMockAccessTokenCache() | ||
{ | ||
return $this->getMock('MatthiasNoback\MicrosoftOAuth\AccessTokenCacheInterface'); | ||
} | ||
} |
Oops, something went wrong.