# Wrong password for imported users from Django

**URL:** <https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105>\
**Category:** Get Help\
**Tags:** django\
**Created:** [April 14, 2021, 4:40pm UTC](https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105 "2021-04-14T16:40:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quantra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/quantra/32/12799_2.png) [@Quantra](https://community.auth0.com/u/Quantra)\
**Post date:** [April 14, 2021, 4:40pm UTC](https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105/1 "2021-04-14T16:40:18Z")

</div>

I have a Django app that I want to import the users from into Auth0.

I am able to do this but when I try and log in using my email and password which has been imported I get ‘Wrong email or password.’

I would like to know how to import these users and passwords correctly so they work in Auth0?

I have been following these guides:

[https://auth0.com/docs/users/bulk-user-imports](https://auth0.com/docs/users/bulk-user-imports)

> **[Bulk User Import Database Schema and Examples](https://auth0.com/docs/manage-users/user-migration/bulk-user-import-database-schema-and-examples)**
>
> Learn how to perform bulk user imports with the Management API.

I am using the Auth0 python API wrapper:

> **[GitHub - auth0/auth0-python: Auth0 SDK for Python](https://github.com/auth0/auth0-python)**
>
> Auth0 SDK for Python. Contribute to auth0/auth0-python development by creating an account on GitHub.

I have written a Django management command to import the users.

```
import io
import json
from base64 import b64encode

from auth0.v3.authentication import GetToken
from auth0.v3.management import Auth0
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management import BaseCommand

User = get_user_model()

class Command(BaseCommand):
    """
    Take all users in database and import them to Auth0
    """
    help = 'Take all users in database and import them to Auth0'

    def handle(self, *args, **options):
        self.stdout.write('Generating users.json')

        users = []
        for user in User.objects.all():
            if not user.email:
                self.stdout.write(f'User {user.id} has no email.')
                continue

            if not user.password:
                self.stdout.write(f'User {user.id} has no password.')
                continue

            password_parts = user.password.split('$')

            password_parts[0] = password_parts[0].replace('_', '-')
            password_parts[2] = b64encode(password_parts[2].encode()).decode().replace('=', '')
            password_parts[3] = password_parts[3].replace('=', '')
            password_hash = f'${password_parts[0]}$i={password_parts[1]},l=64${password_parts[2]}${password_parts[3]}'

            user_dict = {
                'email': user.email,
                'email_verified': False,
                'custom_password_hash': {
                    'algorithm': 'pbkdf2',
                    'hash': {
                        'value': password_hash,
                        'encoding': 'utf8'
                    }
                }
            }

            users.append(user_dict)

        json_file = io.StringIO()
        json.dump(users, json_file)
        json_file.seek(0)

        self.stdout.write('Importing users to Auth0')

        get_token = GetToken(settings.SOCIAL_AUTH_AUTH0_DOMAIN)
        token = get_token.client_credentials(
            settings.SOCIAL_AUTH_AUTH0_KEY,
            settings.SOCIAL_AUTH_AUTH0_SECRET,
            f'https://{settings.SOCIAL_AUTH_AUTH0_DOMAIN}/api/v2/'
        )
        mgmt_api_token = token['access_token']

        auth0 = Auth0(settings.SOCIAL_AUTH_AUTH0_DOMAIN, mgmt_api_token)

        response = auth0.jobs.import_users(settings.SOCIAL_AUTH_AUTH0_CONNECTION_ID, json_file)

        self.stdout.write(str(response))

        json_file.close()

```

Here is an example of how a password is stored (for a fake user) in Django and the hash value sent to Auth0.

```
pbkdf2_sha256$150000$olC2KpMR07WB$onWQOO++v7VYL1e5J6+Ma53f32MxQBGjG4ZANvfEbSM=

$pbkdf2-sha256$i=150000,l=64$b2xDMktwTVIwN1dC$onWQOO++v7VYL1e5J6+Ma53f32MxQBGjG4ZANvfEbSM

```

---

<div class="post-metadata">

**Author:** ![Quantra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/quantra/32/12799_2.png) [@Quantra](https://community.auth0.com/u/Quantra)\
**Post date:** [April 15, 2021, 12:24pm UTC](https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105/2 "2021-04-15T12:24:07Z")

</div>

I have resolved this issue here and will leave the complete management command incase someone else would like to use it in the future to export/import their users from Django to Auth0.

It turns out I was bamboozled by this comment in the Django source.

```
class PBKDF2PasswordHasher(BasePasswordHasher):
    """
    Secure password hashing using the PBKDF2 algorithm (recommended)

    Configured to use PBKDF2 + HMAC + SHA256.
    The result is a 64 byte binary string. Iterations may be changed
    safely but you must rename the algorithm if you change SHA256.
    """

```

I thought this meant l=64. However after some experimentation I discovered l=32. Changing this line in my management command resolved the issue.

```
password_hash = f'${password_parts[0]}$i={password_parts[1]},l=32${password_parts[2]}${password_parts[3]}'

```

Here is the complete management command with the corrected keylen.

```
import io
import json
from base64 import b64encode

from auth0.v3.authentication import GetToken
from auth0.v3.management import Auth0
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management import BaseCommand

User = get_user_model()

class Command(BaseCommand):
    """
    Take all users in database and import them to Auth0
    """
    help = 'Take all users in database and import them to Auth0'

    def handle(self, *args, **options):
        self.stdout.write('Generating users.json')

        users = []
        for user in User.objects.all():
            if not user.email:
                self.stdout.write(f'User {user.id} has no email.')
                continue

            if not user.password:
                self.stdout.write(f'User {user.id} has no password.')
                continue

            password_parts = user.password.split('$')

            password_parts[0] = password_parts[0].replace('_', '-')
            password_parts[2] = b64encode(password_parts[2].encode()).decode().replace('=', '')
            password_parts[3] = password_parts[3].replace('=', '')

            password_hash = f'${password_parts[0]}$i={password_parts[1]},l=32${password_parts[2]}${password_parts[3]}'

            user_dict = {
                'email': user.email,
                'email_verified': True,
                'custom_password_hash': {
                    'algorithm': 'pbkdf2',
                    'hash': {
                        'value': password_hash,
                        'encoding': 'utf8'
                    }
                }
            }

            users.append(user_dict)

        json_file = io.StringIO()
        json.dump(users, json_file)
        json_file.seek(0)

        self.stdout.write('Importing users to Auth0')

        get_token = GetToken(settings.SOCIAL_AUTH_AUTH0_DOMAIN)
        token = get_token.client_credentials(
            settings.SOCIAL_AUTH_AUTH0_KEY,
            settings.SOCIAL_AUTH_AUTH0_SECRET,
            f'https://{settings.SOCIAL_AUTH_AUTH0_DOMAIN}/api/v2/'
        )
        mgmt_api_token = token['access_token']

        auth0 = Auth0(settings.SOCIAL_AUTH_AUTH0_DOMAIN, mgmt_api_token)

        response = auth0.jobs.import_users(settings.SOCIAL_AUTH_AUTH0_CONNECTION_ID, json_file)

        self.stdout.write(str(response))

        json_file.close()

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 15, 2021, 12:46pm UTC](https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105/3 "2021-04-15T12:46:01Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 29, 2021, 12:42pm UTC](https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105/4 "2021-04-29T12:42:42Z")

</div>


