Home » Php » php – Missing Authorization header using JWT

php – Missing Authorization header using JWT

Posted by: admin July 12, 2020 Leave a comment


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:

    [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_ENCODING] => gzip, deflate
    [PATH] => /usr/bin:/bin:/usr/sbin:/sbin
    [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/
    [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?

How to&Answers:

Ok, i just found the answer here: https://devhacksandgoodies.wordpress.com/2014/06/27/apache-pass-authorization-header-to-phps-_serverhttp_authorization/

So i added the following line to my htaccess file and it fixed my issue:

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


I just had this problem (same plugin!), and solved it by editing the validation function:

// 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;

Although I’m not a PHP expert, I don’t see why this code can’t be included in the plugin to start with.