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 {
73 if (relyingParty == null || request == null) {
74 throw new IllegalArgumentException("SAML 2 authentication request and relying party ID may not be null");
76 setRelyingParty(relyingParty);
77 authnRequest = request;
78 serialAuthnRequest = serializeRequest(request);
80 setForceAuth(authnRequest.isForceAuthn());
81 setPassiveAuth(authnRequest.isPassive());
82 getRequestedAuthenticationMethods().addAll(extractRequestedAuthenticationMethods());
86 * Gets the authentication request that started the login process.
88 * @return authentication request that started the login process
90 * @throws UnmarshallingException thrown if the serialized form on the authentication request can be unmarshalled
92 public AuthnRequest getAuthenticationRequest() throws UnmarshallingException {
93 if (authnRequest == null) {
94 authnRequest = deserializeRequest(serialAuthnRequest);
101 * Gets the requested authentication context information from the authentication request.
103 * @return requested authentication context information or null
105 public RequestedAuthnContext getRequestedAuthenticationContext() {
107 AuthnRequest request = getAuthenticationRequest();
108 return request.getRequestedAuthnContext();
109 } catch (UnmarshallingException e) {
115 * Serializes an authentication request into a string.
117 * @param request the request to serialize
119 * @return the serialized form of the string
121 * @throws MarshallingException thrown if the request can not be marshalled and serialized
123 protected String serializeRequest(AuthnRequest request) throws MarshallingException {
124 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(request);
125 Element requestElem = marshaller.marshall(request);
126 StringWriter writer = new StringWriter();
127 XMLHelper.writeNode(requestElem, writer);
128 return writer.toString();
132 * Deserailizes an authentication request from a string.
134 * @param request request to deserialize
136 * @return the request XMLObject
138 * @throws UnmarshallingException thrown if the request can no be deserialized and unmarshalled
140 protected AuthnRequest deserializeRequest(String request) throws UnmarshallingException {
141 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
143 DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
144 InputSource requestInput = new InputSource(new StringReader(request));
145 Element requestElem = docBuilder.parse(requestInput).getDocumentElement();
146 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(requestElem);
147 return (AuthnRequest) unmarshaller.unmarshall(requestElem);
148 } catch (Exception e) {
149 throw new UnmarshallingException("Unable to read serialized authentication request");
154 * Extracts the authentication methods requested within the request.
156 * @return requested authentication methods
158 protected List<String> extractRequestedAuthenticationMethods(){
159 ArrayList<String> requestedMethods = new ArrayList<String>();
161 RequestedAuthnContext authnContext = getRequestedAuthenticationContext();
162 if (authnContext == null) {
163 return requestedMethods;
166 // For the immediate future, we only support the "exact" comparator.
167 AuthnContextComparisonTypeEnumeration comparator = authnContext.getComparison();
168 if (comparator != null && comparator != AuthnContextComparisonTypeEnumeration.EXACT) {
169 log.error("Unsupported comparision operator ( " + comparator
170 + ") in RequestedAuthnContext. Only exact comparisions are supported.");
171 return requestedMethods;
174 // build a list of all requested authn classes and declrefs
175 List<AuthnContextClassRef> authnClasses = authnContext.getAuthnContextClassRefs();
176 List<AuthnContextDeclRef> authnDeclRefs = authnContext.getAuthnContextDeclRefs();
178 if (authnClasses != null) {
179 for (AuthnContextClassRef classRef : authnClasses) {
180 if (classRef != null) {
181 requestedMethods.add(classRef.getAuthnContextClassRef());
186 if (authnDeclRefs != null) {
187 for (AuthnContextDeclRef declRef : authnDeclRefs) {
188 if (declRef != null) {
189 requestedMethods.add(declRef.getAuthnContextDeclRef());
194 return requestedMethods;