2026-02-27 21:51:40 -07:00
|
|
|
#!/usr/bin/env bash
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
set -euo pipefail
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Constants
|
|
|
|
|
CREDENTIALS_FILE="/etc/config/oauth2/client/client-credentials.json"
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Create the credentials file if it doesn't exist
|
|
|
|
|
if [ ! -f "$CREDENTIALS_FILE" ]; then
|
|
|
|
|
echo "ERROR: credentials file $CREDENTIALS_FILE not found"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Wait for Hydra to be ready
|
|
|
|
|
sleep 3
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Function to create client
|
|
|
|
|
create_client() {
|
|
|
|
|
local client_id=$1
|
|
|
|
|
local client_secret=$2
|
|
|
|
|
local scope=$3
|
|
|
|
|
local exists_in_hydra=false
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Check if client exists in Hydra
|
|
|
|
|
if hydra get oauth2-client $client_id --endpoint $HYDRA_ADMIN_URL >/dev/null 2>&1; then
|
|
|
|
|
exists_in_hydra=true
|
2026-02-27 20:46:59 -07:00
|
|
|
fi
|
|
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Log client existence status
|
|
|
|
|
if [ "$exists_in_hydra" = true ]; then
|
|
|
|
|
echo "INFO: client $client_id exists in Hydra"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
2026-02-27 20:46:59 -07:00
|
|
|
|
2026-02-27 21:51:40 -07:00
|
|
|
# Create new client if it doesn't exist in Hydra
|
|
|
|
|
if [ "$exists_in_hydra" = false ]; then
|
|
|
|
|
client_output=$(hydra create oauth2-client --endpoint $HYDRA_ADMIN_URL \
|
|
|
|
|
--id $client_id \
|
|
|
|
|
--secret $client_secret \
|
|
|
|
|
--grant-type "client_credentials" \
|
|
|
|
|
--response-type "token" \
|
|
|
|
|
--scope "$scope" \
|
|
|
|
|
--token-endpoint-auth-method "client_secret_post" \
|
|
|
|
|
--format json)
|
|
|
|
|
|
|
|
|
|
echo "INFO: client $client_id created"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Load client credentials
|
|
|
|
|
jq -c '.[]' "$CREDENTIALS_FILE" | while read -r client; do
|
|
|
|
|
client_id=$(echo "$client" | jq -r '.client_id')
|
|
|
|
|
client_secret=$(echo "$client" | jq -r '.client_secret')
|
|
|
|
|
scope=$(echo "$client" | jq -r '.scope')
|
|
|
|
|
create_client "$client_id" "$client_secret" "$scope"
|
|
|
|
|
done
|