1
Assuming you’ve implemented your own AbstractUser
sub-class named DannyUser
, you then have a couple of choices:
-
Embed all of the information needed into a new field (a
JSONField
subclass for example) — not really recommended as this leaks random information into a housekeeping table and is ill-advised IMO. -
Design a new Model for each authentication service and tie it via a foreign key & reverse relationship to your
DannyUser
instances. Then you could use something likemy_danny_instance.fastapi_account.auth_token
where needed. This will also allow you to encrypt specific fields or use a secret management solution later on.
Simply normalize your data as far as it makes sense to.
1
This is mostly a software architecture design problem, In your old login endpoint put every required data by the new FastAPI project in the JWT token payload(such as user_id, user_name and etc), sign it and give it to the user:
# using PyJWT
import jwt
user_token = jwt.encode(
{"user_id": "1222", "user_name": "yosef", },
"YOUR_SECRET_KEY",
algorithm="HS256",
headers={},
)
Afterwards in new FastAPI project endpoint, validate JWT token in request header with the key you have signed it in Django login endpoint:
# using PyJWT
import jwt
try:
data = jwt.decode(JWT_STRING_VAR, "YOUR_SECRET_KEY", algorithms=["HS256"])
except jwt.ExpiredSignatureError:
# reject
...
If the token is valid you accept the request and use the payload as confirmed data to do your usual endpoint functionality and if sign is not valid reject request.
This way you are not required to change your old endpoints at all.
*note: instead of PyJWT you can use SimpleJWT or any other package for Django and a suitable JWT package for FastAPI
**note: this scenario may go crazy complex when your JWT payload may get changed before the JWT expires, like when user_name changes but you can not change that in JWT and user can use it in your other service with old data, there are more solutions on this, like removing the properties that may change from payload and get them directly from database, meaning you query Django populated table from FastAPI, you can achieve this by import dynamic tables in SQLAlchemy
***note: system design schema image
- [Django]-Django/Python convert string to Model filter with '=' in result
- [Django]-Can I use alias field names in django templates?