Skip to content

Commit 45f28b8

Browse files
committed
Rework retrieve_authorization_headers for best practices
1 parent 110cc71 commit 45f28b8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/class-wp-json-authentication-oauth1.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,22 @@ public function parse_header( $header ) {
6161
}
6262

6363
public function retrieve_authorization_headers() {
64-
$auth_headers = ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ? $_SERVER['HTTP_AUTHORIZATION'] : false;
64+
if ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) {
65+
return wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] );
66+
}
6567

66-
if ( ! $auth_headers && function_exists( 'apache_request_headers' ) ) {
67-
$all_headers = apache_request_headers();
68+
if ( function_exists( 'apache_request_headers' ) ) {
69+
$headers = apache_request_headers();
6870

69-
$auth_headers = array_key_exists( 'Authorization', $all_headers ) ? $all_headers['Authorization'] : false;
71+
// Check for the authoization header case-insensitively
72+
foreach ( $headers as $key => $value ) {
73+
if ( strtolower( $key ) === 'authorization' ) {
74+
return $value;
75+
}
76+
}
7077
}
7178

72-
return $auth_headers;
79+
return null;
7380
}
7481

7582
public function get_parameters( $require_token = true, $extra = array() ) {
@@ -79,8 +86,6 @@ public function get_parameters( $require_token = true, $extra = array() ) {
7986
$auth_headers = $this->retrieve_authorization_headers();
8087

8188
if ( ! empty( $auth_headers ) ) {
82-
$auth_headers = wp_unslash( $auth_headers );
83-
8489
// Trim leading spaces
8590
$auth_headers = trim( $auth_headers );
8691

0 commit comments

Comments
 (0)