Skip to content

Commit 17f069d

Browse files
committed
Add DecisionType-type
1 parent 04ef207 commit 17f069d

File tree

7 files changed

+146
-38
lines changed

7 files changed

+146
-38
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"psr/http-message": "~2.0",
3232
"psr/log": "~2.3.1 || ~3.0.0",
3333
"simplesamlphp/assert": "~1.8.1",
34-
"simplesamlphp/xml-common": "dev-feature/xsd-types",
34+
"simplesamlphp/xml-common": "dev-master",
3535
"simplesamlphp/xml-security": "dev-feature/xsd-types",
3636
"simplesamlphp/xml-soap": "dev-feature/xsd-types"
3737
},

src/Type/DecisionTypeValue.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\XML\saml\DecisionTypeEnum;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
use SimpleSAML\XML\Type\StringValue;
11+
12+
use function array_column;
13+
14+
/**
15+
* @package simplesamlphp/saml2
16+
*/
17+
class DecisionTypeValue extends StringValue
18+
{
19+
/** @var string */
20+
public const SCHEMA_TYPE = 'decisionType';
21+
22+
23+
/**
24+
* Validate the value.
25+
*
26+
* @param string $value The value
27+
* @throws \Exception on failure
28+
* @return void
29+
*/
30+
protected function validateValue(string $value): void
31+
{
32+
Assert::oneOf(
33+
$this->sanitizeValue($value),
34+
array_column(DecisionTypeEnum::cases(), 'value'),
35+
SchemaViolationException::class,
36+
);
37+
}
38+
39+
40+
/**
41+
* @param \SimpleSAML\SAML2\XML\saml\DecisionTypeEnum $value
42+
* @return static
43+
*/
44+
public static function fromEnum(DecisionTypeEnum $value): static
45+
{
46+
return new static($value->value);
47+
}
48+
49+
50+
/**
51+
* @return \SimpleSAML\SAML2\XML\saml\DecisionTypeEnum $value
52+
*/
53+
public function toEnum(): DecisionTypeEnum
54+
{
55+
return DecisionTypeEnum::from($this->getValue());
56+
}
57+
}

src/XML/Decision.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/XML/saml/AuthzDecisionStatement.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use DOMElement;
88
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\SAML2\Constants as C;
10-
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11-
use SimpleSAML\SAML2\Type\SAMLStringValue;
12-
use SimpleSAML\SAML2\XML\Decision;
10+
use SimpleSAML\SAML2\Type\DecisionTypeValue;
1311
use SimpleSAML\XML\Exception\{
1412
InvalidDOMElementException,
1513
MissingElementException,
@@ -18,10 +16,9 @@
1816
};
1917
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
2018
use SimpleSAML\XML\Type\AnyURIValue;
21-
use ValueError;
2219

2320
use function array_pop;
24-
use function sprintf;
21+
use function strval;
2522

2623
/**
2724
* Class representing a SAML2 AuthzDecisionStatement
@@ -35,15 +32,15 @@ final class AuthzDecisionStatement extends AbstractStatementType implements Sche
3532
/**
3633
* Initialize an AuthzDecisionStatement.
3734
*
38-
* @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue $resource
39-
* @param \SimpleSAML\SAML2\XML\Decision $decision
35+
* @param \SimpleSAML\XML\Type\AnyURIValue $resource
36+
* @param \SimpleSAML\SAML2\Type\DecisionTypeValue $decision
4037
* @param \SimpleSAML\SAML2\XML\saml\Action[] $action
4138
* @param \SimpleSAML\SAML2\XML\saml\Evidence|null $evidence
4239
*/
4340
public function __construct(
4441
// Uses the base AnyURIValue because the SAML-specification allows this attribute to be an empty string
4542
protected AnyURIValue $resource,
46-
protected Decision $decision,
43+
protected DecisionTypeValue $decision,
4744
protected array $action,
4845
protected ?Evidence $evidence = null,
4946
) {
@@ -66,9 +63,9 @@ public function getResource(): AnyURIValue
6663
/**
6764
* Collect the value of the decision-property
6865
*
69-
* @return \SimpleSAML\SAML2\XML\Decision
66+
* @return \SimpleSAML\SAML2\Type\DecisionTypeValue
7067
*/
71-
public function getDecision(): Decision
68+
public function getDecision(): DecisionTypeValue
7269
{
7370
return $this->decision;
7471
}
@@ -129,16 +126,10 @@ public static function fromXML(DOMElement $xml): static
129126
TooManyElementsException::class,
130127
);
131128

132-
$decision = self::getAttribute($xml, 'Decision', SAMLStringValue::class);
133-
try {
134-
$decision = Decision::from($decision->getValue());
135-
} catch (ValueError) {
136-
throw new ProtocolViolationException(sprintf('Unknown value \'%s\' for Decision attribute.', $decision));
137-
}
138129

139130
return new static(
140131
self::getAttribute($xml, 'Resource', AnyURIValue::class),
141-
$decision,
132+
self::getAttribute($xml, 'Decision', DecisionTypeValue::class),
142133
$action,
143134
array_pop($evidence),
144135
);
@@ -155,8 +146,8 @@ public function toXML(?DOMElement $parent = null): DOMElement
155146
{
156147
$e = $this->instantiateParentElement($parent);
157148

158-
$e->setAttribute('Resource', $this->getResource()->getValue());
159-
$e->setAttribute('Decision', $this->getDecision()->value);
149+
$e->setAttribute('Resource', strval($this->getResource()));
150+
$e->setAttribute('Decision', strval($this->getDecision()));
160151

161152
foreach ($this->getAction() as $action) {
162153
$action->toXML($e);

src/XML/saml/DecisionTypeEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\XML\saml;
6+
7+
enum DecisionTypeEnum: string
8+
{
9+
case Deny = 'Deny';
10+
case Indeterminate = 'Indeterminate';
11+
case Permit = 'Permit';
12+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\SAML2\Type;
6+
7+
use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, DependsOnClass};
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\SAML2\Type\DecisionTypeValue;
10+
use SimpleSAML\SAML2\XML\saml\DecisionTypeEnum;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
13+
/**
14+
* Class \SimpleSAML\Test\SAML2\Type\DecisionTypeValueTest
15+
*
16+
* @package simplesamlphp/saml2
17+
*/
18+
#[CoversClass(DecisionTypeValue::class)]
19+
final class DecisionTypeValueTest extends TestCase
20+
{
21+
/**
22+
* @param string $decisionType
23+
* @param bool $expected
24+
*/
25+
#[DataProvider('provideDecisionType')]
26+
public function testDecisionTypeValue(string $decisionType, bool $shouldPass): void
27+
{
28+
try {
29+
DecisionTypeValue::fromString($decisionType);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* Test helpers
39+
*/
40+
public function testHelpers(): void
41+
{
42+
$x = DecisionTypeValue::fromEnum(DecisionTypeEnum::Deny);
43+
$this->assertEquals(DecisionTypeEnum::Deny, $x->toEnum());
44+
45+
$y = DecisionTypeValue::fromString('Deny');
46+
$this->assertEquals(DecisionTypeEnum::Deny, $y->toEnum());
47+
}
48+
49+
50+
/**
51+
* @return array<string, array{0: string, 1: string}>
52+
*/
53+
public static function provideDecisionType(): array
54+
{
55+
return [
56+
'deny' => ['Deny', true],
57+
'indeterminate' => ['Indeterminate', true],
58+
'permit' => ['Permit', true],
59+
'undefined' => ['undefined', false],
60+
'empty' => ['', false],
61+
];
62+
}
63+
}

tests/SAML2/XML/saml/AuthzDecisionStatementTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use DOMDocument;
88
use PHPUnit\Framework\Attributes\{CoversClass, Group};
99
use PHPUnit\Framework\TestCase;
10-
use SimpleSAML\SAML2\Type\{SAMLAnyURIValue, SAMLStringValue};
11-
use SimpleSAML\SAML2\XML\Decision;
10+
use SimpleSAML\SAML2\Type\{DecisionTypeValue, SAMLAnyURIValue, SAMLStringValue};
1211
use SimpleSAML\SAML2\XML\saml\{
1312
AbstractSamlElement,
1413
AbstractStatement,
1514
Action,
1615
AuthzDecisionStatement,
16+
DecisionTypeEnum,
1717
Evidence,
1818
};
1919
use SimpleSAML\XML\DOMDocumentFactory;
@@ -62,7 +62,7 @@ public function testMarshalling(): void
6262
{
6363
$authzDecisionStatement = new AuthzDecisionStatement(
6464
SAMLAnyURIValue::fromString('urn:x-simplesamlphp:resource'),
65-
Decision::PERMIT,
65+
DecisionTypeValue::fromEnum(DecisionTypeEnum::Permit),
6666
[
6767
new Action(
6868
SAMLAnyURIValue::fromString('urn:x-simplesamlphp:namespace'),

0 commit comments

Comments
 (0)