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

JWSRefresher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A decode() 0 21 4
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