saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.09k stars 107 forks source link

Remove authentication on a specific request #402

Closed labomatik closed 5 months ago

labomatik commented 7 months ago

I would like to remove the authentication on a specific request while the connector is using OAuth

The request to the API is the following:

The last step is actually sending an error when we push to AWS "Only one auth mechanism allowed;" since we are using an API key on that step

Is there a way to remove from the connector the Authentication?

        $api = new APIConnector(config('services.api.client_id'), config('services.api.client_secret'));

        $authenticator = $api->getAccessToken([
            "connect/read:jobs",
            "connect/submit:jobs",
            "connect/read:sending-events"
        ]);

        $api->authenticate($authenticator);

        $data = $api->send(new CreateJobSingleFile('01HT2RK56ETAD2GGNRGA4Z03BK', 'test', 'test.pdf'));

        $jobFile = $data->dtoOrFail();

        // We should now post file to $jobFile->fileUploadUrl

        $file = \Storage::disk('local')->path('test.pdf');

        // We should remove the Auth

        $result = $api->send(new AttachFileToJob('01HT2RK56ETAD2GGNRGA4Z03BK', 'test.pdf', $file, $jobFile->fileUploadUrl));
jlevers commented 7 months ago

Maybe you could do it with a NullAuthenticator, something like this:

class NullAuthenticator implements Authenticator
{
    public function set(PendingRequest $pendingRequest): void
    {
    }
}

And then when you want to make the auth-free call:

$api->authenticate(new NullAuthenticator());
Sammyjo20 commented 5 months ago

Hey @labomatik I have just released v3.9.0 which includes the above PR made by @patrickcarlohickman. Would you be able to try the NullAuthenticator and see if this fixes your issue?

labomatik commented 5 months ago

Hello,

Sorry for being late on this... I just validated and it seems that the nullAuthenticator doesn't erase all previous auth mechanism, the API we are using is still pinpointing an issue with a mixed auth... I added a debug just after the NullAuthenticator and i do see the oauthConfig object part of the connector

image

patrickcarlohickman commented 5 months ago

@labomatik ,

Changing the authenticator won't affect the oauthConfig property that is already set on the connector. However, the NullAuthenticator doesn't use this config data at all, so I think the issue is somewhere else.

Would you be able to provide the implementation for your APIConnector connector, AttachFileToJob request, and possibly a dump of the request actually made (with any sensitive data/ids/secrets/keys redacted).

Thanks, Patrick

labomatik commented 5 months ago

Thanks @patrickcarlohickman after a review on the AttachFileToJob i just found this:

 protected function defaultHeaders(): array
    {

        return [
            'x-api-key' => config('services.api.api_key'),
            //'Authorization' => null. ---> I removed this...
        ];
    }

And now it's working :-) Thanks for the support