Passed
Push — master ( 5e92ea...2f45b9 )
by
unknown
17:08 queued 08:12
created

JWTClient::makeToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
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