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

How to achieve Hashing/ Encrypting a password field? #110

Open
mtech2008 opened this issue Oct 14, 2019 · 2 comments
Open

How to achieve Hashing/ Encrypting a password field? #110

mtech2008 opened this issue Oct 14, 2019 · 2 comments

Comments

@mtech2008
Copy link

Hi,
How can I achieve Hashing/ Encrypting a password field?
How about having a HashField or MD5Field?

Regards

@abawchen
Copy link
Collaborator

@mtech2008 : It's more like a question about mongoengine, not a graphql one, you can refer: https://stackoverflow.com/questions/27943258/save-password-as-salted-hash-in-mongodb-in-users-collection-using-python-bcrypt

make sense?

@bhavishyasharma
Copy link

bhavishyasharma commented Apr 9, 2020

Can be done easily using bcrypt

Mutation Class

class RegisterUserMutation(graphene.Mutation):
    user = graphene.Field(UserType)

    class Arguments:
        user_data = UserInput(required=True)

    def mutate(self, info, user_data=None):
        user = UserModel(
            firstname = user_data.firstname,
            lastname = user_data.lastname,
            username = user_data.username,
            email = user_data.email,
            roles = list()
        )
        user.setPassword(user_data.password)
        user.save()
        return RegisterUserMutation(user=user)

Model Class

class UserModel(Document):
    meta = {'collection': 'user'}
    _id = ObjectIdField()
    firstname = StringField()
    lastname = StringField()
    email = EmailField()
    username = StringField()
    password = StringField()
    roles = ListField(ReferenceField(RoleModel, reverse_delete_rule=mongoengine.DENY))

    def setPassword(self,password):
        self.password = (bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())).decode("utf-8") 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants