Missing Authorization header using JWT

Refresh

November 2018

Views

2.1k time

4

I'm trying to setup JSON Web Tokens to communicate with my php backend from a mobile app. I can request a token just fine. When i need to validate it(or make a request to another endpoint), i setup the Authorization header with the following format:

Bearer <token here>

But for some reason on my backend, $_SERVER['HTTP_AUTHORIZATION'] is not set.

I'm on localhost using Mamp Pro with PHP7. This is a dump for my $_SERVER array:

Array
(
    [SERVER_SOFTWARE] => Apache
    [REQUEST_URI] => /wp-json/jwt-auth/v1/token/validate/
    [REDIRECT_STATUS] => 200
    [HTTP_HOST] => localhost.dev
    [CONTENT_TYPE] => application/x-www-form-urlencoded
    [CONTENT_LENGTH] => 54
    [HTTP_CONNECTION] => keep-alive
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => CocoaRestClient/15 CFNetwork/760.2.6 Darwin/15.3.0 (x86_64)
    [HTTP_ACCEPT_LANGUAGE] => en-us
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [PATH] => /usr/bin:/bin:/usr/sbin:/sbin
    [SERVER_SIGNATURE] => 
    [SERVER_NAME] => cloud.iblue.eu
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => /Applications/MAMP/htdocs/dev
    [SERVER_ADMIN] => [email protected]
    [SCRIPT_FILENAME] => /Applications/MAMP/htdocs/dev/index.php
    [REMOTE_PORT] => 51804
    [REDIRECT_URL] => /wp-json/jwt-auth/v1/token/validate/
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => POST
    [QUERY_STRING] => 
    [SCRIPT_NAME] => /index.php
    [PHP_SELF] => /index.php
    [REQUEST_TIME_FLOAT] => 1459177711.33
    [REQUEST_TIME] => 1459177711
    [argv] => Array
        (
        )

    [argc] => 0
)

When i'm trying to use HTTP Basic authentication with Basic dGVzdEB0ZXN0LmNvbToxMjM0NQ== as the authorization header, it works fine:

[PHP_AUTH_USER] => [email protected]
[PHP_AUTH_PW] => 12345

Any idea whats wrong?

2 answers

0

Я просто имел эту проблему (такой же плагин!), И решить ее путем редактирования функции проверки:

// in public/class-jwt-auth-plugin.php
// Check for auth in raw headers without $_SERVER variable (Apache Server).
if ( !$auth && function_exists('getallheaders') ) {
     $headers = getallheaders();
     $auth = isset($headers['Authorization'])
     ? $headers['Authorization'] : false;
}

Хотя я не эксперт PHP, я не понимаю, почему этот код не может быть включен в плагине, чтобы начать с.

8

Хорошо, я только что нашел ответ здесь: https://devhacksandgoodies.wordpress.com/2014/06/27/apache-pass-authorization-header-to-phps-_serverhttp_authorization/

Поэтому я добавил следующую строку в мой файл Htaccess и сосредоточил свой вопрос:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1