-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
246 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
|
||
# Ignore composer stuff | ||
/vendor/ | ||
composer.lock | ||
|
||
# Ignore developer stuff | ||
.idea | ||
node_modules | ||
.env | ||
.env |
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,8 @@ | ||
The MIT License | ||
Copyright © 2008, Concrete CMS Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,83 @@ | ||
# concrete5 provider for OAuth 2.0 Client | ||
|
||
This package provides concrete5 OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client). | ||
|
||
## Installation | ||
|
||
To install, use composer: | ||
|
||
``` | ||
composer require concrete5/oauth2-concrete5 | ||
``` | ||
|
||
## Requirements | ||
|
||
You must be running concrete5 8.5.0 or greater on your concrete5 website. Additionally, you must create an API Integration with Standard User Consent from the Dashboard > System > API > Integrations page. This integration will create a client ID and a client secret that you will use below. | ||
|
||
## Usage | ||
|
||
Usage is the same as The League's OAuth client, using `\Concrete\OAuth2\Client\Provider\Concrete5` as the provider. | ||
|
||
### Authorization Code Flow | ||
|
||
```php | ||
$provider = new Concrete\OAuth2\Client\Provider\Concrete5([ | ||
'clientId' => '{integrationClientId}', | ||
'clientSecret' => '{integrationClientSecret}', | ||
'redirectUri' => 'https://example.com/callback-url' | ||
]); | ||
|
||
if (!isset($_GET['code'])) { | ||
|
||
$url = $provider->getAuthorizationUrl(); | ||
$_SESSION['oauth2state'] = $provider->getState(); | ||
header('Location: '.$authUrl); | ||
exit; | ||
|
||
// Check given state against previously stored one to mitigate CSRF attack | ||
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { | ||
|
||
unset($_SESSION['oauth2state']); | ||
exit('Invalid state'); | ||
|
||
} else { | ||
|
||
// Try to get an access token (using the authorization code grant) | ||
$token = $provider->getAccessToken('authorization_code', [ | ||
'code' => $_GET['code'] | ||
]); | ||
|
||
// Optional: Now you have a token you can look up a users profile data | ||
try { | ||
|
||
// We got an access token, let's now get the user's details | ||
$user = $provider->getResourceOwner($token); | ||
|
||
// Use these details to create a new profile | ||
printf('Hello %s!', $user->getUserName()); | ||
|
||
} catch (Exception $e) { | ||
|
||
// Failed to get user details | ||
exit('Oh dear...'); | ||
} | ||
|
||
// Use this to interact with an API on the users behalf | ||
echo $token->getToken(); | ||
} | ||
``` | ||
|
||
## Testing | ||
|
||
``` bash | ||
$ ./vendor/bin/phpunit | ||
``` | ||
|
||
## Credits | ||
|
||
- [Andrew Embler](http://andrewembler.com) | ||
|
||
|
||
## License | ||
|
||
The MIT License (MIT). Please see LICENSE.TXT for more info. |
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,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="./vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="true" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./test/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,134 @@ | ||
<?php | ||
|
||
namespace Concrete\OAuth2\Client\Test\Provider; | ||
|
||
use Concrete\OAuth2\Client\Provider\Concrete5; | ||
use Mockery as m; | ||
use ReflectionClass; | ||
|
||
class Concrete5Test extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $provider; | ||
|
||
protected function setUp() | ||
{ | ||
$this->provider = new Concrete5([ | ||
'clientId' => 'mock_client_id', | ||
'clientSecret' => 'mock_secret', | ||
'redirectUri' => 'http://none', | ||
'baseUrl' => 'http://example.com', | ||
]); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
m::close(); | ||
parent::tearDown(); | ||
} | ||
|
||
|
||
public function testAuthorizationUrl() | ||
{ | ||
$url = $this->provider->getAuthorizationUrl(); | ||
$uri = parse_url($url); | ||
parse_str($uri['query'], $query); | ||
$this->assertArrayHasKey('client_id', $query); | ||
$this->assertArrayHasKey('redirect_uri', $query); | ||
$this->assertArrayHasKey('state', $query); | ||
$this->assertArrayHasKey('scope', $query); | ||
$this->assertArrayHasKey('response_type', $query); | ||
$this->assertArrayHasKey('approval_prompt', $query); | ||
$this->assertNotNull($this->provider->getState()); | ||
} | ||
|
||
public function testGetBasenUrl() | ||
{ | ||
$url = $this->provider->getBaseUrl(); | ||
$this->assertEquals('http://example.com', $url); | ||
} | ||
|
||
public function testGetAuthorizationUrl() | ||
{ | ||
$url = $this->provider->getAuthorizationUrl(); | ||
$uri = parse_url($url); | ||
$this->assertEquals('/oauth/2.0/authorize', $uri['path']); | ||
} | ||
|
||
public function testGetBaseAccessTokenUrl() | ||
{ | ||
$params = []; | ||
$url = $this->provider->getBaseAccessTokenUrl($params); | ||
$uri = parse_url($url); | ||
$this->assertEquals('/oauth/2.0/token', $uri['path']); | ||
} | ||
|
||
public function testGetAccessToken() | ||
{ | ||
$response = m::mock('Psr\Http\Message\ResponseInterface'); | ||
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}'); | ||
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | ||
$response->shouldReceive('getStatusCode')->andReturn(200); | ||
$client = m::mock('GuzzleHttp\ClientInterface'); | ||
$client->shouldReceive('send')->times(1)->andReturn($response); | ||
$this->provider->setHttpClient($client); | ||
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | ||
$this->assertEquals('mock_access_token', $token->getToken()); | ||
$this->assertNull($token->getExpires()); | ||
$this->assertNull($token->getRefreshToken()); | ||
$this->assertNull($token->getResourceOwnerId()); | ||
} | ||
|
||
public function testUserData() | ||
{ | ||
$response_data = [ | ||
'id' => rand(1000, 9999), | ||
'username' => uniqid(), | ||
'email' => uniqid(), | ||
]; | ||
|
||
$response_data_wrapped = ['data' => $response_data]; | ||
|
||
|
||
$postResponse = m::mock('Psr\Http\Message\ResponseInterface'); | ||
$postResponse->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token","expires_in":"3600","token_type":"Bearer","scope":"openid email profile","id_token":"mock_token_id"}'); | ||
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | ||
$postResponse->shouldReceive('getStatusCode')->andReturn(200); | ||
$userResponse = m::mock('Psr\Http\Message\ResponseInterface'); | ||
$userResponse->shouldReceive('getBody')->andReturn(json_encode($response_data_wrapped)); | ||
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | ||
$userResponse->shouldReceive('getStatusCode')->andReturn(200); | ||
$client = m::mock('GuzzleHttp\ClientInterface'); | ||
$client->shouldReceive('send') | ||
->times(2) | ||
->andReturn($postResponse, $userResponse); | ||
$this->provider->setHttpClient($client); | ||
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | ||
$user = $this->provider->getResourceOwner($token); | ||
|
||
$this->assertEquals($response_data['id'], $user->getId()); | ||
$this->assertEquals($response_data['id'], $user->toArray()['id']); | ||
$this->assertEquals($response_data['email'], $user->getUserEmail()); | ||
$this->assertEquals($response_data['email'], $user->toArray()['email']); | ||
$this->assertEquals($response_data['username'], $user->getUserName()); | ||
$this->assertEquals($response_data['username'], $user->toArray()['username']); | ||
} | ||
|
||
/** | ||
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException | ||
*/ | ||
public function testExceptionThrownWhenErrorObjectReceived() | ||
{ | ||
$message = uniqid(); | ||
$status = rand(400, 600); | ||
$postResponse = m::mock('Psr\Http\Message\ResponseInterface'); | ||
$postResponse->shouldReceive('getBody')->andReturn(' {"error":"'.$message.'"}'); | ||
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | ||
$postResponse->shouldReceive('getStatusCode')->andReturn($status); | ||
$client = m::mock('GuzzleHttp\ClientInterface'); | ||
$client->shouldReceive('send') | ||
->times(1) | ||
->andReturn($postResponse); | ||
$this->provider->setHttpClient($client); | ||
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | ||
} | ||
} |