RequestSigner   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 4
eloc 40
dl 0
loc 101
ccs 0
cts 23
cp 0
rs 10
c 6
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A JWKString() 0 4 1
A KID() 0 29 2
A JWK() 0 31 1
1
<?php
2
3
namespace LE_ACME2\Utilities;
4
5
class RequestSigner {
6
7
    /**
8
     * Generates a JSON Web Key signature to attach to the request.
9
     *
10
     * @param array 	$payload		The payload to add to the signature.
11
     * @param string	$url 			The URL to use in the signature.
12
     * @param string 	$privateKeyDir  The directory to get the private key from. Default to the account keys directory given in the constructor. (optional)
13
     * @param string 	$privateKeyFile The private key to sign the request with. Defaults to 'private.pem'. (optional)
14
     *
15
     * @return array	Returns an array containing the signature.
16
     */
17
    public static function JWK(array $payload, string $url, string $nonce, string $privateKeyDir, string $privateKeyFile = 'private.pem') : array {
18
19
        Logger::getInstance()->add(Logger::LEVEL_DEBUG, 'JWK sign request for ' . $url, ['payload' => $payload]);
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
        Logger::getInstance()->/** @scrutinizer ignore-call */ add(Logger::LEVEL_DEBUG, 'JWK sign request for ' . $url, ['payload' => $payload]);
Loading history...
20
21
        $privateKey = openssl_pkey_get_private(file_get_contents($privateKeyDir . $privateKeyFile));
22
        $details = openssl_pkey_get_details($privateKey);
23
24
        $protected = [
25
            "alg" => "RS256",
26
            "jwk" => [
27
                "kty" => "RSA",
28
                "n" => Base64::UrlSafeEncode($details["rsa"]["n"]),
29
                "e" => Base64::UrlSafeEncode($details["rsa"]["e"]),
30
            ],
31
            "nonce" => $nonce,
32
            "url" => $url
33
        ];
34
35
        $payload64 = Base64::JSONUrlSafeEncode($payload);
36
        $protected64 = Base64::JSONUrlSafeEncode($protected);
37
38
        openssl_sign($protected64.'.'.$payload64, $signed, $privateKey, "SHA256");
39
        $signed64 = Base64::UrlSafeEncode($signed);
40
41
        $data = array(
42
            'protected' => $protected64,
43
            'payload' => $payload64,
44
            'signature' => $signed64
45
        );
46
47
        return $data;
48
    }
49
50
    /**
51
     * Generates a JSON Web Key signature to attach to the request.
52
     *
53
     * @param array 	$payload		The payload to add to the signature.
54
     * @param string	$url 			The URL to use in the signature.
55
     * @param string 	$privateKeyDir  The directory to get the private key from. Default to the account keys directory given in the constructor. (optional)
56
     * @param string 	$privateKeyFile The private key to sign the request with. Defaults to 'private.pem'. (optional)
57
     *
58
     * @return string	Returns a JSON encoded string containing the signature.
59
     */
60
    public static function JWKString(array $payload, string $url, string $nonce, string $privateKeyDir, string $privateKeyFile = 'private.pem') : string {
61
62
        $jwk = self::JWK($payload, $url, $nonce, $privateKeyDir, $privateKeyFile);
63
        return json_encode($jwk);
64
    }
65
66
    /**
67
     * Generates a Key ID signature to attach to the request.
68
     *
69
     * @param array|null 	$payload		The payload to add to the signature.
70
     * @param string	$kid			The Key ID to use in the signature.
71
     * @param string	$url 			The URL to use in the signature.
72
     * @param string 	$privateKeyDir  The directory to get the private key from.
73
     * @param string 	$privateKeyFile The private key to sign the request with. Defaults to 'private.pem'. (optional)
74
     *
75
     * @return string	Returns a JSON encoded string containing the signature.
76
     */
77
    public static function KID(?array $payload, string $kid, string $url, string $nonce, string $privateKeyDir, string $privateKeyFile = 'private.pem') : string {
78
79
        Logger::getInstance()->add(Logger::LEVEL_DEBUG, 'KID sign request for ' . $url, ['payload' => $payload]);
80
81
        $privateKey = openssl_pkey_get_private(file_get_contents($privateKeyDir . $privateKeyFile));
82
        // TODO: unused - $details = openssl_pkey_get_details($privateKey);
83
84
        $protected = [
85
            "alg" => "RS256",
86
            "kid" => $kid,
87
            "nonce" => $nonce,
88
            "url" => $url
89
        ];
90
91
        Logger::getInstance()->add(Logger::LEVEL_DEBUG, 'KID: ready to sign request for: ' . $url, ['protected' => $protected]);
92
93
        $payload64 = $payload === null ? Base64::UrlSafeEncode('') : Base64::JSONUrlSafeEncode($payload);
0 ignored issues
show
introduced by
The condition $payload === null is always false.
Loading history...
94
        $protected64 = Base64::JSONUrlSafeEncode($protected);
95
96
        openssl_sign($protected64.'.'.$payload64, $signed, $privateKey, "SHA256");
97
        $signed64 = Base64::UrlSafeEncode($signed);
98
99
        $data = [
100
            'protected' => $protected64,
101
            'payload' => $payload64,
102
            'signature' => $signed64
103
        ];
104
105
        return json_encode($data);
106
    }
107
}