#different ways of authentication django microservice without dedicated user models
19 messages · Page 1 of 1 (latest)
Do you have this database listed in your DATABASES setting?
If you do, you can use User.objects.using('other_db') in a custom authentication class: https://www.django-rest-framework.org/api-guide/authentication/#example
Django, API, REST, Authentication
@green dagger Thanks for the response, but the microservice does not access to the main database itself
Do you have the code to determine if a request is authenticated through your microservice?
If yes, wrap that in a custom authentication class as described in the link above.
You'll need to create a proxy user account to associate with it in your django application.
(I think)
Thats the problem, i dont have a method to authenticate the requests made to the microservice
Why not? Sorry if that's an obvious question.
Okay maybe I am missing a few strings here, but how do i authenticate a microservice ?
It depends on the microservice.
its a todo app
From my perspective you're asking, "how do I use X to authenticate my django app?". In the scenario that I understand X is already built and a known quantity. Is that correct?
yes
Who built X / the microservice that you're supposed to be using for authentication?
it is like this situation : So far, I have two separate projects with two separate databases,
- To authenticate the user using simplejwt authentication. (todo_auth project with todo_auth database)
- To show the todo/task information specific to that user. (todo project with todo database)
in this case i want to authenticate apis of 2 using users of 1
Assuming todo_auth is done and is using django-rest-framework-simplejwt, you should be able to authenticate with something like their example: https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html#usage
You'll need to use requests or something similar instead of curl though. In your todo project, you'll create a custom authentication class as I linked originally, and make a request to your todo_auth DRF web service. You'll have to check that the token is valid, get the unique user identifer (maybe a separate web service or you'll have to customize the /api/token/ view). Then create a user for that token in todo's database and return that user from your authentication class.
That said, unless you're specifically trying to learn this I would recommend building a monolith.