-
Notifications
You must be signed in to change notification settings - Fork 27
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
thoughts on async implementation #72
Comments
Initial step for Colin-b#72
Initial step for Colin-b#72
Initial step for Colin-b#72
Initial step for Colin-b#72
Initial step for Colin-b#72
Initial step for Colin-b#72
Hello @rafalkrupinski Thanks a lot for your patience |
I've just pushed a commit that removes the inner Client from Auth classes - https://github.com/python-lapidary/httpx_auth_async/tree/feature/async. If you like the direction I can separate the flows to sync and async versions. edit: Because of that, and because auth flows need to read responses (which may be either sync or async), there isn't that much of implementation to share between a/sync versions. It's possible that I know OAuth2 too little, and a simpler implementation is possible, e.g. if only HTTP Basic authentication is ever used, it could be simply done in the OAuth class, without using meta-auth. Cheers |
The test suite is now ensuring non regression on async flows as well. Feel free to submit small scale focused PRs and I will gladly review. The client is a parameter that is necessary because some users might have rules that requires specific proxy settings, certificates or whatever and this is not the goal of this package to reproduce every feature httpx offers to cover those cases, so we delegate to the user, the responsibility to provide a properly configured client in case they need to. |
Is it something mounts can't handle? |
I am not sure to follow, if the client has custom rules to achieve auth and an additional set of rules to reach the authenticated endpoint, how would you do it without providing N parameters to every auth class that would need to be forwarded to the inner client of the auth class? Let's say your certificate differs and your proxy differs (mounts can cover proxy on client side, but not certificate). And this is only an example I can think of, I am sure we would have to cover a whole lot of use cases, corporate networks are a nightmare ;) |
How large is N? Cookies, headers, query params, body... perhaps a single parameter
This doesn't work? httpx.AsyncClient(mounts={'https://example.com', httpx.AsyncHTTPTransport(verify=ssl.SSLContext(...) or cert=...)})
Would be a great help if I knew some corner cases. |
You are right, I guess it's possible to do everything via mounts, though you would admit it's not as straightforward to provide for a client. Thanks again ! |
Thank you @Colin-b and all contributors for this awesome package!
Just wanted to share some thoughts on possibility of async OAuth2 flow. I don't know all the corner cases this package handles, so please let me know whenever I write something naive.
OAuth2 flows use a global cache with a (threading) lock. It's a problem for async code (Support for async httpx clients #48 (comment)), but also doesn't seem necessary. The token and lock could be simply instance variables. The only problem that that I can think of, is when user creates multiple instances with the same auth server and key. Is there a valid case for such usage?
Actually the cache holds two locks, one for accessing cache, the other for refreshing the lock. Again this seem unnecessary:
The OAuth2 flows use locks within
.auth_flow()
method, against the Auth documentation.This is addressed in Support for async httpx clients #48 patch.
Having two locks makes sense when storage and refresh were split. In this case acquiring the refresh lock could be pulled to
.a/sync_auth_flow()
..auth_flow()
, via.request_new_token()
uses own instance ofhttpx.Client
. Again, it doesn't seem necessary. Httpx already provides a/sync-portable protocol for making HTTP requests from Auth instances:response = yield request
.If the auth server needs different transport options than the target server, mounts can be used. And if authentication is needed by the authentication server (meta-auth,
client_auth
param), the yielded requests need to be processed by meta-auth first.Since all OAuth2 implementations in this package follow the pattern:
self.request_new_grant()
steps 1+2 run with a lock, and 3 is very lightweight, the entire flow can run with a single lock.
When the above are addressed, supporting both sync and async should be as simple as implementing this class:
Implementations would still yield their auth requests instead of directly using Client, and yield the user request before returning.
I've forked the repo to work on a POC. If it makes sense, and it's welcome, I'll make a PR. Meanwhile, comments are more than welcome.
edit 1: I've just noticed that auth token refresh requests also need to have authentication headers, and I guess that's why a client is used. But I still think it's unnecessary, and simply another Auth instance can be used
edit 2: It makes sense to have two locks if one lock protects a single Auth instance and the other the global cache.
The text was updated successfully, but these errors were encountered: