csrf-token-model.php
3 years ago
csrf-token-view.php
3 years ago
csrf-tools.php
3 years ago
wp-nonce-tools.php
3 years ago
wp-nonce-tools.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes\Security; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 7 | |
| 8 | class Wp_Nonce_Tools { |
| 9 | |
| 10 | const KEY = '_wpnonce'; |
| 11 | |
| 12 | public static function register() { |
| 13 | add_filter( |
| 14 | 'jet-form-builder/request-handler/request', |
| 15 | array( static::class, 'handle_request' ) |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | public static function get_nonce_id(): string { |
| 20 | $form_id = jet_fb_live_args()->form_id; |
| 21 | |
| 22 | return "jet-form-builder-wp-nonce-{$form_id}"; |
| 23 | } |
| 24 | |
| 25 | public static function get_nonce_field(): string { |
| 26 | if ( ! jet_fb_live_args()->is_use_nonce() ) { |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | return wp_nonce_field( static::get_nonce_id(), self::KEY, true, false ); |
| 31 | } |
| 32 | |
| 33 | public static function verify( $nonce ): bool { |
| 34 | return ( ! jet_fb_live_args()->is_use_nonce() || wp_verify_nonce( $nonce, static::get_nonce_id() ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param array $request |
| 39 | * |
| 40 | * @return array |
| 41 | * @throws Request_Exception |
| 42 | */ |
| 43 | public static function handle_request( array $request ): array { |
| 44 | $nonce = $request[ self::KEY ] ?? ''; |
| 45 | |
| 46 | if ( ! self::verify( $nonce ) ) { |
| 47 | throw ( new Request_Exception( 'Invalid nonce.' ) )->dynamic_error(); |
| 48 | } |
| 49 | |
| 50 | return $request; |
| 51 | } |
| 52 | |
| 53 | } |
| 54 |