Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skipping authentication when request is cached #384

Open
ClaraLeigh opened this issue Mar 13, 2024 · 7 comments
Open

Skipping authentication when request is cached #384

ClaraLeigh opened this issue Mar 13, 2024 · 7 comments

Comments

@ClaraLeigh
Copy link

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
Copy link

leroy commented Mar 15, 2024

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

@ClaraLeigh
Copy link
Author

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
Copy link

gjinali commented Mar 19, 2024

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
Copy link
Collaborator

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
Copy link
Author

ClaraLeigh commented Mar 20, 2024

@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
Copy link
Collaborator

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

@ClaraLeigh
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants