In a single sign on system there are basically two roles; Service Providers and Identity Providers (IP). The important characteristic of a single sign on system is the pre-defined trust relation between the service providers and the identity providers Service providers trust the assertions issued by the identity providers and the identity providers issue assertions based on the results of authentication and authorization of principles which access services at service providers.
The following are some of the advantages you can have with SSO:
- Users need only a single username/password pair to access multiple services. Thus they do not have the issue of remembering multiple username/password pairs.
- Users are authenticated only once at the identity provider and then they are automatically logged into all services within that "trust-domain". This process is more convenient to users since they do not have to provide their username/password at every service provider.
- Service providers do not have the overhead of managing user identities, which is more convenient for them.
- User identities are managed at a central point. This is more secure, less complex and easily manageable.
With the release of WSO2 Identity Server 3.0, it supports the SAML 2.0 web browser based SSO profile. WSO2 Identity Server can act as the identity provider of a single sign on system with minimal configurations. This section provides information on how to configure the identity server and how your applications can be deployed in a SAML 2.0 web browser based SSO system.
The following sections expand on SAML2 based SSO:
Single Sign On In Reality
Single Sign On is widely used in web technologies. Google is one of the best examples.
Try this simple exercise,
- Visit www.google.com from your web browser.
- Click on the SIGN IN button on the top right of the page.
- Once you sign in, you are redirected to www.google.com/accounts/ServiceLogin. There you are requested to enter your Username and Password. Enter your Google credentials there.
- Once you enter your Username and Password, you are directed back to www.google.com where you started.
- Now visit www.igoogle.com, the Google web portal.
- Notice that you are automatically signed in to the portal. You did not have to enter your Username and Password there.
- Next visit www.gmail.com, the Google mail server.
- Once again you are automatically signed in and you directly access your Gmail Inbox. You did not have to enter your Username and Password at Gmail.
- In addition to that; now try www.youtube.com.
- Click on the “Sign In” button on the top right of the YouTube home page.
You are automatically signed in. You do not have to enter your username and password at YouTube.
Tip: Notice the URL of the web browser. Each time you access an application, you see that you are being redirected to www.google.com/accounts/ServiceLogin and return immediately back to the website.
Single Sign On (SSO) allows you to sign in only once but provides access to multiple resources without having to re-enter your username and password.
SAML 2.0 Web Browser Based SSO Profile
SAML 2.0 Web Browser based SSO profile is defined under the SAML 2.0 Profiles specification. SAML 2.0 provides five main specifications:
- Core
- Binding
- Profile
- Metadata
- Conformance
In a web browser based SSO system, the flow can be started by the user either by attempting to access a service at the service provider or by directly accessing the identity provider itself.
If the user accesses a service at a service provider:
- The service provider determines which identity provider to use (this is the case when there are multiple identity providers. SAML identity provider discovery profile may be used).
- The service provider generates a SAML message and then redirects the web browser to the identity provider along with the message.
- Identity provider authenticates the user.
- The identity provider generates a SAML message and then redirects the web browser back to the service provider.
- The service provider processes the SAML message and decides to grant or deny access to the user.
If the user accesses the identity provider directly, then only the steps 3, 4 and 5 are in the flow.
The message MUST contain an element which uniquely identifies the service provider who created the message. Optionally the message may contain elements such as , etc. More information regarding the message can be found in SAML Core Specification.
The message MUST contain , , , , elements. The message MUST be integrity protected. More information regarding the message can be found in SAML Core Specification.
The following diagram illustrates the scenario:
SAML 2.0 SSO Assertion Consumers
Service providers act as SAML assertion consumers. They have two basic functions:
- Create messages and redirect users to the identity provider with the created message.
- Process messages from the identity provider and make decisions based on them.
The following code is a sketch of a sample service provider servlet in a SAML 2.0 Web-Browser based SSO system.
public class Resource extends HttpServlet { private static SamlConsumer consumer = new SamlConsumer(); public void doGet(HttpServletRequest request, HttpServletResponse response) { requestMessage = consumer.buildRequestMessage(); response.sendRedirect(requestMessage); } public void doPost(HttpServletRequest request, HttpServletResponse response) { responseMessage = request.getParameter("SAMLResponse").toString(); result = consumer.processResponseMessage(responseMessage); } }
When a web user attempts to access the above servlet, its doGet() method is called. Inside the doGet() method, it generates an message and then redirects the user to the Identity Provider.
After authentication is completed by the Identity Provider, it does a POST call back to the above servlet with a message. Then the doPost() method of the servlet gets called and inside the doPost() method, it retrieves the message from the request and then the message is passed to the SamlConsumer instance for processing.
The complete source code can be checked out here.
<AuthnRequest> Message
To create an <AuthnRequest> message using the OpenSAML library:
- Add the OpenSAML library to the build path of the project. You can download the open SAML JAR file from here.
- A sample <AuthnRequest> message can be found here.
According to SAML 2.0 specifications, the message must contain an element. Create the Issuer element first.
// the issuerUrl is the url of the service provider who generates the message String issuerUrl = "http://localhost:8080/saml2.demo/consumer"; IssuerBuilder issuerBuilder = new IssuerBuilder(); Issuer issuer = issuerBuilder.buildObject("urn:oasis:names:tc:SAML:2.0:assertion", "Issuer", "samlp"); issuer.setValue(issuerUrl);
Create the <AuthnRequest> next.
DateTime issueInstant = new DateTime(); AuthnRequestBuilder authnRequestBuilder = new AuthnRequestBuilder(); AuthnRequest authnRequest = authnRequestBuilder.buildObject("urn:oasis:names:tc:SAML:2.0:protocol", "AuthnRequest", "samlp"); authnRequest.setForceAuthn(new Boolean(false)); authnRequest.setIsPassive(new Boolean(false)); authnRequest.setIssueInstant(issueInstant); authnRequest.setProtocolBinding("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"); authnRequest.setAssertionConsumerServiceURL(issuerUrl); authnRequest.setIssuer(issuer); authnRequest.setID(aRandomId); authnRequest.setVersion(SAMLVersion.VERSION_20);
The message may contain many other elements like , etc. those elements can be created and added to the message in the same way.Next encode the message.
Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(authnRequest); Element authDOM = marshaller.marshall(authnRequest); StringWriter rspWrt = new StringWriter(); XMLHelper.writeNode(authDOM, rspWrt); String requestMessage = rspWrt.toString(); Deflater deflater = new Deflater(Deflater.DEFLATED, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater); deflaterOutputStream.write(requestMessage.getBytes()); deflaterOutputStream.close(); /* Encoding the compressed message */ String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES); String encodedAuthnRequest = URLEncoder.encode(encodedRequestMessage,"UTF-8").trim();
Construct the redirection URL.
redirectionUrl = identitypProviderUrl+ "?SAMLRequest=" + encodedRequestMessage;
Redirect the user to the identity provider.
response.sendRedirect(redirectionUrl);
<Response> Message
To read the <Response> message issued by the WSO2 Identity Server:
- A sample <Response> message can be found here.
The response message must be fetched from the request.
responseMessage = request.getParameter("SAMLResponse").toString();
The fetched “responseMessage” is unmarshaled and the SAML message is retrieved.
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(authnReqStr.trim().getBytes())); Element element = document.getDocumentElement(); UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); Response response = (Response) unmarshaller.unmarshall(element);
The retrieved SAML 2.0 Response message can be easily processed. For example, lets takes the User Name or the Subject's Name Id.
String subject = response.getAssertions().get(0).getSubject() .getNameID().getValue();
Alternatively, you can retrieve the certificate.
String certificate = response.getSignature().getKeyInfo().getX509Datas().get(0).getX509Certificates().get(0).getValue();
Likewise the message from the WSO2 Identity Server can be read easily.
Configuring the SAML 2.0 SSO Demonstration
To configure the SAML 2.0 SSO demonstration:
- Download the SAML2 SSO sample service provider .war file from here.
- Extract the saml2-demo.war file to your Apache-Tomcat server's webapps folder.
- Open the web.xml file which can be found in the path
[tomcat-home]/webapps/saml2.demo/WEB-INF/web.xml
.
- Given configurations are valid for default configurations of the WSO2 Identity Server and Apache Tomcat Server
- Issuer: This is a unique name given for this Service Provider
- IdpUrl: The URL of the WSO2 Identity Server SAML SSO Provider. This has the pattern:
https://{is-host-name}:{is-https-port}/samlsso.
- ConsumerUrl: The URL of the SAML2ConsumerServlet of this webapp. This has the pattern:
http://{tomcat-host-name}:{tomcat-http-port}/saml2.demo/consumer
.
Configuring the WSO2 Identity Server as a SAML 2.0 SSO Identity Provider
To configure the WSO2 Identity Server as a SAML 2.0 SSO Identity Provider:
- Start the WSO2 Identity Server and sign in as an admin.
- Go to the SAML SSO page which is under the Manage menu in the left pane.
- Make the following configuration changes. Use exactly the same values used to configure the webapp.
- Issuer: saml2.demo
- Assertion Consumer URL: http://localhost:8080/saml2.demo/consumer
WSO2 Identity Server SSO Feature Demonstration
- Start Apache-tomcat and visit http://localhost:8080/saml2.demo/.
- Click on Sign In. You are redirected to the WSO2 Identity Server. Enter the Username and Password.
- After successful authentication you are logged into the service provider.