Skip to content

Instantly share code, notes, and snippets.

@fabiograsso
Last active September 26, 2026 11:36
Show Gist options
  • Select an option

  • Save fabiograsso/2bc80ee1a0f7dc90a6e6827c7cf53563 to your computer and use it in GitHub Desktop.

Select an option

Save fabiograsso/2bc80ee1a0f7dc90a6e6827c7cf53563 to your computer and use it in GitHub Desktop.
Discovers the OAuth endpoints, supported scopes, and resource advertised by an Okta Agent Gateway for configuring Dust Static OAuth
#!/usr/bin/env bash
# Discover the OAuth metadata advertised by an Okta Agent Gateway.
# Prints the authorization server, authorization/token endpoints, supported
# scopes, and resource value to use when configuring Dust Static OAuth.
# Requires curl and jq.
# Usage: bash discover-gateway-oauth.sh <gateway-url>
# or: GATEWAY_URL=<gateway-url> bash discover-gateway-oauth.sh
set -euo pipefail
gateway_url="${1:-${GATEWAY_URL:-}}"
if [[ -z "$gateway_url" ]]; then
printf 'Usage: %s <gateway-url>\n' "$0" >&2
printf ' or: GATEWAY_URL=<gateway-url> %s\n' "$0" >&2
exit 2
fi
if [[ "$gateway_url" != https://* || "$gateway_url" == *\?* || "$gateway_url" == *\#* ]]; then
printf 'Error: provide an HTTPS Gateway URL without a query string or fragment.\n' >&2
exit 2
fi
if ! command -v curl >/dev/null 2>&1 || ! command -v jq >/dev/null 2>&1; then
printf 'Error: this script requires both curl and jq.\n' >&2
exit 2
fi
gateway_url="${gateway_url%/}"
gateway_authority="${gateway_url#https://}"
gateway_origin="https://${gateway_authority%%/*}"
gateway_path="${gateway_url#"$gateway_origin"}"
if [[ -z "$gateway_path" ]]; then
gateway_path="/"
fi
protected_resource_url="${gateway_origin}/.well-known/oauth-protected-resource${gateway_path}"
protected_resource_metadata="$(curl --fail --silent --show-error --location "$protected_resource_url")" || {
printf 'Error: could not retrieve protected resource metadata from %s\n' "$protected_resource_url" >&2
exit 1
}
resource="$(jq -er '.resource' <<<"$protected_resource_metadata")" || {
printf 'Error: protected resource metadata does not contain a valid resource.\n' >&2
exit 1
}
authorization_server="$(jq -er '
.authorization_servers
| if type == "array" and length == 1 then .[0]
else error("expected exactly one authorization server")
end
' <<<"$protected_resource_metadata")" || {
printf 'Error: expected exactly one authorization server in protected resource metadata.\n' >&2
exit 1
}
supported_scopes="$(jq -er '
(.scopes_supported // [])
| if type == "array" then join(" ")
else error("scopes_supported must be an array")
end
' <<<"$protected_resource_metadata")" || {
printf 'Error: scopes_supported must be an array when present.\n' >&2
exit 1
}
openid_configuration_url="${authorization_server%/}/.well-known/openid-configuration"
openid_configuration="$(curl --fail --silent --show-error --location "$openid_configuration_url")" || {
printf 'Error: could not retrieve OIDC configuration from %s\n' "$openid_configuration_url" >&2
exit 1
}
issuer="$(jq -er '.issuer' <<<"$openid_configuration")" || {
printf 'Error: OIDC configuration does not contain a valid issuer.\n' >&2
exit 1
}
if [[ "$issuer" != "$authorization_server" ]]; then
printf 'Error: discovered issuer (%s) does not match the advertised authorization server (%s).\n' \
"$issuer" "$authorization_server" >&2
exit 1
fi
authorization_endpoint="$(jq -er '.authorization_endpoint' <<<"$openid_configuration")" || {
printf 'Error: OIDC configuration does not contain an authorization endpoint.\n' >&2
exit 1
}
token_endpoint="$(jq -er '.token_endpoint' <<<"$openid_configuration")" || {
printf 'Error: OIDC configuration does not contain a token endpoint.\n' >&2
exit 1
}
printf 'Authorization server: %s\n' "$authorization_server"
printf 'OAuth Token Endpoint: %s\n' "$token_endpoint"
printf 'OAuth Authorization Endpoint: %s\n' "$authorization_endpoint"
printf 'OAuth Scopes: %s\n' "${supported_scopes:-none advertised}"
printf 'OAuth Resource / Audience: %s\n' "$resource"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment