Laminas services cant access bearer token passed in header.?

Same code is working in others system.
I use the same composer, xamp and php versions they using.
Composer 2.7.4
Xamp 8.2

Iam using laminas AuthenticationService for accessing token. Token passed as bearer token in header.

header values available in getallheaders(), but cant access using laminas AuthenticationService.

Hi and welcome to the forum :slight_smile:

You are going to need to give us some more information before we can help you.

Can you show us how you initialized the laminas-authentication service?

Thanks :slight_smile:

Issue in using Laminas AuthenticationService,
getallheaders() returns the headers passed.

servise used
use Laminas\Authentication\AuthenticationService;
in constructor, $this->auth = new AuthenticationService();

$this->auth->getIdentity(); this returns NULL

How are you actually using the AuthenticationService to authenticate the user? Which Authentication Adapter are you using?

Where is your call to the authenticate method?

$result = $this->auth->authenticate($adapter);

Please see:

authentication works fine here, and token is generated.
The problem with other APIs which sends this auth token in header. that token can’t be accessed by Laminas\Authentication\AuthenticationService.
$this->auth itself returns NULL.

Have you verified that the auth data is being correctly written to whatever mechanism you are using to store it? Laminas auth “generally” creates its own session container for persistence of that data. I have no idea how you have it setup so beyond that… Not sure which direction to point you in.

Actually you should use Laminas’ own request and response objects, that contain all headers received and going to send. For doing so write your own authentication adapter factory and pass the request and response classes to your instance. Actually this is the way I would prefer. There might be other ways to inject the request object to your authentication adapter.

Just have a look at this example code.

<?php 
// BearerAuthenticationAdapterFactory.php
declare(strict_types=1);

namespace Marcel\Authentication\Adapter\Factory;

class BearerAuthenticationAdapterFactory 
{
    public function __invoke(ContainerInterface $container): AuthenticationAdapter
    {
        $request = $container->get('request');
        return new BearerAuthenticationAdapter($request);
    }
}

In your adapter class just access the header data with something like

$headers = $this->getRequest()->getHeaders();
$authorization = $headers->get('Authorization')->getFieldValue();

It would be nice if you had shown your adapter code. But as there is just this limited information about your issue, that 's all I can give you.

1 Like