What a client secret is
In OAuth 2.0, a client is an application that asks an authorization server for tokens. A confidential client, such as a backend web app or a service, can keep a secret, and proves its identity with it when it calls the token endpoint. That secret is the client secret.
A client secret is a password for software. Unlike a person’s password, nobody has to remember it, so there is no reason for it to be anything but long and fully random.
This tool makes such secrets from your browser’s cryptographic random generator, in the encodings systems usually expect. The same values work as API keys, HMAC keys for webhooks and HS256 signing keys.
How strong a secret needs to be
Strength is measured in bits of randomness. A secret with 128 random bits has 2^128 possible values, far beyond what anyone can guess. 256 bits is the common choice because it also meets the key size HMAC-SHA256 needs.
| Random bytes | Bits | Hex length | Base64url length | Letters and digits length |
|---|---|---|---|---|
| 16 | 128 | 32 | 22 | 22 |
| 24 | 192 | 48 | 32 | 33 |
| 32 | 256 | 64 | 43 | 43 |
| 48 | 384 | 96 | 64 | 65 |
| 64 | 512 | 128 | 86 | 86 |
The encoding does not change the strength, only the length of the text. Hex uses two characters per byte; Base64url uses four characters for every three bytes. For letters and digits, the tool picks enough characters from the 62 available to reach at least the chosen number of bits.
Choosing a format
| Format | Characters | Good for |
|---|---|---|
| Base64url | A–Z a–z 0–9 - _ | Headers, URLs, environment variables; compact |
| Base64 | A–Z a–z 0–9 + / = | Systems that expect standard Base64; + and / need escaping in URLs |
| Hex | 0–9 a–f | Anything; easy to read and compare, but longest |
| Letters and digits | A–Z a–z 0–9 | Systems that reject or mangle symbols |
When in doubt, use Base64url or letters and digits. Both survive being pasted into shells, YAML, JSON and URLs without escaping.
How a client uses its secret
RFC 6749 defines two ways to send the secret to the token endpoint, and Keycloak supports both.
client_secret_basic
The client sends its id and secret in an HTTP Basic Authorization header. RFC 6749 says the id and secret are form-encoded before being joined with a colon and Base64-encoded, so a secret with symbols must be encoded first. Secrets without symbols avoid that trap entirely.
Authorization: Basic base64( urlencode(client_id) ":" urlencode(client_secret) )client_secret_post
The client sends client_id and client_secret as form fields in the body of the token request. It is simpler but puts the secret in request bodies, which some logging setups capture. Basic is the more common default.
Client secrets in Keycloak
In Keycloak, a client becomes confidential when Client authentication is turned on in its settings. Its Credentials tab then shows the client authenticator and, for Client Id and Secret, the current secret.
- Keycloak generates a secret when the client is created, and the Regenerate button on the Credentials tab makes a new one.
- To set a specific secret, for example one generated here and already stored in your secrets manager, set the client’s
secretfield through the admin REST API or in a realm import file. - Regenerating or replacing a secret takes effect at once, so update the application at the same moment, or expect token requests to fail until you do.
The Keycloak client JSON builder and the kcadm command builder help you write the client definition or the command that sets the secret.
What this tool does and does not do
- It makes one, three or five secrets of 128 to 512 bits.
- It encodes them as Base64url, Base64, hex, or letters and digits only.
- It uses
crypto.getRandomValues; letters and digits are picked with rejection sampling so none is favored. - It sends and stores nothing. A secret exists only on this page until you copy it.
- It does not register the secret anywhere. You set it in your authorization server and your application yourself.
How to generate and set a client secret
- Choose 256 bits unless a system needs something else.
- Choose Base64url, or letters and digits for systems that dislike symbols.
- Copy the secret straight into your secrets manager.
- Set it on the authorization server, for example through Keycloak’s admin API.
- Point the application at the secret in the secrets manager and test a token request.
Common mistakes
- Committing the secret to source control, even in a private repository.
- Putting a client secret in a single-page app or mobile app. Anything shipped to a device can be extracted; such apps are public clients and should use PKCE instead.
- Sharing one secret across environments, so a leak in development exposes production.
- Using a short or human-made secret.
- Logging the token request body or the Authorization header.
Rotating a client secret
Rotate when a secret may have been exposed, when someone with access leaves, and on a schedule your policy sets. Plan for the moment between changing the server and changing the app.
- Generate the new secret and store it in your secrets manager.
- Choose a low-traffic window, or run two clients side by side if downtime is not acceptable.
- Set the new secret on the authorization server.
- Restart or reload the application so it reads the new secret.
- Confirm token requests succeed, then delete the old secret from storage.
Storing and delivering the secret
A strong secret is only as safe as the places it is kept. Plan where it lives before you generate it.
- Keep the master copy in a secrets manager, such as a cloud secret store or a vault, with access limited to the people and services that need it.
- Deliver it to the application as an environment variable or a mounted file, injected at deploy time. In Kubernetes that usually means a Secret, ideally synced from the secrets manager rather than written by hand.
- Prefer mounted files for long-running services. Environment variables can leak into crash reports, debug pages and child processes.
- Never pass a secret as a command-line argument. Other users on the machine can often see process arguments.
- Keep secrets out of container images, CI logs and error messages. Mask them in CI output.