Failed Conditions
Push — master ( 8e9cbc...6d4032 )
by Florent
03:41
created

IdTokenAlgorithmsRule::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\Model\Client\Rule;
15
16
use Assert\Assertion;
17
use Jose\EncrypterInterface;
18
use Jose\SignerInterface;
19
use OAuth2Framework\Component\Server\Model\DataBag\DataBag;
20
use OAuth2Framework\Component\Server\Model\UserAccount\UserAccountId;
21
22
final class IdTokenAlgorithmsRule implements RuleInterface
23
{
24
    /**
25
     * @var SignerInterface
26
     */
27
    private $signer;
28
29
    /**
30
     * @var EncrypterInterface|null
31
     */
32
    private $encrypter;
33
34
    /**
35
     * IdTokenAlgorithmsRule constructor.
36
     *
37
     * @param SignerInterface         $signer
38
     * @param EncrypterInterface|null $encrypter
39
     */
40
    public function __construct(SignerInterface $signer, ?EncrypterInterface $encrypter)
41
    {
42
        $this->signer = $signer;
43
        $this->encrypter = $encrypter;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function handle(DataBag $commandParameters, DataBag $validatedParameters, ? UserAccountId $userAccountId, callable $next): DataBag
50
    {
51
        if ($commandParameters->has('id_token_signed_response_alg')) {
52
            Assertion::string($commandParameters['id_token_signed_response_alg'], 'Invalid parameter \'id_token_signed_response_alg\'. The value must be a string.');
53
            Assertion::inArray($commandParameters['id_token_signed_response_alg'], $this->signer->getSupportedSignatureAlgorithms(), sprintf('The ID Token signature response algorithm \'%s\' is not supported. Please choose one of the following algorithm: %s', $commandParameters->get('id_token_signed_response_alg'), implode(', ', $this->signer->getSupportedSignatureAlgorithms())));
54
            $validatedParameters = $validatedParameters->with('id_token_signed_response_alg', $commandParameters['id_token_signed_response_alg']);
55
        }
56
57
        if ($commandParameters->has('id_token_encrypted_response_alg') && $commandParameters->has('id_token_encrypted_response_enc') && null !== $this->encrypter) {
58
            Assertion::string($commandParameters['id_token_encrypted_response_alg'], 'Invalid parameter \'id_token_encrypted_response_alg\'. The value must be a string.');
59
            Assertion::string($commandParameters['id_token_encrypted_response_enc'], 'Invalid parameter \'id_token_encrypted_response_enc\'. The value must be a string.');
60
            Assertion::inArray($commandParameters['id_token_encrypted_response_alg'], $this->encrypter->getSupportedKeyEncryptionAlgorithms(), sprintf('The ID Token content encryption algorithm \'%s\' is not supported. Please choose one of the following algorithm: %s', $commandParameters->get('id_token_encrypted_response_alg'), implode(', ', $this->encrypter->getSupportedContentEncryptionAlgorithms())));
61
            Assertion::inArray($commandParameters['id_token_encrypted_response_enc'], $this->encrypter->getSupportedContentEncryptionAlgorithms(), sprintf('The ID Token key encryption algorithm \'%s\' is not supported. Please choose one of the following algorithm: %s', $commandParameters->get('id_token_encrypted_response_enc'), implode(', ', $this->encrypter->getSupportedKeyEncryptionAlgorithms())));
62
            $validatedParameters = $validatedParameters->with('id_token_encrypted_response_alg', $commandParameters['id_token_encrypted_response_alg']);
63
            $validatedParameters = $validatedParameters->with('id_token_encrypted_response_enc', $commandParameters['id_token_encrypted_response_enc']);
64
        }
65
66
        return $next($commandParameters, $validatedParameters, $userAccountId);
67
    }
68
}
69