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

JWSRefresher::decode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 1
dl 0
loc 21
rs 9.0534
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\JWSRefresher;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSEncoder\JWSEncoderInterface;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSExtractor\TokenExtractor;
15
use Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Class JWSRefresher.
20
 *
21
 * @author Jafar Jabr <[email protected]>
22
 */
23
class JWSRefresher implements JWSRefresherInterface
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
    public function decode(Request $request)
44
    {
45
        if ($request->headers->has('refresh-token')) {
46
            $extractor    = new TokenExtractor('', 'refresh-token');
47
            $token        = $extractor->extract($request);
48
            if ($token) {
49
                try {
50
                    return  $this->encoder->decode($token);
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
            }
60
        } else {
61
            throw new ApiException(
62
                'refresh token not provided',
63
                'An error occurred while trying 
64
                to extract the refresh token. Please check your request header'
65
            );
66
        }
67
    }
68
}
69