Skip to content

Instantly share code, notes, and snippets.

@ndom91
Created February 23, 2023 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndom91/c461715c6daebb5364a8ff46c59622da to your computer and use it in GitHub Desktop.
Save ndom91/c461715c6daebb5364a8ff46c59622da to your computer and use it in GitHub Desktop.
Auth0 Bulk User Export
#!/usr/bin/env bash
#
# ndom91 - 23.02.2023
# Based off of: https://github.com/ory/docs/blob/cef92348c250b06efdf65de0ce6c3721147a718c/code-examples/migrate-to-ory/0-get-auth0-user-data.sh
# Management API Tokens: https://manage.auth0.com/dashboard/eu/checkly/apis/management/explorer
# Auth0 API /users-exports docs: https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports
GREEN='\033[0;32m'
BLUE='\033[0;34m'
BRED='\033[1;31m'
BORANGE='\033[1;33m'
NC='\033[0m' # No Color
S='s'
AUTH0_DOMAIN="" # i.e. 'company.eu.auth0.com'
AUTH0_TOKEN="" # i.e. 'eyJh.....'
job_response=$(
curl --request POST -s --url "https://${AUTH0_DOMAIN}/api/v2/jobs/users-exports" \
--header "authorization: Bearer ${AUTH0_TOKEN}" \
--header "content-type: application/json" \
--data '{"format": "json", "fields": [
{"name": "user_id"},
{"name": "email"},
{"name": "email_verified"},
{"name": "emails"},
{"name": "username"},
{"name": "phone_number"},
{"name": "phone_verified"},
{"name": "created_at"},
{"name": "updated_at"},
{"name": "identities[0].connection",
"export_as": "provider" },
{"name": "app_metadata"},
{"name": "user_metadata"},
{"name": "picture"},
{"name": "name"},
{"name": "nickname"},
{"name": "multifactor"},
{"name": "last_ip"},
{"name": "last_login"},
{"name": "logins_count"},
{"name": "blocked"},
{"name": "given_name"},
{"name": "html_url", "export_as": "github_url"},
{"name": "organizations_url", "export_as": "github_organizations_url"},
{"name": "family_name"}
]}'
)
job_id=$(echo "$job_response" | jq -r ".id")
echo -e "\nStarted job"
echo -e "job_id: ${BRED}${job_id}${NC}\n"
poll_job_status() {
jobstatus=$(curl --request GET -s --url "https://${AUTH0_DOMAIN}/api/v2/jobs/${job_id}" --header "authorization: Bearer ${AUTH0_TOKEN}")
state=$(echo "$jobstatus" | jq -r ".status")
if [[ $state == "pending" ]]; then
echo "polling job state"
sleep 2
poll_job_status
elif [[ $state == "completed" ]]; then
location=$(echo "$jobstatus" | jq -r ".location")
curl "$location" --silent --output "auth0_userdata.json.gz"
gzip -d -c "auth0_userdata.json.gz" | jq -s "." >"auth0_userdata.json"
echo -e "\n${GREEN}Finished${NC} downloading Auth0 user data!"
echo -e "Check out ${BORANGE}auth0_userdata.json${NC}"
else
sleep 1
timeleft=$(echo "${jobstatus}" | jq ".time_left_seconds")
percentage_done=$(echo "${jobstatus}" | jq ".percentage_done")
if [[ -n $timeleft && -n $percentage_done ]]; then
echo -e "${BLUE}Exporting${NC}: $percentage_done% ($timeleft$S remaining)"
else
echo "polling job state"
fi
poll_job_status
fi
}
poll_job_status
@ndom91
Copy link
Author

ndom91 commented Feb 23, 2023

Will dump your requested auth0_userdata.json(.gz) to the current directory.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment