Skip to content

Instantly share code, notes, and snippets.

@mdeggies
Last active April 13, 2017 20:55
Show Gist options
  • Save mdeggies/7e25d01883592dce0fca19e1793208a4 to your computer and use it in GitHub Desktop.
Save mdeggies/7e25d01883592dce0fca19e1793208a4 to your computer and use it in GitHub Desktop.
(Python SDK) Import User with SHA-1 Password into Stormpath
"""
password-import.py
~~~~~~~~~
A super basic script that shows how to import a user with a freshly SHA-1 hashed password into Stormpath
and authenticate him using the Python SDK: https://docs.stormpath.com/python/
"""
from base64 import b64encode
from hashlib import sha1
from stormpath.client import Client
from stormpath.error import Error as StormpathError
client = Client(id='$API_KEY_ID', secret='$API_KEY_SECRET')
application = client.applications.search({'name': '$APPLICATION_NAME'})[0]
mcf_string = '$stormpath2$SHA-1$1$%s$%s'
username = raw_input("Input users email: ")
password = raw_input("Input users raw password: ")
salt = 'salty'
username_b64 = b64encode(username)
password_b64 = b64encode(password)
salt_b64 = b64encode(salt)
_hash = b64encode(sha1(salt + password).digest())
print(_hash)
account = application.accounts.create({
'email': username,
'password': mcf_string % (salt_b64, _hash),
}, password_format='mcf')
print('User created in Stormpath!')
try:
result = application.authenticate_account(username, password)
print('User ' + result.account.username + ' successfully authenticated.')
except StormpathError, e:
print('Could not log in. Error: ' + e.user_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment