DefaultContextFactory::setRandomGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the tmilos/jose-jwt package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\JoseJwt\Context;
13
14
use Tmilos\JoseJwt\Jwe\AesCbcHmacEncryption;
15
use Tmilos\JoseJwt\Jwe\AesKeyWrapAlgorithm;
16
use Tmilos\JoseJwt\Jwe\DirectAlgorithm;
17
use Tmilos\JoseJwt\Jwe\JweAlgorithm;
18
use Tmilos\JoseJwt\Jwe\JweEncryption;
19
use Tmilos\JoseJwt\Jwe\RsaAlgorithm;
20
use Tmilos\JoseJwt\Jws\HmacUsingSha;
21
use Tmilos\JoseJwt\Jws\JwsAlgorithm;
22
use Tmilos\JoseJwt\Jws\PlainText;
23
use Tmilos\JoseJwt\Jws\RsaUsingSha;
24
use Tmilos\JoseJwt\Random\OpenSslRandomGenerator;
25
use Tmilos\JoseJwt\Random\RandomGenerator;
26
27
class DefaultContextFactory implements ContextFactory
28
{
29
    /** @var RandomGenerator */
30
    private $randomGenerator;
31
32
    /**
33
     * @param RandomGenerator $randomGenerator
34
     */
35
    public function __construct(RandomGenerator $randomGenerator = null)
36
    {
37
        $this->randomGenerator = $randomGenerator;
38
    }
39
40
    /**
41
     * @param RandomGenerator $randomGenerator
42
     *
43
     * @return DefaultContextFactory
44
     */
45
    public function setRandomGenerator(RandomGenerator $randomGenerator = null)
46
    {
47
        $this->randomGenerator = $randomGenerator;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return Context
54
     */
55
    public function get()
56
    {
57
        $randomGenerator = $this->randomGenerator ?: new OpenSslRandomGenerator();
58
59
        $context = new DefaultContext($randomGenerator);
0 ignored issues
show
Unused Code introduced by
The call to DefaultContext::__construct() has too many arguments starting with $randomGenerator.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
61
        $context->jwsAlgorithms()
62
            ->add(JwsAlgorithm::NONE, new PlainText())
63
            ->add(JwsAlgorithm::HS256, new HmacUsingSha('sha256'))
64
            ->add(JwsAlgorithm::HS384, new HmacUsingSha('sha384'))
65
            ->add(JwsAlgorithm::HS512, new HmacUsingSha('sha512'))
66
            ->add(JwsAlgorithm::RS256, new RsaUsingSha('sha256'))
67
            ->add(JwsAlgorithm::RS384, new RsaUsingSha('sha384'))
68
            ->add(JwsAlgorithm::RS512, new RsaUsingSha('sha512'))
69
        ;
70
71
        $context->jweAlgorithms()
72
            ->add(JweAlgorithm::RSA1_5, new RsaAlgorithm(OPENSSL_PKCS1_PADDING, $randomGenerator))
73
            ->add(JweAlgorithm::RSA_OAEP, new RsaAlgorithm(OPENSSL_PKCS1_OAEP_PADDING, $randomGenerator))
74
            ->add(JweAlgorithm::A128KW, new AesKeyWrapAlgorithm(128, $randomGenerator))
75
            ->add(JweAlgorithm::A192KW, new AesKeyWrapAlgorithm(192, $randomGenerator))
76
            ->add(JweAlgorithm::A256KW, new AesKeyWrapAlgorithm(256, $randomGenerator))
77
            ->add(JweAlgorithm::DIR, new DirectAlgorithm())
78
        ;
79
80
        $context->jweEncryptions()
81
            ->add(JweEncryption::A128CBC_HS256, new AesCbcHmacEncryption(256, new HmacUsingSha('sha256'), $randomGenerator))
82
            ->add(JweEncryption::A192CBC_HS384, new AesCbcHmacEncryption(384, new HmacUsingSha('sha384'), $randomGenerator))
83
            ->add(JweEncryption::A256CBC_HS512, new AesCbcHmacEncryption(512, new HmacUsingSha('sha512'), $randomGenerator))
84
        ;
85
86
        return $context;
87
    }
88
}
89