Google Workspace API creating user, password issue

56 views Asked by At

Im trying to create users using the workspace API, but I'm getting stuck at the password. If I use sha 256 the user is created but I can't log in with the password I made the user with. Below is my code

def create_user(self, email, password, first_name, last_name):
        if 8 < len(password) > 100:
            raise Exception("Password needs to be between 8-100 characters")
        characters = string.ascii_letters + string.digits + string.punctuation
        salt = ''.join(random.choice(characters) for _ in range(20))
        hashedPassword = hashlib.sha256(password.encode() + salt.encode()).hexdigest()
        hashedPassword = "$5$" + salt.encode().hex() + "$" + hashedPassword
        body = {
            "primaryEmail": email,
            "password": hashedPassword,
            "hashFunction": "crypt",
            "name": {
                "givenName": first_name,
                "familyName": last_name
            },
            "changePasswordAtNextLogin": False
        }
        user = self.service.users().insert(body=body).execute()
        return user
0

There are 0 answers