| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Dudlewebs\WPMCS\Firebase\JWT; | |
| 3 | +namespace Dudlewebs\WPMCS\GCP\Firebase\JWT; | |
| 4 | 4 | |
| 5 | 5 | use DomainException; |
| 6 | 6 | use InvalidArgumentException; |
| 7 | 7 | use UnexpectedValueException; |
| @@ -48,9 +48,9 @@ | ||
| 48 | 48 | * @throws DomainException OpenSSL failure |
| 49 | 49 | * |
| 50 | 50 | * @uses parseKey |
| 51 | 51 | */ |
| 52 | - public static function parseKeySet(array $jwks, string $defaultAlg = null): array | |
| 52 | + public static function parseKeySet(#[\SensitiveParameter] array $jwks, ?string $defaultAlg = null) : array | |
| 53 | 53 | { |
| 54 | 54 | $keys = []; |
| 55 | 55 | if (!isset($jwks['keys'])) { |
| 56 | 56 | throw new UnexpectedValueException('"keys" member must exist in the JWK Set'); |
| @@ -83,9 +83,9 @@ | ||
| 83 | 83 | * @throws DomainException OpenSSL failure |
| 84 | 84 | * |
| 85 | 85 | * @uses createPemFromModulusAndExponent |
| 86 | 86 | */ |
| 87 | - public static function parseKey(array $jwk, string $defaultAlg = null): ?Key | |
| 87 | + public static function parseKey(#[\SensitiveParameter] array $jwk, ?string $defaultAlg = null) : ?Key | |
| 88 | 88 | { |
| 89 | 89 | if (empty($jwk)) { |
| 90 | 90 | throw new InvalidArgumentException('JWK must not be empty'); |
| 91 | 91 | } |
| @@ -148,8 +148,13 @@ | ||
| 148 | 148 | } |
| 149 | 149 | // This library works internally with EdDSA keys (Ed25519) encoded in standard base64. |
| 150 | 150 | $publicKey = JWT::convertBase64urlToBase64($jwk['x']); |
| 151 | 151 | return new Key($publicKey, $jwk['alg']); |
| 152 | + case 'oct': | |
| 153 | + if (!isset($jwk['k'])) { | |
| 154 | + throw new UnexpectedValueException('k not set'); | |
| 155 | + } | |
| 156 | + return new Key(JWT::urlsafeB64Decode($jwk['k']), $jwk['alg']); | |
| 152 | 157 | default: |
| 153 | 158 | break; |
| 154 | 159 | } |
| 155 | 160 | return null; |
| @@ -162,12 +167,12 @@ | ||
| 162 | 167 | * @param string $y The EC y-coordinate |
| 163 | 168 | * |
| 164 | 169 | * @return string |
| 165 | 170 | */ |
| 166 | - private static function createPemFromCrvAndXYCoordinates(string $crv, string $x, string $y): string | |
| 171 | + private static function createPemFromCrvAndXYCoordinates(string $crv, string $x, string $y) : string | |
| 167 | 172 | { |
| 168 | 173 | $pem = self::encodeDER(self::ASN1_SEQUENCE, self::encodeDER(self::ASN1_SEQUENCE, self::encodeDER(self::ASN1_OBJECT_IDENTIFIER, self::encodeOID(self::OID)) . self::encodeDER(self::ASN1_OBJECT_IDENTIFIER, self::encodeOID(self::EC_CURVES[$crv]))) . self::encodeDER(self::ASN1_BIT_STRING, \chr(0x0) . \chr(0x4) . JWT::urlsafeB64Decode($x) . JWT::urlsafeB64Decode($y))); |
| 169 | - return sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n", wordwrap(base64_encode($pem), 64, "\n", \true)); | |
| 174 | + return \sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n", \wordwrap(\base64_encode($pem), 64, "\n", \true)); | |
| 170 | 175 | } |
| 171 | 176 | /** |
| 172 | 177 | * Create a public key represented in PEM format from RSA modulus and exponent information |
| 173 | 178 | * |
| @@ -177,9 +182,9 @@ | ||
| 177 | 182 | * @return string The RSA public key represented in PEM format |
| 178 | 183 | * |
| 179 | 184 | * @uses encodeLength |
| 180 | 185 | */ |
| 181 | - private static function createPemFromModulusAndExponent(string $n, string $e): string | |
| 186 | + private static function createPemFromModulusAndExponent(string $n, string $e) : string | |
| 182 | 187 | { |
| 183 | 188 | $mod = JWT::urlsafeB64Decode($n); |
| 184 | 189 | $exp = JWT::urlsafeB64Decode($e); |
| 185 | 190 | $modulus = \pack('Ca*a*', 2, self::encodeLength(\strlen($mod)), $mod); |
| @@ -201,9 +206,9 @@ | ||
| 201 | 206 | * |
| 202 | 207 | * @param int $length |
| 203 | 208 | * @return string |
| 204 | 209 | */ |
| 205 | - private static function encodeLength(int $length): string | |
| 210 | + private static function encodeLength(int $length) : string | |
| 206 | 211 | { |
| 207 | 212 | if ($length <= 0x7f) { |
| 208 | 213 | return \chr($length); |
| 209 | 214 | } |
| @@ -217,9 +222,9 @@ | ||
| 217 | 222 | * @param int $type DER tag |
| 218 | 223 | * @param string $value the value to encode |
| 219 | 224 | * @return string the encoded object |
| 220 | 225 | */ |
| 221 | - private static function encodeDER(int $type, string $value): string | |
| 226 | + private static function encodeDER(int $type, string $value) : string | |
| 222 | 227 | { |
| 223 | 228 | $tag_header = 0; |
| 224 | 229 | if ($type === self::ASN1_SEQUENCE) { |
| 225 | 230 | $tag_header |= 0x20; |
| @@ -235,14 +240,14 @@ | ||
| 235 | 240 | * |
| 236 | 241 | * @param string $oid the OID string |
| 237 | 242 | * @return string the binary DER-encoded OID |
| 238 | 243 | */ |
| 239 | - private static function encodeOID(string $oid): string | |
| 244 | + private static function encodeOID(string $oid) : string | |
| 240 | 245 | { |
| 241 | - $octets = explode('.', $oid); | |
| 246 | + $octets = \explode('.', $oid); | |
| 242 | 247 | // Get the first octet |
| 243 | - $first = (int) array_shift($octets); | |
| 244 | - $second = (int) array_shift($octets); | |
| 248 | + $first = (int) \array_shift($octets); | |
| 249 | + $second = (int) \array_shift($octets); | |
| 245 | 250 | $oid = \chr($first * 40 + $second); |
| 246 | 251 | // Iterate over subsequent octets |
| 247 | 252 | foreach ($octets as $octet) { |
| 248 | 253 | if ($octet == 0) { |
| @@ -255,10 +260,10 @@ | ||
| 255 | 260 | $octet >>= 7; |
| 256 | 261 | } |
| 257 | 262 | $bin[0] = $bin[0] & \chr(0x7f); |
| 258 | 263 | // Convert to big endian if necessary |
| 259 | - if (pack('V', 65534) == pack('L', 65534)) { | |
| 260 | - $oid .= strrev($bin); | |
| 264 | + if (\pack('V', 65534) == \pack('L', 65534)) { | |
| 265 | + $oid .= \strrev($bin); | |
| 261 | 266 | } else { |
| 262 | 267 | $oid .= $bin; |
| 263 | 268 | } |
| 264 | 269 | } |