Skip to content

OAuth client secret generator

Generate a strong random client secret or API key in Base64url, Base64, hex or letters and digits.

Runs in your browser. Nothing you paste is sent to us.

Settings

256 bits is far beyond guessing. Use at least the hash size when the secret is an HMAC key (HS256 needs 256 bits).

Client secret

256.0 bits of entropy 0 characters

Made from your browser’s cryptographic random generator. Store it in a secret manager, not in source control, and rotate it if it is ever exposed.

How to use it

  1. Choose the strength. 256 bits is the recommended size.
  2. Choose the format. Base64url and letters and digits are safe in URLs and headers.
  3. Copy the secret.
  4. Store it in a secret manager and set it on both sides.

What to know

A client secret is how a confidential OAuth client proves who it is to the authorization server. It should be random, long and known only to the client and the server.

Keycloak generates a secret for a confidential client on its Credentials tab and can regenerate it there. To set a secret of your own, use the admin REST API or a realm import.

Where you can, prefer client authentication that needs no shared secret, such as a signed JWT with a private key (private_key_jwt) or mutual TLS.

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 bytesBitsHex lengthBase64url lengthLetters and digits length
16128322222
24192483233
32256644343
48384966465
645121288686

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

FormatCharactersGood for
Base64urlA–Z a–z 0–9 - _Headers, URLs, environment variables; compact
Base64A–Z a–z 0–9 + / =Systems that expect standard Base64; + and / need escaping in URLs
Hex0–9 a–fAnything; easy to read and compare, but longest
Letters and digitsA–Z a–z 0–9Systems 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.

Basic client authentication
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 secret field 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

  1. Choose 256 bits unless a system needs something else.
  2. Choose Base64url, or letters and digits for systems that dislike symbols.
  3. Copy the secret straight into your secrets manager.
  4. Set it on the authorization server, for example through Keycloak’s admin API.
  5. Point the application at the secret in the secrets manager and test a token request.

Better than a secret: options without shared secrets

A shared secret has a weakness: it exists in two places and travels with every token request. Where you can, use client authentication that keeps the secret on one side.

  • Signed JWT with a private key (private_key_jwt, RFC 7523): the client signs a short-lived assertion, and the server checks it with the client’s public key. Keycloak calls this Signed Jwt.
  • Mutual TLS (RFC 8705): the client proves itself with a TLS client certificate. Keycloak calls this X509 Certificate.
  • Signed JWT with the client secret (client_secret_jwt): the secret signs an assertion instead of being sent. Better than sending it, but still a shared secret.

The OAuth 2.0 Security Best Current Practice, RFC 9700, recommends asymmetric methods such as these for client authentication where possible.

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.

  1. Generate the new secret and store it in your secrets manager.
  2. Choose a low-traffic window, or run two clients side by side if downtime is not acceptable.
  3. Set the new secret on the authorization server.
  4. Restart or reload the application so it reads the new secret.
  5. 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.
Questions

Frequently asked questions

How long should an OAuth client secret be?

At least 128 random bits; 256 bits is the common choice. As Base64url, 32 random bytes is 43 characters.

What characters should a client secret use?

Base64url or letters and digits are safest, since they need no escaping in headers, URLs or config files. Standard Base64 uses + and /, which must be encoded in some contexts.

How do I change a client secret in Keycloak?

Open the client’s Credentials tab and press Regenerate for a new random secret. To set a particular value, update the client’s secret field through the admin REST API or a realm import.

Is a client secret the same as an API key?

They are similar: both are random values that prove a caller’s identity. A client secret is used with an OAuth authorization server to get tokens; an API key is usually sent directly to an API.

Can a single-page app use a client secret?

No. Anything in browser code can be read by users. Single-page and mobile apps are public clients and should use the authorization code flow with PKCE and no secret.

Can I use this secret as a JWT signing key?

Yes, for HS256 the default 256 bits meets RFC 7518’s minimum. For HS512, choose 512 bits.

How often should client secrets be rotated?

Whenever exposure is possible, when people with access leave, and on whatever schedule your security policy sets. Make rotation a tested routine rather than an emergency.

What is better than a client secret?

Asymmetric client authentication: a JWT signed with the client’s private key, or a TLS client certificate. The server then holds only a public key, so nothing secret travels with each request.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.