Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Created April 23, 2025 08:57
Show Gist options
  • Save jesperronn/a6f508fa969e66d7528d1d058133e689 to your computer and use it in GitHub Desktop.
Save jesperronn/a6f508fa969e66d7528d1d058133e689 to your computer and use it in GitHub Desktop.
creates multiple identity providers in Keycloak using the Keycloak API (curl)
#!/usr/bin/env bash
set -euo pipefail
# Author: Jesper Rønn-Jensen, Nine A/S
# Date: 2025-04-23
# Version: 1.0
# Description: This script creates multiple identity providers in Keycloak using the Keycloak API.
# Example on how to use Keycloaks API to create multiple Identity providers
# the script file here uses curl command to provide the example
#
# Usage: ./keycloak_create_multi_idps.sh
KEYCLOAK_URL="https://keycloak.example.com"
KEYCLOAK_REALM="example-realm"
KEYCLOAK_USERNAME="admin"
KEYCLOAK_PASSWORD="password"
KEYCLOAK_CLIENT_ID="admin-cli"
IDP_RANGE_START=1
IDP_RANGE_END=500
function keycloak_login() {
# Get the access token
local response
response=$(curl -s -X POST "${KEYCLOAK_URL}/auth/realms/${KEYCLOAK_REALM}/protocol/openid-connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=${KEYCLOAK_CLIENT_ID}" \
-d "username=${KEYCLOAK_USERNAME}" \
-d "password=${KEYCLOAK_PASSWORD}" \
-d "grant_type=password")
# Extract the access token from the response
KEYCLOAK_ACCESS_TOKEN=$(echo "$response" | jq -r '.access_token')
}
function keycloak_create_identity_provider() {
local provider_alias=$1
local provider_url=$2
# Create the identity provider
curl -s -X POST "${KEYCLOAK_URL}/auth/admin/realms/${KEYCLOAK_REALM}/identity-provider/instances" \
-H "Authorization: Bearer ${KEYCLOAK_ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"alias": "'"${provider_alias}"'",
"providerId": "'"${provider_url}"'",
"enabled": true,
"config": {}
}'
}
run_main() {
keycloak_login
for item in $(seq "${IDP_RANGE_START}" "${IDP_RANGE_END}"); do
alias="fake_idp_${item}"
url="https://fake-idp-${item}.example.com"
keycloak_create_identity_provider "${alias}" "${url}"
done
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
run_main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment