From c5ca061805257d558ac2e2cf00be4586b61e4de7 Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Fri, 5 May 2023 12:53:43 -0600 Subject: [PATCH] Add helper format_server_request --- includes/class-signature.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/includes/class-signature.php b/includes/class-signature.php index f735fc3..ae4ff61 100644 --- a/includes/class-signature.php +++ b/includes/class-signature.php @@ -319,4 +319,29 @@ class Signature { $digest = \base64_encode( \hash( 'sha256', $body, true ) ); // phpcs:ignore return "$digest"; } + + /** + * Formats the $_SERVER to resemble the WP_REST_REQUEST array, + * for use with verify_http_signature() + * + * @param array $_SERVER + * @return array $request + */ + public static function format_server_request( $server ) { + $request = array(); + foreach ( $server as $param_key => $param_val ) { + $req_param = strtolower( $param_key ); + if ( 'REQUEST_URI' === $req_param ) { + $request['headers']['route'][] = $param_val; + } else { + $header_key = str_replace( + 'http_', + '', + $req_param + ); + $request['headers'][ $header_key ][] = \wp_unslash( $param_val ); + } + } + return $request; + } }