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.
- Compress the XML with raw DEFLATE, as in RFC 1951. Raw means no zlib header and no checksum.
- Base64-encode the compressed bytes.
- URL-encode the Base64 text and put it in the query string as
SAMLRequestorSAMLResponse.
# 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.xmlCompression 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.
<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
- Paste the message XML, or start from the sample
AuthnRequest. - Edit the issuer, destination and ACS URL to match your setup.
- Pick HTTP-Redirect or HTTP-POST.
- Copy the output you need.
- 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:Signatureelement 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 aSignaturequery 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:Signaturethrough 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:
| Language | Raw DEFLATE | Not this (adds a wrapper) |
|---|---|---|
| Node.js | zlib.deflateRawSync | zlib.deflateSync, zlib.gzipSync |
| Browser JavaScript | new CompressionStream('deflate-raw') | 'deflate' or 'gzip' |
| Python | zlib.compressobj(wbits=-15) | zlib.compress |
| Java | new Deflater(level, true) (nowrap) | new Deflater() |
| .NET | DeflateStream | ZLibStream, GZipStream |
| Go | compress/flate | compress/zlib, compress/gzip |
| PHP | gzdeflate | gzcompress, 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.