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 /** Relay state from authentication request. */
57 private String relayState;
59 /** Serialized authentication request. */
60 private String serialAuthnRequest;
62 /** Unmarshalled authentication request. */
63 private transient AuthnRequest authnRequest;
66 * Creates a new instance of Saml2LoginContext.
68 * @param relyingParty entity ID of the relying party
69 * @param state relay state from incoming authentication request
70 * @param request SAML 2.0 Authentication Request
72 * @throws MarshallingException thrown if the given request can not be marshalled and serialized into a string
74 public Saml2LoginContext(String relyingParty, String state, AuthnRequest request) throws MarshallingException {
77 if (relyingParty == null || request == null) {
78 throw new IllegalArgumentException("SAML 2 authentication request and relying party ID may not be null");
80 setRelyingParty(relyingParty);
82 authnRequest = request;
83 serialAuthnRequest = serializeRequest(request);
85 setForceAuth(authnRequest.isForceAuthn());
86 setPassiveAuth(authnRequest.isPassive());
87 getRequestedAuthenticationMethods().addAll(extractRequestedAuthenticationMethods());
91 * Gets the authentication request that started the login process.
93 * @return authentication request that started the login process
95 * @throws UnmarshallingException thrown if the serialized form on the authentication request can be unmarshalled
97 public AuthnRequest getAuthenticationRequest() throws UnmarshallingException {
98 if (authnRequest == null) {
99 authnRequest = deserializeRequest(serialAuthnRequest);
106 * Gets the relay state from the orginating authentication request.
108 * @return relay state from the orginating authentication request
110 public String getRelayState(){
115 * Gets the requested authentication context information from the authentication request.
117 * @return requested authentication context information or null
119 public RequestedAuthnContext getRequestedAuthenticationContext() {
121 AuthnRequest request = getAuthenticationRequest();
122 return request.getRequestedAuthnContext();
123 } catch (UnmarshallingException e) {
129 * Serializes an authentication request into a string.
131 * @param request the request to serialize
133 * @return the serialized form of the string
135 * @throws MarshallingException thrown if the request can not be marshalled and serialized
137 protected String serializeRequest(AuthnRequest request) throws MarshallingException {
138 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(request);
139 Element requestElem = marshaller.marshall(request);
140 StringWriter writer = new StringWriter();
141 XMLHelper.writeNode(requestElem, writer);
142 return writer.toString();
146 * Deserailizes an authentication request from a string.
148 * @param request request to deserialize
150 * @return the request XMLObject
152 * @throws UnmarshallingException thrown if the request can no be deserialized and unmarshalled
154 protected AuthnRequest deserializeRequest(String request) throws UnmarshallingException {
155 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
157 DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
158 InputSource requestInput = new InputSource(new StringReader(request));
159 Element requestElem = docBuilder.parse(requestInput).getDocumentElement();
160 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(requestElem);
161 return (AuthnRequest) unmarshaller.unmarshall(requestElem);
162 } catch (Exception e) {
163 throw new UnmarshallingException("Unable to read serialized authentication request");
168 * Extracts the authentication methods requested within the request.
170 * @return requested authentication methods
172 protected List<String> extractRequestedAuthenticationMethods(){
173 ArrayList<String> requestedMethods = new ArrayList<String>();
175 RequestedAuthnContext authnContext = getRequestedAuthenticationContext();
176 if (authnContext == null) {
177 return requestedMethods;
180 // For the immediate future, we only support the "exact" comparator.
181 AuthnContextComparisonTypeEnumeration comparator = authnContext.getComparison();
182 if (comparator != null && comparator != AuthnContextComparisonTypeEnumeration.EXACT) {
183 log.error("Unsupported comparision operator ( " + comparator
184 + ") in RequestedAuthnContext. Only exact comparisions are supported.");
185 return requestedMethods;
188 // build a list of all requested authn classes and declrefs
189 List<AuthnContextClassRef> authnClasses = authnContext.getAuthnContextClassRefs();
190 List<AuthnContextDeclRef> authnDeclRefs = authnContext.getAuthnContextDeclRefs();
192 if (authnClasses != null) {
193 for (AuthnContextClassRef classRef : authnClasses) {
194 if (classRef != null) {
195 requestedMethods.add(classRef.getAuthnContextClassRef());
200 if (authnDeclRefs != null) {
201 for (AuthnContextDeclRef declRef : authnDeclRefs) {
202 if (declRef != null) {
203 requestedMethods.add(declRef.getAuthnContextDeclRef());
208 return requestedMethods;