labd / django-cognito-jwt

An Authentication backend for Django Rest Framework for AWS Cognito JWT tokens
MIT License
177 stars 59 forks source link

Adding the option to use a custom user manager to retrieve the user f… #27

Open tonibagur opened 4 years ago

tonibagur commented 4 years ago

Adding the option to use a custom user manager to retrieve the user from cognito without having to create a custom user model To use that feature, all you have to do is to configure the variable COGNITO_USER_MANAGER=cognito_users.models.CognitoManager

Then you can define you own manager: class CognitoManager(BaseUserManager): def get_or_create_for_cognito(self, jwt_payload): username = jwt_payload['cognito:username'] email = jwt_payload['email'] groups = jwt_payload['cognito:groups'] try: user = get_user_model().objects.get(username=username) except: password = get_user_model().objects.make_random_password() user = get_user_model().objects.create_user(username, email, password) if 'superusers' in groups: user.is_superuser = True user.is_active = True user.is_staff = True user.save() return user

In that way you don't need to customize your COGNITO_USER_MODEL which could be hard on an already running project.

Please, tell me if you would be interested in accepting a feature like that. If you are interested I can add documentation for it