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

JsonWebSignature   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 27 1
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
class JsonWebSignature
6
{
7
    public static function generate(array $payload, string $url, string $nonce, string $accountKeysPath): array
8
    {
9
        $privateKey = openssl_pkey_get_private(file_get_contents($accountKeysPath . 'private.pem'));
10
        $details = openssl_pkey_get_details($privateKey);
0 ignored issues
show
Bug introduced by
It seems like $privateKey can also be of type false; however, parameter $key of openssl_pkey_get_details() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

10
        $details = openssl_pkey_get_details(/** @scrutinizer ignore-type */ $privateKey);
Loading history...
11
12
        $protected = [
13
            'alg' => 'RS256',
14
            'jwk' => [
15
                'kty' => 'RSA',
16
                'n'   => Base64::UrlSafeEncode($details['rsa']['n']),
17
                'e'   => Base64::UrlSafeEncode($details['rsa']['e']),
18
            ],
19
            'nonce' => $nonce,
20
            'url'   => $url,
21
        ];
22
23
        $payload64 = Base64::urlSafeEncode(str_replace('\\/', '/', json_encode($payload)));
24
        $protected64 = Base64::urlSafeEncode(json_encode($protected));
25
26
        openssl_sign($protected64 . '.' . $payload64, $signed, $privateKey, '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

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