-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRequestSigner.php
More file actions
44 lines (37 loc) · 1.06 KB
/
RequestSigner.php
File metadata and controls
44 lines (37 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace Yoti\Http;
use Yoti\Http\Exception\RequestSignerException;
use Yoti\Util\PemFile;
class RequestSigner
{
/**
* Return request signed data.
*
* @param \Yoti\Util\PemFile $pemFile
* @param string $endpoint
* @param string $httpMethod
* @param \Yoti\Http\Payload|null $payload
*
* @return string
* The base64 encoded signed message
*
* @throws \Yoti\Http\Exception\RequestSignerException
*/
public static function sign(
PemFile $pemFile,
string $endpoint,
string $httpMethod,
?Payload $payload = null
): string {
$messageToSign = "{$httpMethod}&$endpoint";
if ($payload instanceof Payload) {
$messageToSign .= "&{$payload->toBase64()}";
}
openssl_sign($messageToSign, $signedMessage, (string) $pemFile, OPENSSL_ALGO_SHA256);
if (!$signedMessage) {
throw new RequestSignerException('Could not sign request.');
}
return base64_encode($signedMessage);
}
}