JsonWebSignature   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 15
c 1
b 0
f 0
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 26 1
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
class JsonWebSignature
6
{
7
    public static function generate(
8
        array $payload,
9
        string $url,
10
        string $nonce,
11
        #[\SensitiveParameter] string $accountPrivateKey
12
    ): array {
13
        $privateKey = openssl_pkey_get_private($accountPrivateKey);
14
15
        $protected = [
16
            'alg' => 'RS256',
17
            'jwk' => JsonWebKey::compute($accountPrivateKey),
18
            'nonce' => $nonce,
19
            'url' => $url,
20
        ];
21
22
        $payload64 = Base64::urlSafeEncode(str_replace('\\/', '/', json_encode($payload, JSON_THROW_ON_ERROR)));
23
        $protected64 = Base64::urlSafeEncode(json_encode($protected, JSON_THROW_ON_ERROR));
24
25
        openssl_sign($protected64.'.'.$payload64, $signed, $privateKey, 'SHA256');
26
27
        $signed64 = Base64::urlSafeEncode($signed);
28
29
        return [
30
            'protected' => $protected64,
31
            'payload' => $payload64,
32
            'signature' => $signed64,
33
        ];
34
    }
35
}
36