Skip to content

SAML encoder

Encode SAML XML the way each binding needs it: raw DEFLATE and Base64 for the redirect binding, plain Base64 for the POST binding.

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

SAML XML

A sample AuthnRequest to a sample Keycloak realm is filled in. The XML is encoded exactly as typed, whitespace included.

Encoded

Base64
 
URL-encoded, for the query string
 
Query parameter
 
Redirect binding: raw DEFLATE (no zlib header), then Base64, then URL-encoding. Use the redirect URL builder to add a RelayState and the identity provider’s address.

How to use it

  1. Paste the message XML, such as an AuthnRequest or a LogoutRequest.
  2. Pick HTTP-Redirect or HTTP-POST.
  3. Copy the Base64 value, or for the redirect binding the URL-encoded query parameter.

What to know

The redirect binding compresses the XML with raw DEFLATE (RFC 1951, with no zlib header), then Base64-encodes it and URL-encodes the result. Using zlib or gzip output instead is a common mistake, and identity providers reject it.

The POST binding sends the XML Base64-encoded without compression, in a hidden form field named SAMLRequest or SAMLResponse.

The XML is encoded exactly as you typed it. If the message is signed with an enveloped XML signature, any change to it, whitespace included, breaks the signature.

Why SAML messages are encoded

SAML messages are XML, and XML does not fit in a URL or a form field as it is. The SAML 2.0 bindings specification defines how to wrap the same XML for each way it travels. For browser sign-in, two bindings matter: HTTP-Redirect and HTTP-POST.

Each binding has one exact encoding. An identity provider that expects one will not read the other, and small mistakes, such as the wrong kind of compression, make the whole message unreadable. This tool produces both encodings from the XML you paste.

The redirect binding encoding

The HTTP-Redirect binding uses the DEFLATE encoding defined in section 3.4.4.1 of the bindings specification. It has three steps.

  1. Compress the XML with raw DEFLATE, as in RFC 1951. Raw means no zlib header and no checksum.
  2. Base64-encode the compressed bytes.
  3. URL-encode the Base64 text and put it in the query string as SAMLRequest or SAMLResponse.
The same steps in a shell, for comparison
# Python's zlib with wbits=-15 writes raw DEFLATE
python3 -c 'import sys,zlib,base64,urllib.parse
c = zlib.compressobj(9, zlib.DEFLATED, -15)
raw = c.compress(sys.stdin.buffer.read()) + c.flush()
print(urllib.parse.quote(base64.b64encode(raw), safe=""))' < request.xml

Compression keeps the URL short. A typical AuthnRequest shrinks to a few hundred characters, well within what browsers and servers accept.

Raw DEFLATE versus zlib and gzip

zlib output starts with a two-byte header and ends with a checksum. gzip adds a longer header. Both wrap the same DEFLATE data, but the redirect binding wants the data without any wrapper. In many languages, raw DEFLATE is a separate function, such as deflateRaw, or a negative window-bits value. If an identity provider says it cannot inflate your request, this is the first thing to check.

The POST binding encoding

The HTTP-POST binding is simpler: Base64-encode the XML, without compression, and put it in a hidden form field named SAMLRequest or SAMLResponse. The page that holds the form submits it to the other side, usually with a small script, and offers a button for browsers without scripts.

A POST binding form (sample)
<form method="post" action="https://auth.example.com/realms/acme/protocol/saml">
  <input type="hidden" name="SAMLRequest" value="PHNhbWxwOkF1dGhuUmVxdWVzdC…"/>
  <input type="hidden" name="RelayState" value="/dashboard"/>
  <button type="submit">Continue</button>
</form>

POST has no practical size limit, so it is the binding for SAML responses in browser sign-in and for any message that carries an XML signature inside it.

What this tool does

Paste the XML, pick the binding, and copy the result. For the redirect binding you get three outputs: the Base64 value, the URL-encoded value, and a ready query parameter. For POST you get the Base64 value for the form field.

The tool parses the XML first and stops if it is not well formed. It picks the parameter name from the root element: a message whose name ends in Response goes in SAMLResponse, everything else in SAMLRequest. It warns when the root is not in the SAML protocol namespace, and when you try to send an XML-signed message with the redirect binding.

What it does not do

It does not sign anything. It does not change the XML: whitespace, attribute order and the declaration are encoded exactly as typed. It sends nothing over the network; compression runs on the browser’s own CompressionStream.

How to use it

  1. Paste the message XML, or start from the sample AuthnRequest.
  2. Edit the issuer, destination and ACS URL to match your setup.
  3. Pick HTTP-Redirect or HTTP-POST.
  4. Copy the output you need.
  5. To build a full sign-in URL with a RelayState, use the SAML redirect URL builder.

When an encoder is useful

Testing an identity provider by hand

When you set up a new SAML client, you can test it before the app is ready. Write a minimal AuthnRequest with the client’s entity id as the issuer, encode it for the redirect binding, and open the resulting URL. If the identity provider shows its sign-in page, it accepted the request.

Reproducing a bug

Decode a failing request with the SAML request decoder, change one attribute, encode it again, and send it. This shows which part of the request causes the error without changing the app.

Checking your own code

If you write SAML code yourself, compare its output with this tool’s output for the same XML. Different compression settings can give different bytes that still inflate to the same XML, so compare by decoding both rather than comparing the encoded text.

Signing and the two bindings

The bindings sign messages differently, and mixing them up is a common error.

  • With HTTP-POST, the signature is a ds:Signature element inside the XML, an enveloped XML signature over the message.
  • With HTTP-Redirect, the XML signature must be removed before encoding. Instead, the sender signs the string SAMLRequest=…&RelayState=…&SigAlg=… and adds the result as a Signature query parameter.

Because the redirect signature covers the URL-encoded values, the receiver must check it against the values exactly as they arrived. Re-encoding them can change the bytes and break the check.

Using it with Keycloak

A Keycloak realm accepts both bindings at https://<host>/realms/<realm>/protocol/saml. The request’s Issuer must match the client id of a SAML client in the realm.

If the client has Client signature required on, an unsigned request from this tool is refused. For a manual test you can turn the setting off on a test client, or keep it on and sign the request with the app’s key in your own tooling. Do not turn it off on a production client.

Keycloak also has a client setting, Force POST binding, which makes it answer with POST even when the request came with the redirect binding. That affects the response, not the request you encode here.

Common mistakes

  • Using zlib or gzip output instead of raw DEFLATE.
  • Forgetting to URL-encode, so + turns into a space and = confuses the query parser.
  • Compressing a message for the POST binding. POST values are plain Base64.
  • Sending a message with an enveloped ds:Signature through the redirect binding.
  • Pretty-printing a signed message before encoding it, which changes the signed bytes.

Security notes

Encoding is not encryption. Anyone who sees the URL or the form can decode the message. Do not put secrets in a SAML request. If a message must be protected, the protocol offers XML encryption for assertions and NameIDs, not for the request as a whole.

Raw DEFLATE in common languages

Most languages have raw DEFLATE built in, but it is rarely the default function. These are the calls that produce what the redirect binding expects:

LanguageRaw DEFLATENot this (adds a wrapper)
Node.jszlib.deflateRawSynczlib.deflateSync, zlib.gzipSync
Browser JavaScriptnew CompressionStream('deflate-raw')'deflate' or 'gzip'
Pythonzlib.compressobj(wbits=-15)zlib.compress
Javanew Deflater(level, true) (nowrap)new Deflater()
.NETDeflateStreamZLibStream, GZipStream
Gocompress/flatecompress/zlib, compress/gzip
PHPgzdeflategzcompress, gzencode

For decoding, use the matching inflate function: inflateRaw, negative window bits, Inflater(true) and so on. Mixing a raw compressor with a zlib decompressor, or the other way round, fails with a header or data error.

Checking the output

The surest test of an encoder is a round trip. Paste the encoded value into the SAML request decoder. It should report the binding you chose and show XML identical to what you started with.

If you compare two encoders by their output text instead, expect differences. DEFLATE allows many valid compressed forms of the same input, and compression levels change the bytes. Two different Base64 strings can inflate to exactly the same XML. Compare the decoded XML, not the encoded text.

Questions

Frequently asked questions

How do I encode a SAMLRequest for the redirect binding?

Compress the XML with raw DEFLATE, Base64-encode the result, then URL-encode it. Put it in the query string as SAMLRequest.

Is SAML deflate the same as zlib compression?

No. The redirect binding uses raw DEFLATE with no zlib header or checksum. zlib and gzip output will not inflate on the other side.

Do I need to compress a SAML message for the POST binding?

No. The POST binding uses plain Base64 of the XML. Only the redirect binding compresses.

Why does my encoded SAMLRequest fail in the URL?

Base64 uses +, / and =, which have meaning in URLs. Without URL-encoding, + can become a space and the value breaks. Use the URL-encoded output.

Can I sign the message with this tool?

No. Signing needs a private key, and the page does not ask for one. For POST the signature goes inside the XML; for the redirect binding it is a separate Signature parameter.

Why does the redirect binding not allow an XML signature?

The binding defines its own signature over the query string, which is shorter and does not need XML canonicalization. An enveloped signature must be removed before the message is deflated.

How long can a redirect binding URL be?

SAML sets no limit, but browsers, proxies and servers do. Compression keeps typical requests short. Large messages, such as responses, should use POST.

How do I do raw DEFLATE in Java or .NET?

In Java, create the Deflater with nowrap set to true, as in new Deflater(level, true). In .NET, DeflateStream writes raw DEFLATE; ZLibStream and GZipStream add wrappers.

How do I check that my encoded SAML message is correct?

Decode it again, for example with the SAML request decoder. It should report the binding you used and show the same XML you started with.

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.