JWSRefresher::decode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 12
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 19
rs 9.8666
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('Invalid refresh token', 'An error occurred while trying 
53
                    to decode the JWT token. Please verify your configuration (private key/passPhrase)', $e);
54
                }
55
            }
56
        } else {
57
            throw new ApiException('refresh token not provided', 'An error occurred while trying 
58
               to extract the refresh token. Please check your request header');
59
        }
60
61
        return [];
62
    }
63
}
64