saloonphp / saloon

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

Skipping authentication when request is cached #384

Open ClaraLeigh opened 5 months ago

ClaraLeigh commented 5 months ago

Hey :)

I've been trying to think up a good way to skip authentication if a request has already been cached.

So for example, straight from the docs:

<?php
$connector = new WarehouseConnector;
$authenticator = $connector->getAccessToken();

// Use authenticator to authenticate your connector instance
$connector->authenticate($authenticator);

// Any request sent through this connector will now have authentication applied
$connector->send(new GetInventoryRequest);

If the GetInventoryRequest is cached, the request to fetch a new access token will still occur.

I will likely try and come up with a dirty hack for today to fix this for myself, but I'm wondering if there is a clean way to do this? ie, have the authentication inside the Request so it only runs if needed

leroy commented 5 months ago

A simple solution could be to cache your access token to reduce the number of token requests when your requests are cached aswell.

ClaraLeigh commented 5 months ago

I mean I yes but I was wondering if someone has a neat solution so that only occurs when needed instead of anytime the auth cache expires which is far more often in my situation

I do have a workaround but again if there isn't a built in solution perhaps it's a good one for the roadmap as it does make sense

gjinali commented 5 months ago

if I'm not missing something in your quest I think this should cover you:

https://docs.saloon.dev/conclusion/how-to-guides/per-request-authentication

Sammyjo20 commented 5 months ago

Hey @ClaraLeigh is this an OAuth2 integration or is your ->getAccessToken(); method one that you made yourself? If this is one of those APIs that need a token before making requests, but that token lasts, say 15 minutes - the guide that @gjinali kindly attached is exactly what you need.

If it's OAuth2 you're after, I will keep investigating

ClaraLeigh commented 5 months ago

@Sammyjo20 This is OAuth2, with the example code coming straight from the docs at: https://docs.saloon.dev/digging-deeper/oauth2-authentication/client-credentials-grant

Could definitely hack something into the per request too, I missed that doc hiding down there. Not sure its much neater than my current personal hack, so I might skip it for now.

Either way though I feel like this should probably be built in when using something like oAuth. Maybe something to put in for v4? I could have a crack but I'm not sure I have the time until after easter

Sammyjo20 commented 3 months ago

Hey @ClaraLeigh sorry for the delay on looking at this one more, I will hopefully look soon for you!

ClaraLeigh commented 3 months ago

@Sammyjo20 definitely not urgent by any means. I have a cache wrapper for my usecase which solves the problem. Would definitely improve the program itself to have this though