Crypto - Define a service for creating and verifying JSON Web tokens ('crypto.jwt')
[civicrm-core.git] / tests / phpunit / Civi / Crypto / CryptoTestTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\Crypto;
13
14 trait CryptoTestTrait {
15
16 public static function getExampleKeys() {
17 return [
18 ':b64:cGxlYXNlIHVzZSAzMiBieXRlcyBmb3IgYWVzLTI1NiE',
19 'aes-cbc:hkdf-sha256:abcd1234abcd1234',
20 'aes-ctr::abcd1234abcd1234',
21 'aes-cbc-hs::abcd1234abcd1234',
22 'jwt-hs256::abcd1234abcd1234',
23 'jwt-hs384:b64:8h5wNGnJbdVHpXms2RwcVx+jxCNdYEsYCdNlPpVgNLRMg9Q2xKYnxSfuihS6YCRi',
24 'jwt-hs256::fdsafdsafdsa',
25 ];
26 }
27
28 /**
29 * @param CryptoRegistry $registry
30 * @see \CRM_Utils_Hook::crypto()
31 */
32 public function registerExampleKeys($registry) {
33 $origCount = count($registry->getKeys());
34
35 $examples = self::getExampleKeys();
36 $key = $registry->addSymmetricKey($registry->parseKey($examples[0]) + [
37 'tags' => ['UNIT-TEST'],
38 'weight' => 10,
39 'id' => 'asdf-key-0',
40 ]);
41 $this->assertEquals(10, $key['weight']);
42
43 $key = $registry->addSymmetricKey($registry->parseKey($examples[1]) + [
44 'tags' => ['UNIT-TEST'],
45 'weight' => -10,
46 'id' => 'asdf-key-1',
47 ]);
48 $this->assertEquals(-10, $key['weight']);
49
50 $key = $registry->addSymmetricKey($registry->parseKey($examples[2]) + [
51 'tags' => ['UNIT-TEST'],
52 'id' => 'asdf-key-2',
53 ]);
54 $this->assertEquals(0, $key['weight']);
55
56 $key = $registry->addSymmetricKey($registry->parseKey($examples[3]) + [
57 'tags' => ['UNIT-TEST'],
58 'id' => 'asdf-key-3',
59 ]);
60 $this->assertEquals(0, $key['weight']);
61
62 $key = $registry->addSymmetricKey($registry->parseKey($examples[4]) + [
63 'tags' => ['SIGN-TEST'],
64 'id' => 'sign-key-1',
65 'weight' => 1,
66 ]);
67 $this->assertEquals(1, $key['weight']);
68
69 $key = $registry->addSymmetricKey($registry->parseKey($examples[4]) + [
70 'tags' => ['SIGN-TEST'],
71 'id' => 'sign-key-0',
72 ]);
73 $this->assertEquals(0, $key['weight']);
74
75 $key = $registry->addSymmetricKey($registry->parseKey($examples[4]) + [
76 'tags' => ['SIGN-TEST-ALT'],
77 'id' => 'sign-key-alt',
78 ]);
79 $this->assertEquals(0, $key['weight']);
80
81 $this->assertEquals(7, count($examples));
82 $this->assertEquals(7 + $origCount, count($registry->getKeys()));
83 }
84
85 }