Completed
Push — master ( a70ca1...835655 )
by Jafar
06:00
created

JWSRefresher::decode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 19
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
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSRefresher;
11
12
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSEncoder\JWSEncoderInterface;
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSExtractor\TokenExtractor;
14
use Symfony\Component\HttpFoundation\Request;
15
use Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException;
16
17
/**
18
 * Class JWSRefresher
19
 *
20
 * @author Jafar Jabr <[email protected]>
21
 */
22
class JWSRefresher implements JWSRefresherInterface
23
{
24
25
    /**
26
     * @var JWSEncoderInterface
27
     */
28
    private $encoder;
29
30
    /**
31
     * JWSRefresher constructor.
32
     *
33
     * @param JWSEncoderInterface $JWSEncoder
34
     */
35
    public function __construct(JWSEncoderInterface $JWSEncoder)
36
    {
37
        $this->encoder = $JWSEncoder;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     */
44
    public function decode(Request $request)
45
    {
46
        if ($request->headers->has('refresh-token')) {
47
            $extractor    = new TokenExtractor('', 'refresh-token');
48
            $token        = $extractor->extract($request);
49
            try {
50
                return  $this->encoder->decode($token);
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type false; however, parameter $token of Jafar\Bundle\GuardedAuth...oderInterface::decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
                return  $this->encoder->decode(/** @scrutinizer ignore-type */ $token);
Loading history...
51
            } catch (ApiException $e) {
52
                throw new ApiException(
53
                    'Invalid refresh token',
54
                    'An error occurred while trying 
55
                to decode the JWT token. Please verify your configuration (private key/passPhrase)',
56
                    $e
57
                );
58
            }
59
        } else {
60
            throw new ApiException(
61
                'refresh token not provided',
62
                'An error occurred while trying 
63
                to extract the refresh token. Please check your request header'
64
            );
65
        }
66
    }
67
}
68