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;
20 import java.util.List;
21 import java.util.LinkedList;
23 import org.apache.log4j.Logger;
25 import org.opensaml.saml2.core.AuthnContextClassRef;
26 import org.opensaml.saml2.core.AuthnContextDeclRef;
27 import org.opensaml.saml2.core.AuthnContextComparisonTypeEnumeration;
28 import org.opensaml.saml2.core.AuthnRequest;
29 import org.opensaml.saml2.core.RequestedAuthnContext;
33 * A SAML 2.0 {@link LoginContext}.
35 * This class can interpret {@link RequestedAuthnContext} and act accordingly.
37 public class Saml2LoginContext extends LoginContext {
39 private static final Logger log = Logger.getLogger(Saml2LoginContext.class);
41 /** The {@link RequestedAuthnContext} */
42 private RequestedAuthnContext ctx;
46 * Creates a new instance of Saml2LoginContext.
48 * @param authnRequest A SAML 2.0 Authentication Request.
50 public Saml2LoginContext(AuthnRequest authnRequest) {
52 if (authnRequest != null) {
53 forceAuth = authnRequest.isForceAuthn();
54 passiveAuth = authnRequest.isPassive();
55 ctx = authnRequest.getRequestedAuthnContext();
61 * This method evaluates a SAML2 {@link RequestedAuthnContext}
62 * and returns the list of requested authentication method URIs.
64 * If the AuthnQuery did not contain a RequestedAuthnContext,
65 * this method will return <code>null</code>.
67 * @return An array of authentication method URIs, or <code>null</code>.
69 public String[] getRequestedAuthenticationMethods() {
74 // For the immediate future, we only support the "exact" comparator.
75 // XXX: we should probably throw an exception or somehow indicate this
76 // as an error to the caller.
77 AuthnContextComparisonTypeEnumeration comparator = ctx.getComparison();
78 if (comparator != null && comparator != AuthnContextComparisonTypeEnumeration.EXACT) {
79 log.error("Unsupported comparision operator ( " + comparator
80 + ") in RequestedAuthnContext. Only exact comparisions are supported.");
84 // build a list of all requested authn classes and declrefs
85 List<String> requestedAuthnMethods = new LinkedList<String>();
86 List<AuthnContextClassRef> authnClasses = ctx.getAuthnContextClassRefs();
87 List<AuthnContextDeclRef> authnDeclRefs = ctx.getAuthnContextDeclRefs();
89 if (authnClasses != null) {
90 for (AuthnContextClassRef classRef : authnClasses) {
91 if (classRef != null) {
92 String s = classRef.getAuthnContextClassRef();
94 requestedAuthnMethods.add(s);
100 if (authnDeclRefs != null) {
101 for (AuthnContextDeclRef declRef : authnDeclRefs) {
102 if (declRef != null) {
103 String s = declRef.getAuthnContextDeclRef();
105 requestedAuthnMethods.add(s);
111 if (requestedAuthnMethods.size() == 0) {
114 String[] methods = new String[requestedAuthnMethods.size()];
115 return requestedAuthnMethods.toArray(methods);