Skip to content

Commit

Permalink
Refactor @dschreck header issue changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Sep 16, 2015
1 parent 8fe0308 commit 8a741bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
45 changes: 32 additions & 13 deletions src/MessageBridge.php
Expand Up @@ -20,18 +20,6 @@ public static function newOauth2Request(\Slim\Http\Request $request)
}
}

$headers = $request->headers()->getIterator()->getArrayCopy();
// Fixing bad headers from Slim
$badHeaders = ['Php-Auth-User','Php-Auth-Pw','Php-Auth-Digest','Auth-Type'];
$goodHeaders = ['PHP_AUTH_USER','PHP_AUTH_PW','PHP_AUTH_DIGEST','AUTH_TYPE'];

foreach ($badHeaders as $key => $badHeaderName) {
if (array_key_exists($badHeaderName,$headers)) {
$headers[$goodHeaders[$key]] = $headers[$badHeaderName];
unset($headers[$badHeaderName]);
}
}

return new \OAuth2\Request(
$request->get(),
$post,
Expand All @@ -40,7 +28,7 @@ public static function newOauth2Request(\Slim\Http\Request $request)
[],
\Slim\Environment::getInstance()->getIterator()->getArrayCopy(),
$request->getBody(),
$headers
self::cleanupHeaders($request->headers())
);
}

Expand All @@ -61,4 +49,35 @@ public static function mapResponse(\OAuth2\ResponseInterface $oauth2Response, \S
$slimResponse->status($oauth2Response->getStatusCode());
$slimResponse->setBody($oauth2Response->getResponseBody());
}

/**
* Helper method to clean header keys.
*
* Slim will convert all headers to Camel-Case style. There are certain headers such as PHP_AUTH_USER that the
* OAuth2 library requires CAPS_CASE format. This method will adjust those headers as needed.
*
* @param Slim\Http\Headers $uncleanHeaders The headers to be cleaned.
*
* @return array The cleaned headers
*/
private static function cleanupHeaders(\Slim\Http\Headers $uncleanHeaders)
{
$cleanHeaders = [];
$headerMap = [
'Php-Auth-User' => 'PHP_AUTH_USER',
'Php-Auth-Pw' => 'PHP_AUTH_PW',
'Php-Auth-Digest' => 'PHP_AUTH_DIGEST',
'Auth-Type' => 'AUTH_TYPE',
];
foreach ($uncleanHeaders as $key => $value) {
if (!array_key_exists($key, $headerMap)) {
$cleanHeaders[$key] = $value;
continue;
}

$cleanHeaders[$headerMap[$key]] = $value;
}

return $cleanHeaders;
}
}
4 changes: 2 additions & 2 deletions tests/MessageBridgeTest.php
Expand Up @@ -163,8 +163,8 @@ public function newOAuth2RequestHeaderKeyNames()
$this->assertSame('application/x-www-form-urlencoded', $oauth2Request->headers('Content-Type'));
$this->assertSame('123', $oauth2Request->request('abc'));
$this->assertSame('2', $oauth2Request->query('two'));
$this->assertSame('test_client_id',$oauth2Request->headers('PHP_AUTH_USER'));
$this->assertSame('test_secret',$oauth2Request->headers('PHP_AUTH_PW'));
$this->assertSame('test_client_id', $oauth2Request->headers('PHP_AUTH_USER'));
$this->assertSame('test_secret', $oauth2Request->headers('PHP_AUTH_PW'));
$this->assertNull($oauth2Request->headers('Php-Auth-User'));
$this->assertNull($oauth2Request->headers('Php-Auth-Pw'));

Expand Down

0 comments on commit 8a741bb

Please sign in to comment.