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

Authorization (question) #39

Open
nightblure opened this issue Aug 1, 2024 · 4 comments
Open

Authorization (question) #39

nightblure opened this issue Aug 1, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@nightblure
Copy link

Hi!

Does your wrapper have the ability to send requests with an authentication token?
As a client I could provide an endpoint and authorization data, and in return I could receive, for example, a json web token. This assumes correct operation in a concurrent environment within one service instance and caching of the token until it expires

@Tishka17
Copy link
Member

Tishka17 commented Aug 1, 2024

It depends on a logic required for token and concrete http backend. Here are some trivial examples
https://github.com/annetutil/annetbox/blob/main/src/annetbox/base/client_sync.py#L149
https://github.com/reagento/dataclass-rest/blob/develop/examples/vkapi_example.py#L43

As you need refreshable token the simpliest way is to put that logic into do_request method:

class Client(RequestsClient):
    def do_request(self, request: HttpRequest) -> Any:
         token = get_token()  # you logic
         self.session.headers["Authorization"] = f"Token {token}"
         super().do_request(request)

From library side we can probably extend HttpRequest object with headers and add explicit authorization callback. But probably it is not required

@Tishka17 Tishka17 added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 1, 2024
@nightblure
Copy link
Author

nightblure commented Aug 1, 2024

I think your example works, but clearly in a suboptimal way. a token usually has a lifetime and it would be better for us to use the same token until the current one expires


By the optimal solution I mean your code but with the changes that the wrapper knows when to send a request to receive a token, and when not necessary, and it caches the token if it is still valid

Moreover, you will need to cache the token either locally (if there is one instance of the service), or in a shared data source such as postgresql, redis, and etc. (in the case of, for example, several k8s pods or several wsgi/asgi processes)

@Tishka17
Copy link
Member

Tishka17 commented Aug 1, 2024

I did not mean you need to get new token every time in that method. You can implement your own logic here. There are so many options that I can't imagine now how framework can provide any help here

@nightblure
Copy link
Author

Understood
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants