Keycloak Client Secrets: Configuration, Retrieval, and Security Practices

Dec 03, 2025 · Programming · 12 views · 7.8

Keywords: Keycloak | Client Secret | OAuth 2.0

Abstract: This article delves into the conditions for the existence and methods of retrieving client secrets in Keycloak. Based on the OAuth 2.0 and OpenID Connect protocols, clients are categorized as confidential or public, with only confidential clients possessing a client secret. The article details how to generate a secret by setting the access type to "confidential" or enabling client authentication in the Keycloak admin interface, and viewing it in the Credentials tab. Additionally, it provides programming examples for retrieving secrets via the Keycloak Admin API and discusses best practices for secret management, including regular rotation, secure storage, and access control.

Client Types and Secret Existence

Keycloak, as an identity and access management solution, implements the OpenID Connect protocol, which is built on top of OAuth 2.0. According to the OAuth 2.0 specification, clients are divided into confidential clients and public clients. Confidential clients can securely store credentials, such as client ID and client secret, while public clients cannot. Therefore, Keycloak generates and provides a client secret only when a client is configured as confidential. This ensures that only trusted backend servers can use grant types that require authentication, such as providing client ID and secret when calling the token endpoint.

Configuring and Retrieving Client Secrets

In the Keycloak admin interface, configuring a client to have a secret involves the following steps. First, when creating or editing a client, the access type must be set to "confidential". In the old UI, this is done via the "Access Type" option; in the new UI, the "Client authentication" option must be enabled. After saving the settings, a new "Credentials" tab appears, containing an auto-generated client secret. The secret is typically displayed as a string, and users can view it in plaintext by clicking an eye icon. For example, in code, this corresponds to setting the access-type property to confidential.

Here is a simplified example demonstrating how to retrieve a client secret via the Keycloak Admin API. Assume we use Python and the requests library:

import requests

# Configure Keycloak server and authentication info
keycloak_url = "https://keycloak.example.com"
realm = "myrealm"
client_id = "myclient"
admin_token = "your_admin_access_token"  # Obtained via admin login

# Build API endpoint
endpoint = f"{keycloak_url}/admin/realms/{realm}/clients"
headers = {
    "Authorization": f"Bearer {admin_token}",
    "Content-Type": "application/json"
}

# First, get the internal ID corresponding to the client ID
response = requests.get(endpoint, headers=headers)
clients = response.json()
client_internal_id = None
for client in clients:
    if client["clientId"] == client_id:
        client_internal_id = client["id"]
        break

if client_internal_id:
    # Retrieve the client secret
    secret_endpoint = f"{endpoint}/{client_internal_id}/client-secret"
    secret_response = requests.get(secret_endpoint, headers=headers)
    if secret_response.status_code == 200:
        client_secret = secret_response.json().get("value")
        print(f"Client Secret: {client_secret}")
    else:
        print("Failed to retrieve client secret")
else:
    print("Client not found")

This code demonstrates how to retrieve the secret via the GET /{realm}/clients/{id}/client-secret endpoint. In practice, ensure the admin token is secure and handle potential errors.

Security Management and Best Practices

Client secrets are sensitive information and must be managed properly to prevent unauthorized access. Recommended measures include: regularly rotating secrets to reduce the risk of leakage, storing secrets securely using environment variables or key management services, and avoiding hardcoding in code. In Keycloak, secrets can be regenerated via the "Credentials" tab. Additionally, restrict access to secrets, allowing only necessary personnel or systems to view them. In programming, ensure all API calls use HTTPS and encrypt secret transmission. For example, in configuration files, store the secret as: CLIENT_SECRET = os.getenv("KEYCLOAK_CLIENT_SECRET"), rather than in plain text.

In summary, the existence of a Keycloak client secret depends on the client type configuration. By correctly setting the access type, users can easily retrieve secrets in the admin interface and automate management via APIs. Adhering to security best practices effectively protects secrets, ensuring overall system security.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.