Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

JsonWebToken   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 95
rs 10
ccs 26
cts 26
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setAlgorithm() 0 8 2
B decode() 0 18 5
A create() 0 8 2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Jwt;
13
14
use Firebase\JWT\JWT;
15
use Xmf\Key\KeyAbstract;
16
17
/**
18
 * Basic JSON Web Token support
19
 *
20
 * @category  Xmf\Jwt\JsonWebToken
21
 * @package   Xmf
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2016 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @version   Release: 1.0
26
 * @link      http://xoops.org
27
 */
28
class JsonWebToken
29
{
30
    /**
31
     * @var KeyAbstract
32
     */
33
    protected $key;
34
35
    /**
36
     * @var string
37
     */
38
    protected $algorithm = 'HS256';
39
40
    /**
41
     * @var array
42
     */
43
    protected $claims = array();
44
45
    /**
46
     * JsonWebToken constructor.
47
     *
48
     * @param KeyAbstract $key       key for signing/validating
49
     * @param string      $algorithm algorithm to use for signing/validating
50
     */
51 1
    public function __construct(KeyAbstract $key, $algorithm = 'HS256')
52
    {
53 1
        $this->key = $key;
54 1
        $this->setAlgorithm($algorithm);
55 1
    }
56
57
    /**
58
     * @param string $algorithm algorithm to use for signing/validating
59
     *
60
     * @return JsonWebToken
61
     *
62
     * @throws \DomainException
63
     */
64 1
    public function setAlgorithm($algorithm)
65
    {
66 1
        if (array_key_exists($algorithm, JWT::$supported_algs)) {
67 1
            $this->algorithm = $algorithm;
68 1
            return $this;
69
        }
70 1
        throw new \DomainException('Algorithm not supported');
71
    }
72
73
    /**
74
     * Decode a JWT string, validating signature and well defined registered claims,
75
     * and optionally validate against a list of supplied claims
76
     *
77
     * @param string             $jwtString    string containing the JWT to decode
78
     * @param array|\Traversable $assertClaims associative array, claim => value, of claims to assert
79
     *
80
     * @return object|false
81
     */
82 1
    public function decode($jwtString, $assertClaims = array())
83
    {
84 1
        $allowedAlgorithms = array($this->algorithm);
85
        try {
86 1
            $values = JWT::decode($jwtString, $this->key->getVerifying(), $allowedAlgorithms);
87 1
        } catch (\Exception $e) {
88 1
            trigger_error($e->getMessage(), E_USER_NOTICE);
89 1
            return false;
90
        }
91 1
        foreach ($assertClaims as $claim => $assert) {
92 1
            if (!property_exists($values, $claim)) {
93 1
                return false;
94 1
            } elseif ($values->$claim != $assert) {
95 1
                return false;
96
            }
97
        }
98 1
        return $values;
99
    }
100
101
    /**
102
     * Create a signed token string for a payload
103
     *
104
     * @param array|\ArrayObject $payload          traversable set of claims, claim => value
105
     * @param int                $expirationOffset seconds from now that token will expire. If not specified,
106
     *                                              an "exp" claim will not be added or updated
107
     *
108
     * @return string encoded and signed jwt string
109
     *
110
     * @throws \DomainException;
111
     * @throws \InvalidArgumentException;
112
     * @throws \UnexpectedValueException;
113
     */
114 1
    public function create($payload, $expirationOffset = 0)
115
    {
116 1
        if ((int) $expirationOffset > 0) {
117 1
            $payload['exp'] = time() + (int) $expirationOffset;
118
        }
119 1
        $value = JWT::encode($payload, $this->key->getSigning(), $this->algorithm);
120 1
        return $value;
121
    }
122
}
123