|
| 1 | +<?php |
| 2 | +namespace Firebase\JWT\Keys; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | + |
| 6 | +class JWTKeyTest extends TestCase |
| 7 | +{ |
| 8 | + public function testExpectedAlgGuess() |
| 9 | + { |
| 10 | + $eddsa = 'MpwwPe63YoDwAoO7EDBUOUb7J9lpdjt8vT+hfnLL39k='; |
| 11 | + $ecc384 = $this->getEcdsaPublicKey(); |
| 12 | + $rsa = $this->getRsaPublicKey(); |
| 13 | + $misc = 'maybe_use_paseto_instead'; |
| 14 | + |
| 15 | + $this->assertSame( |
| 16 | + 'RS512', |
| 17 | + JWTKey::guessAlgFromKeyMaterial($rsa, array('RS512')) |
| 18 | + ); |
| 19 | + $this->assertSame( |
| 20 | + 'RS384', |
| 21 | + JWTKey::guessAlgFromKeyMaterial($rsa, array('RS384')) |
| 22 | + ); |
| 23 | + $this->assertSame( |
| 24 | + 'RS256', |
| 25 | + JWTKey::guessAlgFromKeyMaterial($rsa, array('RS256')) |
| 26 | + ); |
| 27 | + $this->assertSame( |
| 28 | + 'RS256', |
| 29 | + JWTKey::guessAlgFromKeyMaterial($rsa) |
| 30 | + ); |
| 31 | + $this->assertSame( |
| 32 | + 'ES384', |
| 33 | + JWTKey::guessAlgFromKeyMaterial($ecc384, array('ES384')) |
| 34 | + ); |
| 35 | + $this->assertSame( |
| 36 | + 'ES256', |
| 37 | + JWTKey::guessAlgFromKeyMaterial($ecc384) |
| 38 | + ); |
| 39 | + $this->assertSame( |
| 40 | + 'EdDSA', |
| 41 | + JWTKey::guessAlgFromKeyMaterial($eddsa, array('EdDSA')) |
| 42 | + ); |
| 43 | + $this->assertSame( |
| 44 | + 'HS256', |
| 45 | + JWTKey::guessAlgFromKeyMaterial($eddsa) |
| 46 | + ); |
| 47 | + $this->assertSame( |
| 48 | + 'HS384', |
| 49 | + JWTKey::guessAlgFromKeyMaterial($misc, array('HS512')) |
| 50 | + ); |
| 51 | + $this->assertSame( |
| 52 | + 'HS384', |
| 53 | + JWTKey::guessAlgFromKeyMaterial($misc, array('HS384')) |
| 54 | + ); |
| 55 | + $this->assertSame( |
| 56 | + 'HS256', |
| 57 | + JWTKey::guessAlgFromKeyMaterial($misc, array('HS256')) |
| 58 | + ); |
| 59 | + $this->assertSame( |
| 60 | + 'HS256', |
| 61 | + JWTKey::guessAlgFromKeyMaterial($misc) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function getRsaPublicKey() |
| 66 | + { |
| 67 | + $privKey = openssl_pkey_new(array('digest_alg' => 'sha256', |
| 68 | + 'private_key_bits' => 1024, |
| 69 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA)); |
| 70 | + $pubKey = openssl_pkey_get_details($privKey); |
| 71 | + return $pubKey['key']; |
| 72 | + } |
| 73 | + |
| 74 | + public function getEcdsaPublicKey() |
| 75 | + { |
| 76 | + $privKey = openssl_pkey_new( |
| 77 | + array( |
| 78 | + 'curve_name' => 'secp384r1', |
| 79 | + 'digest_alg' => 'sha384', |
| 80 | + 'private_key_bits' => 384, |
| 81 | + 'private_key_type' => OPENSSL_KEYTYPE_EC |
| 82 | + ) |
| 83 | + ); |
| 84 | + $pubKey = openssl_pkey_get_details($privKey); |
| 85 | + return $pubKey['key']; |
| 86 | + } |
| 87 | +} |
0 commit comments