-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_bcrypt.py
65 lines (55 loc) · 1.67 KB
/
import_bcrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Import a user with salted bcrypt hash into Okta.
https://www.usenix.org/legacy/event/usenix99/provos/provos.pdf
"""
import bcrypt
import requests
from dotenv import load_dotenv
import os
def encode_bcrypt(password):
"""Create a Bcrypt password that Okta will accept"""
rounds = 10 # 20 works, but takes over 1 minute.
salt = bcrypt.gensalt(rounds)
hashed = bcrypt.hashpw(password, salt)
salt_only = salt.decode('utf-8').split('$')[3]
value_only = hashed.decode('utf-8').split('$')[3].replace(salt_only, '')
return {
'algorithm': 'BCRYPT',
'workFactor': rounds,
'salt': salt_only,
'value': value_only
}
username = '[email protected]'
password = 'P@ssword123'
hash = encode_bcrypt(password.encode())
print('bcrypt hash', hash)
load_dotenv()
# Store these in a local .env file.
url = os.getenv('OKTA_ORG_URL')
token = os.getenv('OKTA_API_TOKEN')
headers = {
'Authorization': f'SSWS {token}',
'Accept': 'application/json'
}
user = {
'profile': {
'firstName': 'Isaac',
'lastName': 'Brock',
'email': username,
'login': username
},
'credentials': {
'password': {
'hash': hash
}
}
}
# Create the user.
response = requests.post(f'{url}/api/v1/users', json=user, headers=headers)
print(response.json())
# Now, sign in as the user to verify the password hash was imported correctly.
response = requests.post(f'{url}/api/v1/authn', json={'username': username, 'password': password})
authn = response.json()
if response.ok:
print(authn['status'])
else:
print(authn['errorSummary'])