Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Tests/Hooks/ClerkBeforeRequestHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_adds_api_version_header(): void
{
// Create a mock for the RequestInterface
$request = $this->createMock(RequestInterface::class);

// Set up the mock to expect withHeader to be called with the correct parameters
$request->expects($this->once())
->method('withHeader')
Expand All @@ -27,19 +27,19 @@ public function test_adds_api_version_header(): void
$this->equalTo('2024-10-01')
)
->willReturnSelf();

// Create a mock for the HookContext
$hookContext = new HookContext('test_operation', null, null);

// Create the BeforeRequestContext with the HookContext
$context = new BeforeRequestContext($hookContext);

// Create the hook instance
$hook = new ClerkBeforeRequestHook();

// Call the beforeRequest method
$result = $hook->beforeRequest($context, $request);

// Assert that the result is the same as the request (since we configured the mock to return itself)
$this->assertSame($request, $result);
}
Expand Down
24 changes: 12 additions & 12 deletions Tests/Hooks/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ class HooksTest extends TestCase
{
private $apiVersion = '2024-10-01';

public function testGetJwksWithApiVersionHeader(): void
public function test_get_jwks_with_api_version_header(): void
{
// Create a container to capture the request
$container = [];
$history = Middleware::history($container);

// Create a mock response
$mockResponse = new Response(
200,
['Content-Type' => 'application/json'],
json_encode([
'keys' => []
])
200,
['Content-Type' => 'application/json'],
json_encode([
'keys' => [],
])
);

$mock = new MockHandler([$mockResponse]);
$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

// Create a client with the mock handler
$client = new Client(['handler' => $handlerStack]);

// Create SDK with the mock
$sdk = Backend\ClerkBackend::builder()
->setSecurity('sk_test_foo')
->setClient($client)
->build();

$sdk->jwks->get();
$sdk->jwks->getJWKS();

// Assert we made exactly one request
$this->assertCount(1, $container);

// Get the request from the container
$request = $container[0]['request'];

Expand Down
28 changes: 14 additions & 14 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
]);
$rectorConfig->paths([
__DIR__.'/src',
]);

$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
]);
$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
]);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
]);
};
51 changes: 40 additions & 11 deletions src/Helpers/Jwks/AuthenticateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,16 @@ public static function authenticateRequest(
*/
private static function getSessionToken(mixed $request): ?string
{
$authorizationHeader = self::getAuthorizationHeader($request);

if (in_array('getHeader', get_class_methods($request))) {
$authorizationHeaders = $request->hasHeader('Authorization') ? $request->getHeader('Authorization')[0] : null;
$cookieHeaders = $request->hasHeader('Cookie') ? $request->getHeader('Cookie')[0] : null;
} else {
$authorizationHeaders = $request->headers->get('Authorization');
$cookieHeaders = $request->headers->get('Cookie');
if (! empty($authorizationHeader)) {
return str_replace('Bearer ', '', $authorizationHeader);
}

if (! empty($authorizationHeaders)) {
return str_replace('Bearer ', '', $authorizationHeaders);
$cookieHeader = self::getCookieHeader($request);

}
if (! empty($cookieHeaders)) {
$cookies = array_map('trim', explode(';', $cookieHeaders));
if (! empty($cookieHeader)) {
$cookies = array_map('trim', explode(';', $cookieHeader));
foreach ($cookies as $cookie) {
[$name, $value] = explode('=', $cookie, 2);
if (str_starts_with($name, self::SESSION_COOKIE_NAME)) {
Expand All @@ -92,4 +87,38 @@ private static function getSessionToken(mixed $request): ?string

return null;
}

/**
* Get Authorization header.
*
* @param mixed $request The HTTP request
* @return string|null The Authorization header, if present
*/
private static function getAuthorizationHeader(mixed $request): ?string
{
if (method_exists($request, 'getHeader')) {
return $request->hasHeader('Authorization')
? ($request->getHeader('Authorization')[0] ?? null)
: null;
} else {
return $request->headers->get('Authorization');
}
}

/**
* Get Cookie header.
*
* @param mixed $request The HTTP request
* @return string|null The Cookie headers, if present
*/
private static function getCookieHeader(mixed $request): ?string
{
if (method_exists($request, 'getHeader')) {
return $request->hasHeader('Cookie')
? ($request->getHeader('Cookie')[0] ?? null)
: null;
} else {
return $request->headers->get('Cookie');
}
}
}