Skip to content

Commit

Permalink
Adding Identity providers services (LM-Commons#49)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Jul 8, 2024
1 parent 89f1c68 commit 60b8ca7
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Identity/AuthenticationIdentityProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Identity;

use Laminas\Authentication\AuthenticationServiceInterface;

class AuthenticationIdentityProvider implements IdentityProviderInterface
{
public function __construct(
protected AuthenticationServiceInterface $authenticationService
) {
}

/**
* @inheritDoc
*/
public function getIdentity(): ?IdentityInterface
{
return $this->authenticationService->getIdentity();
}
}
26 changes: 26 additions & 0 deletions src/Identity/AuthenticationIdentityProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Identity;

use Laminas\Authentication\AuthenticationService;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* Factory to create the authentication identity provide
* @author Eric Richer <[email protected]>
*/
class AuthenticationIdentityProviderFactory implements FactoryInterface
{
/**
* @inheritDoc
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): AuthenticationIdentityProvider
{
// Get the Authentication provider
return new AuthenticationIdentityProvider($container->get(AuthenticationService::class));

}
}
12 changes: 12 additions & 0 deletions src/Identity/IdentityProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace LmcRbac\Identity;

interface IdentityProviderInterface
{
/**
* Get the identity
* @return IdentityInterface|null
*/
public function getIdentity(): ?IdentityInterface;
}
44 changes: 44 additions & 0 deletions test/Identity/AuthenticationIdentityProviderFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace LmcRbacTest\Identity;

use Laminas\ServiceManager\ServiceManager;
use LmcRbac\Identity\AuthenticationIdentityProviderFactory;

/**
* @covers \LmcRbac\Identity\AuthenticationIdentityProviderFactory
*/
class AuthenticationIdentityProviderFactoryTest extends \PHPUnit\Framework\TestCase
{
public function testFactory()
{
$serviceManager = new ServiceManager();
$serviceManager->setService(
'Laminas\Authentication\AuthenticationService',
$this->createMock('Laminas\Authentication\AuthenticationService')
);

$factory = new AuthenticationIdentityProviderFactory();
$authenticationProvider = $factory($serviceManager, 'LmcRbac\Identity\AuthenticationIdentityProvider');

$this->assertInstanceOf('LmcRbac\Identity\AuthenticationIdentityProvider', $authenticationProvider);
}
}
53 changes: 53 additions & 0 deletions test/Identity/AuthenticationIdentityProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace LmcRbacTest\Identity;

use LmcRbac\Identity\AuthenticationIdentityProvider;

/**
* @covers \LmcRbac\Identity\AuthenticationIdentityProvider
*/
class AuthenticationIdentityProviderTest extends \PHPUnit\Framework\TestCase
{
protected AuthenticationIdentityProvider $identityProvider;

/**
* @var \Laminas\Authentication\AuthenticationService|\PHPUnit\Framework\MockObject\MockObject
*/
protected $authenticationService;

public function setUp(): void
{
$this->authenticationService = $this->createMock('Laminas\Authentication\AuthenticationService');
$this->identityProvider = new AuthenticationIdentityProvider($this->authenticationService);
}

public function testCanReturnIdentity()
{
$identity = $this->createMock('LmcRbac\Identity\IdentityInterface');

$this->authenticationService->expects($this->once())
->method('getIdentity')
->willReturn($identity);

$this->assertSame($identity, $this->identityProvider->getIdentity());
}
}

0 comments on commit 60b8ca7

Please sign in to comment.