This commit is contained in:
2025-11-20 16:34:30 +01:00
parent 15d1a30e88
commit 4e4351e833
631 changed files with 130125 additions and 36318 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "stevenmaguire\/oauth2-keycloak",
"name": "stevenmaguire/oauth2-keycloak",
"description": "Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"authors": [
{
"name": "Steven Maguire",
"email": "stevenmaguire@gmail.com",
"homepage": "https:\/\/github.com\/stevenmaguire"
"homepage": "https://github.com/stevenmaguire"
}
],
"keywords": [
@@ -19,22 +19,22 @@
],
"require": {
"php": "~7.2 || ~8.0",
"league\/oauth2-client": "^2.0",
"firebase\/php-jwt": "^6.0"
"league/oauth2-client": "^2.0",
"firebase/php-jwt": "^6.0"
},
"require-dev": {
"phpunit\/phpunit": "~9.6.4",
"mockery\/mockery": "~1.5.0",
"squizlabs\/php_codesniffer": "~3.7.0"
"phpunit/phpunit": "~9.6.4",
"mockery/mockery": "~1.5.0",
"squizlabs/php_codesniffer": "~3.7.0"
},
"autoload": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\Stevenmaguire\\OAuth2\\Client\\": "src\/"
"Stevenmaguire\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\Stevenmaguire\\OAuth2\\Client\\Test\\": "test\/src\/"
"Stevenmaguire\\OAuth2\\Client\\Test\\": "test/src/"
}
},
"extra": {

View File

@@ -1,35 +1,53 @@
<?php
require 'vendor/autoload.php';
require 'vendor/autoload.php';
\session_start();
$provider = new \Pshowsso\Scope68f5e85e9608b\Stevenmaguire\OAuth2\Client\Provider\Keycloak(['authServerUrl' => '', 'realm' => '', 'clientId' => '', 'clientSecret' => '', 'redirectUri' => '', 'encryptionAlgorithm' => null, 'encryptionKey' => null, 'encryptionKeyPath' => null]);
if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $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, make sure HTTP sessions are enabled.');
} else {
// Try to get an access token (using the authorization coe grant)
try {
$token = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);
} catch (\Exception $e) {
exit('Failed to get access token: ' . $e->getMessage());
}
// 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!\n<br>', $user->getName());
} catch (\Exception $e) {
exit('Failed to get resource owner: ' . $e->getMessage());
}
// Use this to interact with an API on the users behalf
echo $token->getToken();
session_start();
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
'authServerUrl' => '',
'realm' => '',
'clientId' => '',
'clientSecret' => '',
'redirectUri' => '',
'encryptionAlgorithm' => null,
'encryptionKey' => null,
'encryptionKeyPath' => null
]);
if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $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, make sure HTTP sessions are enabled.');
} else {
// Try to get an access token (using the authorization coe grant)
try {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
} catch (Exception $e) {
exit('Failed to get access token: '.$e->getMessage());
}
// 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!\n<br>', $user->getName());
} catch (Exception $e) {
exit('Failed to get resource owner: '.$e->getMessage());
}
// Use this to interact with an API on the users behalf
echo $token->getToken();
}

View File

@@ -1,12 +1,12 @@
{
"name": "stevenmaguire\/oauth2-microsoft",
"name": "stevenmaguire/oauth2-microsoft",
"description": "Microsoft OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"authors": [
{
"name": "Steven Maguire",
"email": "stevenmaguire@gmail.com",
"homepage": "https:\/\/github.com\/stevenmaguire"
"homepage": "https://github.com/stevenmaguire"
}
],
"keywords": [
@@ -18,21 +18,21 @@
"microsoft"
],
"require": {
"league\/oauth2-client": "^2.0"
"league/oauth2-client": "^2.0"
},
"require-dev": {
"phpunit\/phpunit": "~4.0",
"mockery\/mockery": "~0.9",
"squizlabs\/php_codesniffer": "~2.0"
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\Stevenmaguire\\OAuth2\\Client\\": "src\/"
"Stevenmaguire\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\Stevenmaguire\\OAuth2\\Client\\Test\\": "tests\/src\/"
"Stevenmaguire\\OAuth2\\Client\\Test\\": "tests/src/"
}
}
}
}

View File

@@ -1,27 +1,35 @@
<?php
<?php namespace Stevenmaguire\OAuth2\Client\Test\Provider;
namespace Pshowsso\Scope68f5e85e9608b\Stevenmaguire\OAuth2\Client\Test\Provider;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Tool\QueryBuilderTrait;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use Mockery as m;
class MicrosoftTest extends \PHPUnit_Framework_TestCase
{
use QueryBuilderTrait;
protected $provider;
protected function setUp()
{
$this->provider = new \Pshowsso\Scope68f5e85e9608b\Stevenmaguire\OAuth2\Client\Provider\Microsoft(['clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none']);
$this->provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}
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);
@@ -30,6 +38,7 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase
$this->assertArrayHasKey('approval_prompt', $query);
$this->assertNotNull($this->provider->getState());
}
public function testScopes()
{
$scopeSeparator = ',';
@@ -39,67 +48,95 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase
$encodedScope = $this->buildQueryString($query);
$this->assertContains($encodedScope, $url);
}
public function testGetAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
$this->assertEquals('/oauth20_authorize.srf', $uri['path']);
}
public function testGetBaseAccessTokenUrl()
{
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/oauth20_token.srf', $uri['path']);
}
public function testSettingAuthEndpoints()
{
$customAuthUrl = uniqid();
$customTokenUrl = uniqid();
$customResourceOwnerUrl = uniqid();
$token = m::mock('Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Token\AccessToken');
$this->provider = new \Pshowsso\Scope68f5e85e9608b\Stevenmaguire\OAuth2\Client\Provider\Microsoft(['clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none', 'urlAuthorize' => $customAuthUrl, 'urlAccessToken' => $customTokenUrl, 'urlResourceOwnerDetails' => $customResourceOwnerUrl]);
$token = m::mock('League\OAuth2\Client\Token\AccessToken');
$this->provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
'urlAuthorize' => $customAuthUrl,
'urlAccessToken' => $customTokenUrl,
'urlResourceOwnerDetails' => $customResourceOwnerUrl
]);
$authUrl = $this->provider->getAuthorizationUrl();
$this->assertContains($customAuthUrl, $authUrl);
$tokenUrl = $this->provider->getBaseAccessTokenUrl([]);
$this->assertContains($customTokenUrl, $tokenUrl);
$resourceOwnerUrl = $this->provider->getResourceOwnerDetailsUrl($token);
$this->assertContains($customResourceOwnerUrl, $resourceOwnerUrl);
}
public function testGetAccessToken()
{
$response = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$response = m::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}');
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$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->assertLessThanOrEqual(time() + 3600, $token->getExpires());
$this->assertGreaterThanOrEqual(time(), $token->getExpires());
$this->assertEquals('mock_refresh_token', $token->getRefreshToken());
$this->assertNull($token->getResourceOwnerId());
}
public function testUserData()
{
$email = uniqid();
$firstname = uniqid();
$lastname = uniqid();
$name = uniqid();
$userId = rand(1000, 9999);
$userId = rand(1000,9999);
$urls = uniqid();
$postResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$userResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('{"id": ' . $userId . ', "name": "' . $name . '", "first_name": "' . $firstname . '", "last_name": "' . $lastname . '", "emails": {"preferred": "' . $email . '"}, "link": "' . $urls . '"}');
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('{"id": '.$userId.', "name": "'.$name.'", "first_name": "'.$firstname.'", "last_name": "'.$lastname.'", "emails": {"preferred": "'.$email.'"}, "link": "'.$urls.'"}');
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(2)->andReturn($postResponse, $userResponse);
$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($email, $user->getEmail());
$this->assertEquals($email, $user->toArray()['emails']['preferred']);
$this->assertEquals($firstname, $user->getFirstname());
@@ -110,22 +147,28 @@ class MicrosoftTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($name, $user->toArray()['name']);
$this->assertEquals($userId, $user->getId());
$this->assertEquals($userId, $user->toArray()['id']);
$this->assertEquals($urls . '/cid-' . $userId, $user->getUrls());
$this->assertEquals($urls . '/cid-' . $userId, $user->toArray()['link'] . '/cid-' . $user->toArray()['id']);
$this->assertEquals($urls.'/cid-'.$userId, $user->getUrls());
$this->assertEquals($urls.'/cid-'.$userId, $user->toArray()['link'].'/cid-'.$user->toArray()['id']);
}
/**
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
**/
public function testExceptionThrownWhenErrorObjectReceived()
{
$message = uniqid();
$postResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"error": {"code": "request_token_expired", "message": "' . $message . '"}}');
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"error": {"code": "request_token_expired", "message": "'.$message.'"}}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn(500);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($postResponse);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
}