Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

KeyId   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 22
c 2
b 1
f 0
dl 0
loc 33
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 31 2
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
class KeyId
6
{
7
    public static function generate(array $payload = null, string $kid, string $url, string $nonce, string $accountKeysPath): array
8
    {
9
        $privateKey = openssl_pkey_get_private(file_get_contents($accountKeysPath . 'private.pem'));
10
11
        $data = [
12
            'alg'   => 'RS256',
13
            'kid'   => $kid,
14
            'nonce' => $nonce,
15
            'url'   => $url,
16
        ];
17
18
        $payload = is_array($payload)
19
            ? str_replace('\\/', '/', json_encode($payload))
20
            : '';
21
22
        $payload64 = Base64::urlSafeEncode($payload);
23
        $protected64 = Base64::urlSafeEncode(json_encode($data));
24
25
        openssl_sign(
26
            $protected64 . '.' . $payload64,
27
            $signed,
28
            $privateKey,
29
            'SHA256'
0 ignored issues
show
Bug introduced by
'SHA256' of type string is incompatible with the type integer expected by parameter $signature_alg of openssl_sign(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
            /** @scrutinizer ignore-type */ 'SHA256'
Loading history...
30
        );
31
32
        $signed64 = Base64::urlSafeEncode($signed);
33
34
        return [
35
            'protected' => $protected64,
36
            'payload'   => $payload64,
37
            'signature' => $signed64,
38
        ];
39
    }
40
}
41