Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
jwt.php
1<?php
2
4
7
8final class Jwt
9{
10 public const DEFAULT_TTL = 12 * 3600;
11
15 public static function create(array $channels = [], int $userId = 0, array $options = []): array
16 {
17 if (empty($channels) && $userId === 0)
18 {
19 throw new Main\ArgumentException("Either channel list or user id must be specified");
20 }
21
22 $ttl = is_integer($options['ttl']) && $options['ttl'] > 0 ? $options['ttl'] : self::DEFAULT_TTL;
23
24 $time = time();
25 $exp = $time + $ttl;
26 $data = [
27 'iss' => (string)Config::getHostId(),
28 'iat' => $time,
29 'exp' => $exp,
30 ];
31 if ($userId > 0)
32 {
33 $data['sub'] = (string)$userId;
34 }
35 if (!empty($channels))
36 {
37 $data['chan'] = implode(',', $channels);
38 }
39 $secret = Config::getSignatureKey();
40
41 return [Main\Web\JWT::encode($data, $secret), $exp];
42 }
43
44 public static function decode(string $jwt, string $secret)
45 {
46 return Main\Web\JWT::decode($jwt, $secret);
47 }
48}
const DEFAULT_TTL
Definition jwt.php:10
static create(array $channels=[], int $userId=0, array $options=[])
Definition jwt.php:15
static decode(string $jwt, string $secret)
Definition jwt.php:44