Passed
Push — master ( 90db0f...396226 )
by Jafar
03:16
created

JWSProvider::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSProvider;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSCreator\JWSCreator;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\JWS;
15
use Jafar\Bundle\GuardedAuthenticationBundle\Api\KeyLoader\KeyLoaderInterface;
16
use Jafar\Bundle\GuardedAuthenticationBundle\Api\KeyLoader\LoadedJWS;
17
18
/**
19
 * Class JWSProvider.
20
 *
21
 * @author Jafar Jabr <[email protected]>
22
 */
23
class JWSProvider implements JWSProviderInterface
24
{
25
    const SIGNATUREALGORITHM = 'RS256';
26
27
    /**
28
     * @var KeyLoaderInterface
29
     */
30
    private $keyLoader;
31
32
    /**
33
     * @var int
34
     */
35
    private $ttl;
36
37
    /**
38
     * @var int
39
     */
40
    private $refresh_ttl;
41
42
    /**
43
     * @param KeyLoaderInterface $keyLoader
44
     * @param int                $ttl
45
     * @param int                $refresh_ttl
46
     *
47
     * @throws \InvalidArgumentException If the given ttl is not numeric
48
     */
49
    public function __construct(KeyLoaderInterface $keyLoader, $ttl, $refresh_ttl)
50
    {
51
        if (null === $ttl) {
0 ignored issues
show
introduced by
The condition null === $ttl is always false.
Loading history...
52
            throw new \InvalidArgumentException(sprintf('The TTL should be a numeric value, got %s instead.', $ttl));
53
        }
54
55
        if (null === $refresh_ttl) {
0 ignored issues
show
introduced by
The condition null === $refresh_ttl is always false.
Loading history...
56
            throw new \InvalidArgumentException(
57
                sprintf('The Refresh TTL should be a numeric value, got %s instead.', $refresh_ttl)
58
            );
59
        }
60
        $this->keyLoader   = $keyLoader;
61
        $this->ttl         = $ttl;
62
        $this->refresh_ttl = $refresh_ttl;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function create(array $payload, string $type = 'Main')
69
    {
70
        $jws    = new JWS(['alg' => self::SIGNATUREALGORITHM]);
71
        $claims = ['iat' => time()];
72
        if ('Main' == $type) {
73
            $claims['exp'] = time() + $this->ttl;
74
        } else {
75
            $claims['exp'] = time() + $this->refresh_ttl;
76
        }
77
        $jws->setPayload($payload + $claims);
78
        $jws->sign(
79
            $this->keyLoader->loadKey('private'),
80
            $this->keyLoader->getPassphrase()
0 ignored issues
show
Unused Code introduced by
The call to Jafar\Bundle\GuardedAuth...i\JWTSigner\JWS::sign() has too many arguments starting with $this->keyLoader->getPassphrase(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $jws->/** @scrutinizer ignore-call */ 
81
              sign(

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. Please note the @ignore annotation hint above.

Loading history...
81
        );
82
83
        return new JWSCreator($jws->getTokenString(), $jws->isSigned());
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function load($token)
90
    {
91
        $jws = JWS::load($token, false, null);
92
93
        return new LoadedJWS(
94
            $jws->getPayload(),
95
            $jws->verify($this->keyLoader->loadKey('public'), self::SIGNATUREALGORITHM),
96
            null !== $this->ttl
97
        );
98
    }
99
}
100