-
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
8 changed files
with
277 additions
and
31 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
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\OpenID\Federation\Factories; | ||
|
||
use SimpleSAML\OpenID\Federation\TrustChain; | ||
use SimpleSAML\OpenID\Federation\TrustChainBag; | ||
|
||
class TrustChainBagFactory | ||
{ | ||
public function build(TrustChain $trustChain, TrustChain ...$trustChains): TrustChainBag | ||
{ | ||
return new TrustChainBag($trustChain, ...$trustChains); | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\OpenID\Federation; | ||
|
||
use SimpleSAML\OpenID\Exceptions\TrustChainException; | ||
|
||
class TrustChainBag | ||
{ | ||
/** @var \SimpleSAML\OpenID\Federation\TrustChain[] */ | ||
protected array $trustChains; | ||
|
||
public function __construct(TrustChain $trustChain, TrustChain ...$trustChains) | ||
{ | ||
$this->add($trustChain, ...$trustChains); | ||
} | ||
|
||
public function add(TrustChain $trustChain, TrustChain ...$trustChains): void | ||
{ | ||
$this->trustChains[] = $trustChain; | ||
$this->trustChains += $trustChains; | ||
|
||
// Order the chains from shortest to longest one. | ||
usort($this->trustChains, function (TrustChain $a, TrustChain $b) { | ||
return $a->getResolvedLength() <=> $b->getResolvedLength(); | ||
}); | ||
} | ||
|
||
/** | ||
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException | ||
*/ | ||
public function getShortest(): TrustChain | ||
{ | ||
($shortestChain = reset($this->trustChains)) || throw new TrustChainException('Invalid trust chain bag.'); | ||
return $shortestChain; | ||
} | ||
|
||
/** | ||
* Get the shortest Trust Chain prioritized by Trust Anchor ID. Returns null if none of the given Trust Anchor IDs | ||
* is found. | ||
* | ||
* @throws \SimpleSAML\OpenID\Exceptions\EntityStatementException | ||
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException | ||
* @throws \SimpleSAML\OpenID\Exceptions\JwsException | ||
*/ | ||
public function getShortestByTrustAnchorPriority(string $trustAnchorId, string ...$trustAnchorIds): ?TrustChain | ||
{ | ||
// Map of trust anchor identifiers to their order. | ||
$prioritizedTrustAnchorIds = array_flip([$trustAnchorId, ...$trustAnchorIds]); | ||
|
||
$prioritizedChains = $this->trustChains; | ||
|
||
usort($prioritizedChains, function (TrustChain $a, TrustChain $b) use ($prioritizedTrustAnchorIds) { | ||
// Get defined position, or default to high value if not found. | ||
$posA = $prioritizedTrustAnchorIds[$a->getResolvedTrustAnchor()->getIssuer()] ?? PHP_INT_MAX; | ||
$posB = $prioritizedTrustAnchorIds[$b->getResolvedTrustAnchor()->getIssuer()] ?? PHP_INT_MAX; | ||
|
||
return $posA <=> $posB; | ||
}); | ||
|
||
($prioritizedChain = reset($prioritizedChains)) || throw new TrustChainException('Invalid trust chain bag.'); | ||
return array_key_exists($prioritizedChain->getResolvedTrustAnchor()->getIssuer(), $prioritizedTrustAnchorIds) ? | ||
$prioritizedChain : null; | ||
} | ||
|
||
/** | ||
* @return \SimpleSAML\OpenID\Federation\TrustChain[] | ||
*/ | ||
public function getAll(): array | ||
{ | ||
return $this->trustChains; | ||
} | ||
} |
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\OpenID\Federation; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\OpenID\Federation\EntityStatement; | ||
use SimpleSAML\OpenID\Federation\TrustChain; | ||
use SimpleSAML\OpenID\Federation\TrustChainBag; | ||
|
||
#[CoversClass(TrustChainBag::class)] | ||
class TrustChainBagTest extends TestCase | ||
{ | ||
protected MockObject $trustChainMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->trustChainMock = $this->createMock(TrustChain::class); | ||
} | ||
|
||
protected function sut( | ||
?TrustChain $trustChain = null, | ||
): TrustChainBag { | ||
$trustChain ??= $this->trustChainMock; | ||
|
||
return new TrustChainBag($trustChain); | ||
} | ||
|
||
public function testCanCreateInstance(): void | ||
{ | ||
$this->assertInstanceOf(TrustChainBag::class, $this->sut()); | ||
} | ||
|
||
public function testCanAdd(): void | ||
{ | ||
$sut = $this->sut(); | ||
$this->assertCount(1, $sut->getAll()); | ||
$sut->add($this->trustChainMock); | ||
$this->assertCount(2, $sut->getAll()); | ||
} | ||
|
||
public function testCanGetShortest(): void | ||
{ | ||
$shortest = $this->createMock(TrustChain::class); | ||
$shortest->method('getResolvedLength')->willReturn(2); | ||
$mid = $this->createMock(TrustChain::class); | ||
$mid->method('getResolvedLength')->willReturn(3); | ||
$longest = $this->createMock(TrustChain::class); | ||
$longest->method('getResolvedLength')->willReturn(4); | ||
|
||
$sut = $this->sut($longest); | ||
|
||
$this->assertCount(1, $sut->getAll()); | ||
$this->assertSame(4, $sut->getShortest()->getResolvedLength()); | ||
|
||
$sut->add($mid); | ||
$this->assertCount(2, $sut->getAll()); | ||
$this->assertSame(3, $sut->getShortest()->getResolvedLength()); | ||
|
||
$sut->add($shortest); | ||
$this->assertCount(3, $sut->getAll()); | ||
$this->assertSame(2, $sut->getShortest()->getResolvedLength()); | ||
} | ||
|
||
public function testCanGetShortestByTrustAnchorPriority(): void | ||
{ | ||
$trustAnchor1 = $this->createMock(EntityStatement::class); | ||
$trustAnchor1->method('getIssuer')->willReturn('ta1'); | ||
$chain1 = $this->createMock(TrustChain::class); | ||
$chain1->method('getResolvedTrustAnchor')->willReturn($trustAnchor1); | ||
$chain1->method('getResolvedLength')->willReturn(2); | ||
|
||
$trustAnchor2 = $this->createMock(EntityStatement::class); | ||
$trustAnchor2->method('getIssuer')->willReturn('ta2'); | ||
$chain2 = $this->createMock(TrustChain::class); | ||
$chain2->method('getResolvedTrustAnchor')->willReturn($trustAnchor2); | ||
$chain2->method('getResolvedLength')->willReturn(3); | ||
|
||
$sut = $this->sut($chain2); | ||
$sut->add($chain1); | ||
|
||
$this->assertSame($chain1, $sut->getShortestByTrustAnchorPriority('ta1')); | ||
$this->assertSame($sut->getShortestByTrustAnchorPriority('ta1', 'ta2') | ||
->getResolvedTrustAnchor()->getIssuer(), 'ta1'); | ||
Check warning on line 87 in tests/src/Federation/TrustChainBagTest.php
|
||
|
||
$this->assertSame($chain2, $sut->getShortestByTrustAnchorPriority('ta2')); | ||
$this->assertSame($sut->getShortestByTrustAnchorPriority('ta2', 'ta1') | ||
->getResolvedTrustAnchor()->getIssuer(), 'ta2'); | ||
Check warning on line 91 in tests/src/Federation/TrustChainBagTest.php
|
||
|
||
// Can get chain even if some trust anchors are unknown. | ||
$this->assertSame($chain2, $sut->getShortestByTrustAnchorPriority('unknown', 'ta2')); | ||
$this->assertSame($sut->getShortestByTrustAnchorPriority('unknown', 'ta2', 'ta1') | ||
->getResolvedTrustAnchor()->getIssuer(), 'ta2'); | ||
Check warning on line 96 in tests/src/Federation/TrustChainBagTest.php
|
||
|
||
// Returns null if Trust Anchor is unknown. | ||
$this->assertNull($sut->getShortestByTrustAnchorPriority('unknown')); | ||
} | ||
} |
Oops, something went wrong.