2 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.idp.authn;
19 import java.io.Serializable;
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.util.ArrayList;
23 import java.util.List;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
28 import org.apache.log4j.Logger;
29 import org.opensaml.Configuration;
30 import org.opensaml.saml2.core.AuthnContextClassRef;
31 import org.opensaml.saml2.core.AuthnContextComparisonTypeEnumeration;
32 import org.opensaml.saml2.core.AuthnContextDeclRef;
33 import org.opensaml.saml2.core.AuthnRequest;
34 import org.opensaml.saml2.core.RequestedAuthnContext;
35 import org.opensaml.xml.io.Marshaller;
36 import org.opensaml.xml.io.MarshallingException;
37 import org.opensaml.xml.io.Unmarshaller;
38 import org.opensaml.xml.io.UnmarshallingException;
39 import org.opensaml.xml.util.XMLHelper;
40 import org.w3c.dom.Element;
41 import org.xml.sax.InputSource;
44 * A SAML 2.0 {@link LoginContext}.
46 * This class can interpret {@link RequestedAuthnContext} and act accordingly.
48 public class Saml2LoginContext extends LoginContext implements Serializable {
50 /** Serial version UID. */
51 private static final long serialVersionUID = -2518779446947534977L;
54 private final Logger log = Logger.getLogger(Saml2LoginContext.class);
56 /** Serialized authentication request. */
57 private String serialAuthnRequest;
59 /** Unmarshalled authentication request. */
60 private transient AuthnRequest authnRequest;
63 * Creates a new instance of Saml2LoginContext.
65 * @param relyingParty entity ID of the relying party
66 * @param request SAML 2.0 Authentication Request
68 * @throws MarshallingException thrown if the given request can not be marshalled and serialized into a string
70 public Saml2LoginContext(String relyingParty, AuthnRequest request) throws MarshallingException {
71 if (relyingParty == null || request == null) {
72 throw new IllegalArgumentException("SAML 2 authentication request and relying party ID may not be null");
75 serialAuthnRequest = serializeRequest(request);
76 authnRequest = request;
77 setForceAuth(authnRequest.isForceAuthn());
78 setPassiveAuth(authnRequest.isPassive());
79 setRelyingParty(relyingParty);
83 * Gets the authentication request that started the login process.
85 * @return authentication request that started the login process
87 * @throws UnmarshallingException thrown if the serialized form on the authentication request can be unmarshalled
89 public AuthnRequest getAuthenticationRequest() throws UnmarshallingException {
90 if (authnRequest == null) {
91 authnRequest = deserializeRequest(serialAuthnRequest);
98 * Gets the requested authentication context information from the authentication request.
100 * @return requested authentication context information or null
102 public RequestedAuthnContext getRequestedAuthenticationContext() {
104 AuthnRequest request = getAuthenticationRequest();
105 return request.getRequestedAuthnContext();
106 } catch (UnmarshallingException e) {
112 * This method evaluates a SAML2 {@link RequestedAuthnContext} and returns the list of requested authentication
115 * If the AuthnQuery did not contain a RequestedAuthnContext, this method will return <code>null</code>.
117 * @return An array of authentication method URIs, or <code>null</code>.
119 public List<String> getRequestedAuthenticationMethods() {
120 ArrayList<String> requestedMethods = new ArrayList<String>();
122 RequestedAuthnContext authnContext = getRequestedAuthenticationContext();
123 if (authnContext == null) {
124 return requestedMethods;
127 // For the immediate future, we only support the "exact" comparator.
128 AuthnContextComparisonTypeEnumeration comparator = authnContext.getComparison();
129 if (comparator != null && comparator != AuthnContextComparisonTypeEnumeration.EXACT) {
130 log.error("Unsupported comparision operator ( " + comparator
131 + ") in RequestedAuthnContext. Only exact comparisions are supported.");
135 // build a list of all requested authn classes and declrefs
136 List<AuthnContextClassRef> authnClasses = authnContext.getAuthnContextClassRefs();
137 List<AuthnContextDeclRef> authnDeclRefs = authnContext.getAuthnContextDeclRefs();
139 if (authnClasses != null) {
140 for (AuthnContextClassRef classRef : authnClasses) {
141 if (classRef != null) {
142 requestedMethods.add(classRef.getAuthnContextClassRef());
147 if (authnDeclRefs != null) {
148 for (AuthnContextDeclRef declRef : authnDeclRefs) {
149 if (declRef != null) {
150 requestedMethods.add(declRef.getAuthnContextDeclRef());
155 return requestedMethods;
159 * Serializes an authentication request into a string.
161 * @param request the request to serialize
163 * @return the serialized form of the string
165 * @throws MarshallingException thrown if the request can not be marshalled and serialized
167 protected String serializeRequest(AuthnRequest request) throws MarshallingException {
168 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(request);
169 Element requestElem = marshaller.marshall(request);
170 StringWriter writer = new StringWriter();
171 XMLHelper.writeNode(requestElem, writer);
172 return writer.toString();
176 * Deserailizes an authentication request from a string.
178 * @param request request to deserialize
180 * @return the request XMLObject
182 * @throws UnmarshallingException thrown if the request can no be deserialized and unmarshalled
184 protected AuthnRequest deserializeRequest(String request) throws UnmarshallingException {
185 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
187 DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
188 InputSource requestInput = new InputSource(new StringReader(request));
189 Element requestElem = docBuilder.parse(requestInput).getDocumentElement();
190 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(requestElem);
191 return (AuthnRequest) unmarshaller.unmarshall(requestElem);
192 } catch (Exception e) {
193 throw new UnmarshallingException("Unable to read serialized authentication request");