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 Xmf\Key\KeyAbstract;
15: use Xmf\Key\StorageInterface;
16:
17: /**
18: * Build a token
19: *
20: * @category Xmf\Jwt\TokenFactory
21: * @package Xmf
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2016-2018 XOOPS Project (https://xoops.org)
24: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25: * @link https://xoops.org
26: */
27: class TokenFactory
28: {
29: /**
30: * Create a JSON Web Token string
31: *
32: * @param KeyAbstract|string $key the key to use to sign the token, or name of key to build
33: * @param array|\Traversable $payload traversable set of claims, claim => value
34: * @param int $expirationOffset seconds from now that token will expire. If not specified,
35: * an "exp" claim will not be added or updated
36: *
37: * @return string a token string returned from JsonWebToken::create()
38: *
39: * @throws \DomainException
40: * @throws \InvalidArgumentException
41: * @throws \UnexpectedValueException
42: */
43: public static function build($key, $payload, $expirationOffset = 0)
44: {
45: $key = ($key instanceof KeyAbstract) ? $key : KeyFactory::build($key);
46: $token = new JsonWebToken($key);
47: return $token->create($payload, $expirationOffset);
48: }
49: }
50: