AgenticCheckoutUtils.php
4 months ago
ArrayUtils.php
2 years ago
CartController.php
4 weeks ago
CartTokenUtils.php
1 year ago
CheckoutTrait.php
4 weeks ago
DraftOrderTrait.php
1 year ago
JsonWebToken.php
11 months ago
LocalPickupUtils.php
5 months ago
NoticeHandler.php
1 year ago
OrderAuthorizationTrait.php
6 months ago
OrderController.php
4 weeks ago
Pagination.php
2 years ago
PaymentUtils.php
1 year ago
ProductItemTrait.php
4 weeks ago
ProductLinksTrait.php
3 months ago
ProductQuery.php
2 months ago
ProductQueryFilters.php
11 months ago
QuantityLimits.php
11 months ago
RateLimits.php
1 year ago
SanitizationUtils.php
2 years ago
ValidationUtils.php
2 years ago
JsonWebToken.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 4 | |
| 5 | /** |
| 6 | * JsonWebToken class. |
| 7 | * |
| 8 | * Simple Json Web Token generator & verifier static utility class, currently supporting only HS256 signatures. |
| 9 | */ |
| 10 | final class JsonWebToken { |
| 11 | |
| 12 | /** |
| 13 | * JWT header type. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | private static $type = 'JWT'; |
| 18 | |
| 19 | /** |
| 20 | * JWT algorithm to generate signature. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private static $algorithm = 'HS256'; |
| 25 | |
| 26 | /** |
| 27 | * Generates a token from provided data and secret. |
| 28 | * |
| 29 | * @param array $payload Payload data. |
| 30 | * @param string $secret The secret used to generate the signature. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function create( array $payload, string $secret ) { |
| 35 | $header = self::to_base_64_url( self::generate_header() ); |
| 36 | $payload = self::to_base_64_url( self::generate_payload( $payload ) ); |
| 37 | $signature = self::to_base_64_url( self::generate_signature( $header . '.' . $payload, $secret ) ); |
| 38 | |
| 39 | return $header . '.' . $payload . '.' . $signature; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Validates a provided token against the provided secret. |
| 44 | * Checks for format, valid header for our class, expiration claim validity and signature. |
| 45 | * https://datatracker.ietf.org/doc/html/rfc7519#section-7.2 |
| 46 | * |
| 47 | * @param string $token Full token string. |
| 48 | * @param string $secret The secret used to generate the signature. |
| 49 | * |
| 50 | * @return bool |
| 51 | */ |
| 52 | public static function validate( string $token, string $secret ) { |
| 53 | if ( ! self::shallow_validate( $token ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | $parts = self::get_parts( $token ); |
| 58 | |
| 59 | /** |
| 60 | * Check if the token is based on our secret. |
| 61 | */ |
| 62 | $encoded_regenerated_signature = self::to_base_64_url( |
| 63 | self::generate_signature( $parts->header_encoded . '.' . $parts->payload_encoded, $secret ) |
| 64 | ); |
| 65 | |
| 66 | return hash_equals( $encoded_regenerated_signature, $parts->signature_encoded ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Shallow validate a token, it does not check the signature or expiration, but it checks the structure and expiry. |
| 71 | * |
| 72 | * @param string $token Full token string. |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public static function shallow_validate( string $token ) { |
| 77 | if ( ! $token ) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Confirm the structure of a JSON Web Token, it has three parts separated |
| 83 | * by dots and complies with Base64URL standards. |
| 84 | */ |
| 85 | if ( preg_match( '/^[a-zA-Z\d\-_=]+\.[a-zA-Z\d\-_=]+\.[a-zA-Z\d\-_=]+$/', $token ) !== 1 ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | $parts = self::get_parts( $token ); |
| 90 | |
| 91 | /** |
| 92 | * Check if header declares a supported JWT by this class. |
| 93 | */ |
| 94 | if ( |
| 95 | ! is_object( $parts->header ) || |
| 96 | ! property_exists( $parts->header, 'typ' ) || |
| 97 | ! property_exists( $parts->header, 'alg' ) || |
| 98 | self::$type !== $parts->header->typ || |
| 99 | self::$algorithm !== $parts->header->alg |
| 100 | ) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Check if token is expired. |
| 106 | */ |
| 107 | if ( ! property_exists( $parts->payload, 'exp' ) || time() > (int) $parts->payload->exp ) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the decoded/encoded header, payload and signature from a token string. |
| 116 | * |
| 117 | * @param string $token Full token string. |
| 118 | * |
| 119 | * @return object |
| 120 | */ |
| 121 | public static function get_parts( string $token ) { |
| 122 | $parts = explode( '.', $token ); |
| 123 | |
| 124 | return (object) array( |
| 125 | 'header' => json_decode( self::from_base_64_url( $parts[0] ) ), |
| 126 | 'header_encoded' => $parts[0], |
| 127 | 'payload' => json_decode( self::from_base_64_url( $parts[1] ) ), |
| 128 | 'payload_encoded' => $parts[1], |
| 129 | 'signature' => self::from_base_64_url( $parts[2] ), |
| 130 | 'signature_encoded' => $parts[2], |
| 131 | |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Generates the json formatted header for our HS256 JWT token. |
| 137 | * |
| 138 | * @return string|bool |
| 139 | */ |
| 140 | private static function generate_header() { |
| 141 | return wp_json_encode( |
| 142 | array( |
| 143 | 'alg' => self::$algorithm, |
| 144 | 'typ' => self::$type, |
| 145 | ) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Generates a sha256 signature for the provided string using the provided secret. |
| 151 | * |
| 152 | * @param string $string Header + Payload token substring. |
| 153 | * @param string $secret The secret used to generate the signature. |
| 154 | * |
| 155 | * @return false|string |
| 156 | */ |
| 157 | private static function generate_signature( string $string, string $secret ) { |
| 158 | return hash_hmac( |
| 159 | 'sha256', |
| 160 | $string, |
| 161 | $secret, |
| 162 | true |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Generates the payload in json formatted string. |
| 168 | * |
| 169 | * @param array $payload Payload data. |
| 170 | * |
| 171 | * @return string|bool |
| 172 | */ |
| 173 | private static function generate_payload( array $payload ) { |
| 174 | return wp_json_encode( array_merge( $payload, [ 'iat' => time() ] ) ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Encodes a string to url safe base64. |
| 179 | * |
| 180 | * @param string $string The string to be encoded. |
| 181 | * |
| 182 | * @return string |
| 183 | */ |
| 184 | private static function to_base_64_url( string $string ) { |
| 185 | return str_replace( |
| 186 | array( '+', '/', '=' ), |
| 187 | array( '-', '_', '' ), |
| 188 | base64_encode( $string ) // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Decodes a string encoded using url safe base64, supporting auto padding. |
| 194 | * |
| 195 | * @param string $string the string to be decoded. |
| 196 | * |
| 197 | * @return string |
| 198 | */ |
| 199 | private static function from_base_64_url( string $string ) { |
| 200 | /** |
| 201 | * Add padding to base64 strings which require it. Some base64 URL strings |
| 202 | * which are decoded will have missing padding which is represented by the |
| 203 | * equals sign. |
| 204 | */ |
| 205 | if ( strlen( $string ) % 4 !== 0 ) { |
| 206 | return self::from_base_64_url( $string . '=' ); |
| 207 | } |
| 208 | |
| 209 | return base64_decode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 210 | str_replace( |
| 211 | array( '-', '_' ), |
| 212 | array( '+', '/' ), |
| 213 | $string |
| 214 | ) |
| 215 | ); |
| 216 | } |
| 217 | } |
| 218 |