Thursday, January 20, 2011

Redirect with AuthnRequest

In this post I will show you how to start the SAML Single sign-on process by sending a authentication request using OpenSAML.

The process starts with a redirect from the SP(The one wanting to authenticate someone) to the IdP(The one authenticating).

To start the authentication the SP sends a SAML AuthnRequest as parameter in the redirect.

The AuthnRequest message identifies the SP and can contain information about how the SP wants the user to be authenticated.

Here is an example how to send the AuthnRequest using OpenSAML.

The example uses the HTTPRedirectDeflateEncoder to encode, sign and redirect which makes everything way easier, especially considering encoding and signing.

//IPR Ergogroup AS
public void doAuthenticationRedirect(final HttpServletResponse response, final HttpSession currentSession, final String gotoURL, final SAMLMetaData metaData) throws IllegalArgumentException, SecurityException, IllegalAccessException {
  AuthnRequest authnRequest = generateAuthnRequest(metaData);


  // Save the request ID to session for future validation
  currentSession.setAttribute("AuthnRequestID", authnRequest.getID());
  currentSession.setAttribute("goto", gotoURL);

  HttpServletResponseAdapter responseAdapter = new HttpServletResponseAdapter(response, true);
  BasicSAMLMessageContext<SAMLObject, AuthnRequest, SAMLObject> context = new BasicSAMLMessageContext<SAMLObject, AuthnRequest, SAMLObject>();  

  HTTPRedirectDeflateEncoder encoder = new HTTPRedirectDeflateEncoder();

  try {
  } catch (MessageEncodingException e) {
   logger.error(e.getMessage(), e);

 private AuthnRequest generateAuthnRequest(final SAMLMetaData metaData) throws IllegalArgumentException, SecurityException, IllegalAccessException {

  AuthnRequest authnRequest = SAMLUtil.buildSAMLObjectWithDefaultName(AuthnRequest.class);

  authnRequest.setIssueInstant(new DateTime());
  for (SingleSignOnService sss : metaData.getIdpEntityDescriptor().getIDPSSODescriptor(SAMLConstants.SAML20P_NS).getSingleSignOnServices()) {
   if (sss.getBinding().equals(SAMLConstants.SAML2_REDIRECT_BINDING_URI)) {

  String deployURL = getDeployURL();
  if (deployURL.charAt(deployURL.length() - 1) == '/') {
   deployURL = deployURL.substring(0, deployURL.length() - 1);
  authnRequest.setAssertionConsumerServiceURL(deployURL + SAMLMetaData.CONSUMER_PATH);


  Issuer issuer = SAMLUtil.buildSAMLObjectWithDefaultName(Issuer.class);

  NameIDPolicy nameIDPolicy = SAMLUtil.buildSAMLObjectWithDefaultName(NameIDPolicy.class);


  RequestedAuthnContext requestedAuthnContext = SAMLUtil.buildSAMLObjectWithDefaultName(RequestedAuthnContext.class);

  AuthnContextClassRef authnContextClassRef = SAMLUtil.buildSAMLObjectWithDefaultName(AuthnContextClassRef.class);


  return authnRequest;

Resulting AuthnRequest



The signature is put in the URL with the request.

Further reading

My book, A Guide to OpenSAML, covers AuthnRequests and the rest of the SAML authentication process in detail. It also covers encryption, digital signatures, SAML in general and more.

A Guide to OpenSAML


  1. Interesting stuff but what is the SAMLUtil and SAMLMetaData classes? If they are yours can you post them?

  2. The SAMLMetaData is just a class I use to store my EntityDecriptor for the IDP. It also contains a constant pointing to the URL of my consumer servlet. The SAMLUtil is a collection of static utility methods. I will explain som convieniex methods in post later today


    1. Hi can u please share Samlutil class

    2. The methods are here

    3. Not all of them are there. Do you have a GitHub or something, where you place your code? Also, the code is all messed up around the generic types, your editor must have confused it with HTML tags

  4. What do you mean when you say signature is put in URL with the request?
    We have a SP initiated Web SSO and a spring mvc application on the server side,we create authrequest and set it in the request parameter as SAMLRequest. We then a do forward to a jsp page which has a form that submits the form to the IDP as the HTTP POST.My question is does HTTPRedirectDeflateEncoder allows to just get the encoded,signed authn request? My challenge has been to sign the authnrequest? Any inputs are highly appreciated.

    1. The HTTPRedirectDeflateEncoder sign deflates and redirects the request. I puts both the SAML request and the signature in the URL as URL parameters. Not sure what your asking. Do you want to sign is without redirecting it?

  5. Where do you specify the relaystate?

  6. How final redirect URI looks like?
    Can you provide more details about what does the code below does.

    HTTPRedirectDeflateEncoder encoder = new HTTPRedirectDeflateEncoder();

    1. It encodes the context as an URL. In this case it uses the PeerEntityEndpoint as host in the URL, uses DEFLATE to encode the OutboundSAMLMessage and puts it in a URL paramter, signs the samlmessage with OutboundSAMLMessageSigningCredential and puts it as a URL parameter.

  7. Hi,

    I tried to implement the same on a SAML Authn Reques but the encode method is returning NullPointerException on removeSignature method call. The stacktrace is as below

    I am not signing the authn request so how can I avoid calling this method context.setOutboundSAMLMessageSigningCredential(getSigningCredential());

    Thanks in advance

    [5/22/13 15:30:49:521 IST] 00000023 SystemErr R at org.opensaml.saml2.binding.encoding.HTTPRedirectDeflateEncoder.removeSignature(
    [5/22/13 15:30:49:521 IST] 00000023 SystemErr R at org.opensaml.saml2.binding.encoding.HTTPRedirectDeflateEncoder.doEncode(
    [5/22/13 15:30:49:521 IST] 00000023 SystemErr R at

    1. I dont understand, if you want to avoid to call context.setOutboundSAMLMessageSigningCredential(getSigningCredential()), just dont do it. Delete the line

    2. What is the source of getSigningCredential()? Does this come from the Identity Providers metadata file?

    3. This is a method I have written myself. It fetches the credential from somewhere. I think in the particular example it gets it from a JKS keystore.

      You can read more on how to fetch the credentails here

  8. Do you have full source code in github?

    1. I dont have anything there yet but I do have som tips on where to find examples here

    2. I also have on project on github that uses OpenSAML, maybe you can use that, but this is using the IDP functions of OpenSAML

  9. i tried implementing like the example here and in the book and i keep getting: The signing credential's algorithm URI could not be derived

    when trying to encode
    but when i look at the certificate file, i see that there is a signature algorithm

    any idea what is wrong here?

    1. Here is someone with the same problem
      Are you using the private key?

    2. Well i tried to add private key and this exception really disappeared.
      But this was with the credentials of my SP.
      The IDP does not know how to handle it. I'm using OKTA IDP for my testing so they gave me a metadata with the certificate, no private key in it. so i'm not sure how i should i handle this, i need to upload some how my SP certificate to the IDP? (a certificate that correspond to the private key i have)

    3. Yes this is what you have to do. Normaly you send the IDP your metadata

    4. The SAMLRequest is generated without a signature and sigalg parameters
      Do you know what can cause that?

    5. I need to see your code to help you with this. Please send it to my email

  10. Hi,

    one question - is it possible to sent a HTTPS Authentication Requst
    (Kerberos) from a Java Rich Client (no webapp) and then get the
    response Assetion using Open SAML. Basically I need the Java Rich
    Client to act as a SP and as Client in the same time?


    1. If your rich client is built to handle the redirects and present the Kerberos ticket to the IDP, I think you can do this. I have never done it though.

    2. This comment has been removed by the author.

    3. Thanks!
      Do you have any hint or know any example where I can see how to present the Kerberos ticket to the IDP?

    4. I dont, but this will not be handled by the OpenSAML lib. Probably you need some windows library on the client side.

    5. I got new Informations, my SAML requets should use:

      IsPassive="false", and

      is this possible in OpenSAML?

      Regardsing Kerberos ticket, do you know if this should be embedded in the SAML Authentication request, or the IDP reuest it separately from the SP?

    6. Yes that binding and setting passive is supported. I have not have time to read this document but I thing you will find alot of answers for your questions here.
      It is a defined standard profile for kerberos SAML authentication

  11. Both this post and your book used HttpServletResponseAdapter for servlet based transportation. If this is not the case, say using Apache Synapse, how should this Transport be constructed?

    1. Interesting question. I dont know as I have not worked with it, but I think you would use OpenSAML to construct the SAML message and then construct the redirect with signature using controls in the ESB

  12. how can i wait for the result of HTTPRedirectDeflateEncoder object.encode()?

    My problem is that i am in a web service and i don´t know how i will wait for the redirect result (the user authenticates in the IDP page) and after that how i catch the result?

    1. You can not wait for it per say. The two request are asynchronous. The best way to "wait for a response" is to set the RelayState parameter in the AuthnRequest. The same parameter and value should then be present when the result is delivered. You could also check the responses for the InResponseTo attribute in the response object, when it is the same as the ID in your AuthnRequest you know its your response.

      However using this approach in a webservice sound strange. Normally you dont have a user on the client side, but a machine that can not sign in to the IDP. The more common way to implement WS with SAML is to have the client receive a SAML assertion from a STS before calling the WS. The WS is then called with an already valid SAML assertion.