Missing Authorization header using JWT

passatgt picture passatgt · Mar 28, 2016 · Viewed 7.3k times · Source

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?

Answer

passatgt picture passatgt · Mar 28, 2016

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