JWSEncoder::decode()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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