Passed
Pull Request — master (#7018)
by
unknown
10:03
created

JWTClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A makeToken() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Chamilo\PluginBundle\Zoom\API;
5
6
use Firebase\JWT\JWT;
7
8
final class JWTClient
9
{
10
    /** @var string */
11
    private string $apiKey;
12
    /** @var string */
13
    private string $apiSecret;
14
15
    /**
16
     * Do NOT sign in the constructor. Just store normalized strings.
17
     */
18
    public function __construct(string $apiKey, string $apiSecret)
19
    {
20
        // Normalize to strings; trim to avoid accidental spaces.
21
        $this->apiKey    = trim((string) $apiKey);
22
        $this->apiSecret = trim((string) $apiSecret);
23
    }
24
25
    /**
26
     * Build a short-lived JWT token. HMAC key MUST be a string.
27
     * Throws InvalidArgumentException when config is missing.
28
     */
29
    public function makeToken(): string
30
    {
31
        if ($this->apiKey === '' || $this->apiSecret === '') {
32
            throw new \InvalidArgumentException('Zoom JWT: missing API Key/Secret');
33
        }
34
35
        $payload = [
36
            'iss' => $this->apiKey,
37
            'exp' => time() + 55, // short-lived token
38
        ];
39
40
        return JWT::encode($payload, $this->apiSecret, 'HS256');
41
    }
42
}
43