Skip to content

Commit 4e6c0ae

Browse files
authored
fix: correct Auth0 export users script and update documentation (#2330)
* fix: correct Auth0 export users script and update documentation * docs: auth0 migration guid formatting improvements * docs: improve formatting in auth0 migration to follow our style guide. * docs: update Auth0 migration guide to correct project link * docs: refine Auth0 migration guide formatting and update project link reference * docs: update Auth0 migration guide for clarity and consistency * docs: clarify wording in Auth0 migration guide import script section * feat: enhance Auth0 migration script with environment variable checks. * docs: refine wording in Auth0 migration guide for bulk user export section * docs: remove redundant sections in Auth0 migration guide
1 parent f00c558 commit 4e6c0ae

File tree

3 files changed

+240
-104
lines changed

3 files changed

+240
-104
lines changed

code-examples/migrate-to-ory/0-get-auth0-user-data.sh

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#!/bin/bash
2+
set -euo pipefail
3+
4+
if [ -z "${AUTH0_DOMAIN:-}" ] || [ -z "${AUTH0_TOKEN:-}" ] || [ -z "${AUTH0_CONNECTION_ID:-}" ]; then
5+
echo "Error: Required environment variables not set"
6+
echo "Please set: AUTH0_DOMAIN, AUTH0_TOKEN, AUTH0_CONNECTION_ID"
7+
exit 1
8+
fi
9+
210
job_response=$(
311
curl --request POST -s --url "https://${AUTH0_DOMAIN}/api/v2/jobs/users-exports" \
412
--header "authorization: Bearer ${AUTH0_TOKEN}" \
513
--header "content-type: application/json" \
6-
--data '{"connection_id": "'$AUTH0_CONNECTION_ID'", "format": "json", "fields": [
14+
--data '{"connection_id": "'"$AUTH0_CONNECTION_ID"'", "format": "json", "fields": [
715
{"name": "user_id"},
816
{"name": "email"},
917
{"name": "email_verified"},
@@ -31,27 +39,46 @@ job_response=$(
3139

3240
job_id=$(echo "$job_response" | jq -r ".id")
3341

42+
if [ -z "$job_id" ] || [ "$job_id" = "null" ]; then
43+
echo "Error: Failed to create export job"
44+
echo "$job_response" | jq
45+
exit 1
46+
fi
47+
48+
echo "Export job created with ID: $job_id"
49+
3450
poll_job_status() {
3551
jobstatus=$(curl --request GET -s --url "https://${AUTH0_DOMAIN}/api/v2/jobs/${job_id}" --header "authorization: Bearer ${AUTH0_TOKEN}")
36-
state=$(echo $jobstatus | jq -r ".status")
37-
echo "jobstate: ${state}"
38-
39-
if [[ $state == "pending" ]] || [[ $state == "processing" ]]; then
40-
echo "${jobstatus}" | jq ".time_left_seconds" | read timeleft
41-
if [ -z $timeleft]; then
42-
sleep 1
43-
echo "polling job state"
52+
state=$(echo "$jobstatus" | jq -r ".status")
53+
echo "Job state: ${state}"
54+
55+
if [[ "$state" == "pending" ]] || [[ "$state" == "processing" ]]; then
56+
timeleft=$(echo "$jobstatus" | jq -r ".time_left_seconds")
57+
if [ "$timeleft" = "null" ] || [ -z "$timeleft" ]; then
58+
sleep 5
59+
echo "Polling job state..."
4460
else
45-
sleep $timeleft
46-
echo "time left: ${timeleft}s"
61+
echo "Time left: ${timeleft}s"
62+
sleep "$timeleft"
4763
fi
4864
poll_job_status
4965

50-
elif [[ $state == "completed" ]]; then
51-
location=$(echo $jobstatus | jq -r ".location")
66+
elif [[ "$state" == "completed" ]]; then
67+
location=$(echo "$jobstatus" | jq -r ".location")
5268
curl "$location" --silent --output "AUTH0_USERDATA_nd.json.gz"
5369
gzip -d -c "AUTH0_USERDATA_nd.json.gz" | jq -s "." >"AUTH0_USERDATA.json"
54-
echo "Finished downloading Auth0 user data!"
70+
echo "Finished downloading Auth0 user data to AUTH0_USERDATA.json!"
71+
72+
elif [[ "$state" == "failed" ]]; then
73+
echo "Error: Export job failed"
74+
echo "$jobstatus" | jq
75+
exit 1
76+
77+
else
78+
echo "Unknown job state: $state"
79+
echo "$jobstatus" | jq
80+
exit 1
5581
fi
5682
}
57-
poll_job_status
83+
84+
poll_job_status

code-examples/migrate-to-ory/1-create-ory-identities.sh

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Check required environment variables
5+
if [[ -z "${ORY_PROJECT_ID:-}" ]] || [[ -z "${ORY_WORKSPACE_ID:-}" ]]; then
6+
echo "Error: Required environment variables not set"
7+
echo "Please set: ORY_PROJECT_ID, ORY_WORKSPACE_ID"
8+
exit 1
9+
fi
10+
11+
if [[ -z "${AUTH0_USERDATA:-}" ]]; then
12+
echo "Error: AUTH0_USERDATA environment variable not set"
13+
echo "Please set: AUTH0_USERDATA (path to Auth0 user data JSON file)"
14+
exit 1
15+
fi
16+
17+
if [[ "${RESERVE_ONLY:-false}" != "true" ]] && [[ -z "${AUTH0_PWEXPORT:-}" ]]; then
18+
echo "Error: AUTH0_PWEXPORT environment variable not set"
19+
echo "Please set: AUTH0_PWEXPORT (path to password hashes JSON file)"
20+
echo "Or set RESERVE_ONLY=true to skip password import"
21+
exit 1
22+
fi
23+
224
create_payload() {
325
unset payload
426
ory_schema_id='preset://email'
@@ -21,7 +43,7 @@ create_payload() {
2143
'{schema_id: $sid,
2244
traits:
2345
{email: $em},
24-
metadata_admin: "auth0",
46+
metadata_admin: {origin: "auth0"},
2547
credentials:
2648
{password:
2749
{config:
@@ -35,7 +57,7 @@ create_identity() {
3557
echo "please supply a valid payload"
3658
exit 1
3759
else
38-
echo $payload | ory import identities --project $ORY_PROJECT_ID --workspace $ORY_WORKSPACE_ID
60+
echo $payload | ory import identities --workspace $ORY_WORKSPACE_ID --project $ORY_PROJECT_ID
3961
fi
4062
}
4163

@@ -47,13 +69,15 @@ if [[ "${RESERVE_ONLY}" == "true" ]]; then
4769
create_identity
4870
done
4971
else
50-
# add passwords to user data by email
51-
pw_hashes=$(cat "${AUTH0_PWEXPORT}" | jq -s "." | jq "map({email, passwordHash})")
52-
auth0_alldata=$(jq 'JOIN(INDEX(inputs[];.email);.[];.email;add)' <(cat "${AUTH0_USERDATA}") <(echo "$pw_hashes") | jq -s ".")
72+
# Create an index from passwords file and merge with user data
73+
auth0_alldata=$(jq --slurpfile pw "${AUTH0_PWEXPORT}" \
74+
'($pw[0] | INDEX(.email)) as $pw_index |
75+
. | map(. + {passwordHash: (if $pw_index[.email] then $pw_index[.email].passwordHash else null end)})' \
76+
"${AUTH0_USERDATA}")
5377

5478
echo "$auth0_alldata" | jq -r '.[] | .email, .passwordHash' | while read email && read pwhash; do
5579
create_payload
56-
create_identity $payload
80+
create_identity
5781
done
5882

5983
fi

0 commit comments

Comments
 (0)