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

JWSEncoder::decode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 1
dl 0
loc 23
rs 8.5906
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\JWSEncoder;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSProvider\JWSProviderInterface;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException;
15
16
/**
17
 * Class JWSEncoder.
18
 *
19
 * @author Jafar Jabr <[email protected]>
20
 */
21
class JWSEncoder implements JWSEncoderInterface
22
{
23
    /**
24
     * @var JWSProviderInterface
25
     */
26
    protected $jwsProvider;
27
28
    /**
29
     * @param JWSProviderInterface $jwsProvider
30
     */
31
    public function __construct(JWSProviderInterface $jwsProvider)
32
    {
33
        $this->jwsProvider = $jwsProvider;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function encode(array $payload, string $type = 'Main')
40
    {
41
        try {
42
            $jws = $this->jwsProvider->create($payload, $type);
43
        } catch (\InvalidArgumentException $e) {
44
            throw new ApiException(
45
                ApiException::INVALID_CONFIG,
46
                'An error occurred while trying 
47
                to encode the JWT token. Please verify your configuration (private key/passPhrase)',
48
                $e
49
            );
50
        }
51
        if (!$jws->isSigned()) {
52
            throw new ApiException(
53
                ApiException::UNSIGNED_TOKEN,
54
                'Unable to create a signed JWT from the given configuration.'
55
            );
56
        }
57
58
        return $jws->getToken();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function decode($token)
65
    {
66
        try {
67
            $jws = $this->jwsProvider->load($token);
68
        } catch (\Exception $e) {
69
            throw new ApiException(ApiException::INVALID_TOKEN, 'Invalid JWT Token', $e);
70
        }
71
        if ($jws->isInvalid()) {
72
            throw new ApiException(ApiException::INVALID_TOKEN, 'Invalid JWT Token');
73
        }
74
        if ($jws->isExpired()) {
75
            throw new ApiException(ApiException::EXPIRED_TOKEN, 'Expired JWT Token');
76
        }
77
        if (!$jws->isVerified()) {
78
            throw new ApiException(
79
                ApiException::UNVERIFIED_TOKEN,
80
                'Unable to verify the given JWT through the given configuration.
81
                 If the encryption keys have been changed since your last authentication, please renew the token.
82
                 If the problem persists, verify that the configured passPhrase is valid.'
83
            );
84
        }
85
86
        return $jws->getPayload();
87
    }
88
}
89