Skip to content

Commit

Permalink
Constructor promotion (#317)
Browse files Browse the repository at this point in the history
* Take advantage of constructor property promotion

* Lots of cleanup & trailing commas
  • Loading branch information
tvdijen committed Jan 26, 2023
1 parent 7a371e4 commit 4a8d983
Show file tree
Hide file tree
Showing 301 changed files with 2,999 additions and 7,394 deletions.
37 changes: 6 additions & 31 deletions src/SAML2/Assertion/Decrypter.php
Expand Up @@ -21,27 +21,6 @@

class Decrypter
{
/**
* @var \SimpleSAML\SAML2\Configuration\IdentityProvider
*/
private IdentityProvider $identityProvider;

/**
* @var \SimpleSAML\SAML2\Configuration\ServiceProvider
*/
private ServiceProvider $serviceProvider;

/**
* @var \SimpleSAML\SAML2\Certificate\PrivateKeyLoader
*/
private PrivateKeyLoader $privateKeyLoader;

/**
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;


/**
* Constructor for Decrypter.
*
Expand All @@ -51,15 +30,11 @@ class Decrypter
* @param \SimpleSAML\SAML2\Certificate\PrivateKeyLoader $privateKeyLoader
*/
public function __construct(
LoggerInterface $logger,
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider,
PrivateKeyLoader $privateKeyLoader
private LoggerInterface $logger,
private IdentityProvider $identityProvider,
private ServiceProvider $serviceProvider,
private PrivateKeyLoader $privateKeyLoader,
) {
$this->logger = $logger;
$this->identityProvider = $identityProvider;
$this->serviceProvider = $serviceProvider;
$this->privateKeyLoader = $privateKeyLoader;
}


Expand Down Expand Up @@ -103,14 +78,14 @@ public function decrypt(EncryptedAssertion $assertion): Assertion
'Could not decrypt assertion with key "#%d", "%s" thrown: "%s"',
$index,
get_class($e),
$e->getMessage()
$e->getMessage(),
));
}
}

throw new NotDecryptedException(sprintf(
'Could not decrypt the assertion, tried with "%d" keys. See the debug log for more information',
count($decryptionKeys)
count($decryptionKeys),
));
}
}
69 changes: 13 additions & 56 deletions src/SAML2/Assertion/Processor.php
Expand Up @@ -22,42 +22,6 @@

class Processor
{
/**
* @var \SimpleSAML\SAML2\Assertion\Decrypter
*/
private Decrypter $decrypter;

/**
* @var \SimpleSAML\SAML2\Assertion\Validation\AssertionValidator
*/
private AssertionValidator $assertionValidator;

/**
* @var \SimpleSAML\SAML2\Assertion\Validation\SubjectConfirmationValidator
*/
private SubjectConfirmationValidator $subjectConfirmationValidator;

/**
* @var \SimpleSAML\SAML2\Assertion\Transformer\TransformerInterface
*/
private TransformerInterface $transformer;

/**
* @var \SimpleSAML\SAML2\Signature\Validator
*/
private Validator $signatureValidator;

/**
* @var \SimpleSAML\SAML2\Configuration\IdentityProvider
*/
private IdentityProvider $identityProviderConfiguration;

/**
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;


/**
* @param \SimpleSAML\SAML2\Assertion\Decrypter $decrypter
* @param \SimpleSAML\SAML2\Signature\Validator $signatureValidator
Expand All @@ -68,21 +32,14 @@ class Processor
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
Decrypter $decrypter,
Validator $signatureValidator,
AssertionValidator $assertionValidator,
SubjectConfirmationValidator $subjectConfirmationValidator,
TransformerInterface $transformer,
IdentityProvider $identityProviderConfiguration,
LoggerInterface $logger
private Decrypter $decrypter,
private Validator $signatureValidator,
private AssertionValidator $assertionValidator,
private SubjectConfirmationValidator $subjectConfirmationValidator,
private TransformerInterface $transformer,
private IdentityProvider $identityProviderConfiguration,
private LoggerInterface $logger,
) {
$this->assertionValidator = $assertionValidator;
$this->signatureValidator = $signatureValidator;
$this->decrypter = $decrypter;
$this->subjectConfirmationValidator = $subjectConfirmationValidator;
$this->transformer = $transformer;
$this->identityProviderConfiguration = $identityProviderConfiguration;
$this->logger = $logger;
}


Expand Down Expand Up @@ -132,14 +89,14 @@ public function process(Assertion $assertion): Assertion
if (!$assertion->wasSignedAtConstruction()) {
$this->logger->info(sprintf(
'Assertion with id "%s" was not signed at construction, not verifying the signature',
$assertion->getId()
$assertion->getId(),
));
} else {
$this->logger->info(sprintf('Verifying signature of Assertion with id "%s"', $assertion->getId()));

if (!$this->signatureValidator->hasValidSignature($assertion, $this->identityProviderConfiguration)) {
throw new InvalidSignatureException(
sprintf('The assertion with id "%s" does not have a valid signature', $assertion->getId())
sprintf('The assertion with id "%s" does not have a valid signature', $assertion->getId()),
);
}
}
Expand Down Expand Up @@ -171,18 +128,18 @@ public function validateAssertion(Assertion $assertion): void
if (!$assertionValidationResult->isValid()) {
throw new InvalidAssertionException(sprintf(
'Invalid Assertion in SAML Response, errors: "%s"',
implode('", "', $assertionValidationResult->getErrors())
implode('", "', $assertionValidationResult->getErrors()),
));
}

foreach ($assertion->getSubjectConfirmation() as $subjectConfirmation) {
foreach ($assertion->getSubject()->getSubjectConfirmation() as $subjectConfirmation) {
$subjectConfirmationValidationResult = $this->subjectConfirmationValidator->validate(
$subjectConfirmation
$subjectConfirmation,
);
if (!$subjectConfirmationValidationResult->isValid()) {
throw new InvalidSubjectConfirmationException(sprintf(
'Invalid SubjectConfirmation in Assertion, errors: "%s"',
implode('", "', $subjectConfirmationValidationResult->getErrors())
implode('", "', $subjectConfirmationValidationResult->getErrors()),
));
}
}
Expand Down
42 changes: 13 additions & 29 deletions src/SAML2/Assertion/ProcessorBuilder.php
Expand Up @@ -47,7 +47,7 @@ public static function build(
Destination $currentDestination,
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider,
Response $response
Response $response,
): Processor {
$keyloader = new PrivateKeyLoader();
$decrypter = new Decrypter($logger, $identityProvider, $serviceProvider, $keyloader);
Expand All @@ -56,14 +56,14 @@ public static function build(
$identityProvider,
$serviceProvider,
$currentDestination,
$response
$response,
);

$transformerChain = self::createAssertionTransformerChain(
$logger,
$keyloader,
$identityProvider,
$serviceProvider
$serviceProvider,
);

return new Processor(
Expand All @@ -73,7 +73,7 @@ public static function build(
$subjectConfirmationValidator,
$transformerChain,
$identityProvider,
$logger
$logger,
);
}

Expand All @@ -85,7 +85,7 @@ public static function build(
*/
private static function createAssertionValidator(
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider
ServiceProvider $serviceProvider,
): AssertionValidator {
$validator = new AssertionValidator($identityProvider, $serviceProvider);
$validator->addConstraintValidator(new NotBefore());
Expand All @@ -108,28 +108,14 @@ private static function createSubjectConfirmationValidator(
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider,
Destination $currentDestination,
Response $response
Response $response,
): SubjectConfirmationValidator {
$validator = new SubjectConfirmationValidator($identityProvider, $serviceProvider);
$validator->addConstraintValidator(
new SubjectConfirmationMethod()
);
$validator->addConstraintValidator(
new SubjectConfirmationNotBefore()
);
$validator->addConstraintValidator(
new SubjectConfirmationNotOnOrAfter()
);
$validator->addConstraintValidator(
new SubjectConfirmationRecipientMatches(
$currentDestination
)
);
$validator->addConstraintValidator(
new SubjectConfirmationResponseToMatches(
$response
)
);
$validator->addConstraintValidator(new SubjectConfirmationMethod());
$validator->addConstraintValidator(new SubjectConfirmationNotBefore());
$validator->addConstraintValidator(new SubjectConfirmationNotOnOrAfter());
$validator->addConstraintValidator(new SubjectConfirmationRecipientMatches($currentDestination));
$validator->addConstraintValidator(new SubjectConfirmationResponseToMatches($response));

return $validator;
}
Expand All @@ -145,12 +131,10 @@ private static function createAssertionTransformerChain(
LoggerInterface $logger,
PrivateKeyLoader $keyloader,
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider
ServiceProvider $serviceProvider,
): TransformerChain {
$chain = new TransformerChain($identityProvider, $serviceProvider);
$chain->addTransformerStep(
new NameIdDecryptionTransformer($logger, $keyloader)
);
$chain->addTransformerStep(new NameIdDecryptionTransformer($logger, $keyloader));

return $chain;
}
Expand Down
20 changes: 5 additions & 15 deletions src/SAML2/Assertion/Transformer/NameIdDecryptionTransformer.php
Expand Up @@ -26,11 +26,6 @@ final class NameIdDecryptionTransformer implements
IdentityProviderAware,
ServiceProviderAware
{
/**
* @var \SimpleSAML\SAML2\Certificate\PrivateKeyLoader
*/
private PrivateKeyLoader $privateKeyLoader;

/**
* @var \SimpleSAML\SAML2\Configuration\IdentityProvider
*/
Expand All @@ -41,11 +36,6 @@ final class NameIdDecryptionTransformer implements
*/
private ServiceProvider $serviceProvider;

/**
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;


/**
* Constructor for NameIdDecryptionTransformer
Expand All @@ -54,8 +44,8 @@ final class NameIdDecryptionTransformer implements
* @param \SimpleSAML\SAML2\Certificate\PrivateKeyLoader $privateKeyLoader
*/
public function __construct(
LoggerInterface $logger,
PrivateKeyLoader $privateKeyLoader
private LoggerInterface $logger,
private PrivateKeyLoader $privateKeyLoader,
) {
$this->logger = $logger;
$this->privateKeyLoader = $privateKeyLoader;
Expand Down Expand Up @@ -97,14 +87,14 @@ public function transform(Assertion $assertion): Assertion
'Decrypting assertion NameId with key "#%d" failed, "%s" thrown: "%s"',
$index,
get_class($e),
$e->getMessage()
$e->getMessage(),
));
}
}

if ($decrypted === null) {
throw new NotDecryptedException(
'Could not decrypt the assertion NameId with the configured keys, see the debug log for information'
'Could not decrypt the assertion NameId with the configured keys, see the debug log for information',
);
}

Expand All @@ -114,7 +104,7 @@ public function transform(Assertion $assertion): Assertion
$assertion->getIssueInstant(),
new Subject($decrypted, $subject->getSubjectConfirmation()),
$assertion->getConditions(),
$assertion->getStatements()
$assertion->getStatements(),
);
}

Expand Down
12 changes: 2 additions & 10 deletions src/SAML2/Assertion/Transformer/TransformerChain.php
Expand Up @@ -15,12 +15,6 @@ class TransformerChain implements TransformerInterface
/** @var \SimpleSAML\SAML2\Assertion\Transformer\TransformerInterface[] */
private $transformers = [];

/** @var \SimpleSAML\SAML2\Configuration\IdentityProvider */
private $identityProvider;

/** @var \SimpleSAML\SAML2\Configuration\ServiceProvider */
private $serviceProvider;


/**
* Constructor for TransformerChain
Expand All @@ -29,11 +23,9 @@ class TransformerChain implements TransformerInterface
* @param \SimpleSAML\SAML2\Configuration\ServiceProvider $serviceProvider
*/
public function __construct(
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider
private IdentityProvider $identityProvider,
private ServiceProvider $serviceProvider,
) {
$this->identityProvider = $identityProvider;
$this->serviceProvider = $serviceProvider;
}


Expand Down
17 changes: 3 additions & 14 deletions src/SAML2/Assertion/Validation/AssertionValidator.php
Expand Up @@ -17,27 +17,15 @@ class AssertionValidator
*/
protected array $constraints;

/**
* @var \SimpleSAML\SAML2\Configuration\IdentityProvider
*/
private IdentityProvider $identityProvider;

/**
* @var \SimpleSAML\SAML2\Configuration\ServiceProvider
*/
private ServiceProvider $serviceProvider;


/**
* @param \SimpleSAML\SAML2\Configuration\IdentityProvider $identityProvider
* @param \SimpleSAML\SAML2\Configuration\ServiceProvider $serviceProvider
*/
public function __construct(
IdentityProvider $identityProvider,
ServiceProvider $serviceProvider
private IdentityProvider $identityProvider,
private ServiceProvider $serviceProvider,
) {
$this->identityProvider = $identityProvider;
$this->serviceProvider = $serviceProvider;
}


Expand Down Expand Up @@ -65,6 +53,7 @@ public function addConstraintValidator(AssertionConstraintValidator $constraint)
public function validate(Assertion $assertion): Result
{
$result = new Result();

foreach ($this->constraints as $validator) {
$validator->validate($assertion, $result);
}
Expand Down

0 comments on commit 4a8d983

Please sign in to comment.