-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByCertificateTest.class.php
More file actions
72 lines (58 loc) · 2.39 KB
/
ByCertificateTest.class.php
File metadata and controls
72 lines (58 loc) · 2.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php namespace web\auth\unittest;
use lang\{IllegalArgumentException, IllegalStateException};
use test\verify\Runtime;
use test\{Assert, Before, Expect, Test, Values};
use web\auth\oauth\ByCertificate;
#[Runtime(extensions: ['openssl'])]
class ByCertificateTest {
use PrivateKey;
const CLIENT_ID = 'b2ba8814';
const FINGERPRINT = 'd41d8cd98f00b204e9800998ecf8427e';
const ENDPOINT = 'https://login.example.com/oauth/token';
private $privateKey;
#[Before]
public function key() {
$this->privateKey= $this->newPrivateKey();
}
#[Test]
public function can_create() {
new ByCertificate(self::CLIENT_ID, self::FINGERPRINT, $this->privateKey);
}
#[Test, Expect(IllegalArgumentException::class)]
public function invalid_private_key() {
new ByCertificate(self::CLIENT_ID, self::FINGERPRINT, 'not.a.private.key');
}
#[Test]
public function key_member() {
Assert::equals(self::CLIENT_ID, (new ByCertificate(self::CLIENT_ID, self::FINGERPRINT, $this->privateKey))->key);
}
#[Test]
public function client_id_and_assertion_type_in_params() {
$params= (new ByCertificate(self::CLIENT_ID, self::FINGERPRINT, $this->privateKey))->params(self::ENDPOINT);
Assert::equals(self::CLIENT_ID, $params['client_id']);
Assert::equals('urn:ietf:params:oauth:client-assertion-type:jwt-bearer', $params['client_assertion_type']);
}
#[Test, Values(['d41d8cd98f00b204', 'D41D8CD98F00B204', 'D4:1D:8C:D9:8F:00:B2:04'])]
public function jwt_headers_with($fingerprint) {
$params= (new ByCertificate(self::CLIENT_ID, $fingerprint, $this->privateKey))->params(self::ENDPOINT);
$headers= json_decode(base64_decode(explode('.', $params['client_assertion'])[0]), true);
Assert::equals(['alg' => 'RS256', 'typ' => 'JWT', 'x5t' => '1B2M2Y8AsgQ'], $headers);
}
#[Test, Values([3600, 86400])]
public function jwt_payload_with($validity) {
$time= time();
$params= (new ByCertificate(self::CLIENT_ID, self::FINGERPRINT, $this->privateKey, $validity))->params(self::ENDPOINT, ['time' => $time]);
$payload= json_decode(base64_decode(explode('.', $params['client_assertion'])[1]), true);
Assert::equals(
[
'aud' => self::ENDPOINT,
'exp' => $time + $validity,
'iss' => self::CLIENT_ID,
'jti' => $payload['jti'], // Random time-based UUID
'nbf' => $time,
'sub' => self::CLIENT_ID,
],
$payload
);
}
}