001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: FSAuthnRequestEnvelope.java,v 1.2 2008/06/25 05:46:43 qcheng Exp $ 026 * Portions Copyrighted 2014 ForgeRock AS 027 */ 028 029package com.sun.identity.federation.message; 030 031import com.sun.identity.federation.common.FSUtils; 032import com.sun.identity.federation.common.IFSConstants; 033import com.sun.identity.federation.message.common.FSMsgException; 034import com.sun.identity.saml.common.SAMLUtils; 035import com.sun.identity.shared.encode.Base64; 036import com.sun.identity.shared.xml.XMLUtils; 037import java.util.List; 038import org.w3c.dom.Document; 039import org.w3c.dom.Element; 040import org.w3c.dom.Node; 041import org.w3c.dom.NodeList; 042 043/** 044 * This class defines methods for setting and retrieving attributes and 045 * elements associated with a Liberty Authentication Request. 046 * 047 * @supported.all.api 048 * @deprecated since 12.0.0 049 */ 050@Deprecated 051public class FSAuthnRequestEnvelope { 052 private String assertionConsumerServiceURL = null; 053 private List otherElements = null; 054 private FSAuthnRequest authnRequest = null; 055 private FSIDPList idpList = null; 056 private String providerID = null; 057 private String providerName = null; 058 private boolean isPassive = false; 059 private int minorVersion = IFSConstants.FF_11_PROTOCOL_MINOR_VERSION; 060 061 /** 062 * Default Constructor. 063 */ 064 public FSAuthnRequestEnvelope() { 065 } 066 067 /** 068 * Constructs a new <code>FSAuthnRequestEnvelope</code> object. 069 * 070 * @param authnRequest the authentication request 071 * @param providerID the provider's identifier 072 * @param providerName name of the provider 073 * @param assertionConsumerServiceURL absolute url of the assertion 074 * consumer service 075 * @param idpList list of identity providers 076 * @param isPassive true if identity provider must not interact 077 * with the <code>Principal</code>. 078 */ 079 080 public FSAuthnRequestEnvelope(FSAuthnRequest authnRequest, 081 String providerID, 082 String providerName, 083 String assertionConsumerServiceURL, 084 FSIDPList idpList, boolean isPassive ) { 085 this.authnRequest = authnRequest; 086 this.providerID = providerID; 087 this.providerName = providerName; 088 this.assertionConsumerServiceURL = assertionConsumerServiceURL; 089 this.idpList = idpList; 090 this.isPassive = isPassive; 091 } 092 093 /** 094 * Constructs a new <code>FSAuthnRequestEnvelope</code> object 095 * from a Document Element. 096 * 097 * @param root the Document Element . 098 * @throws FSMsgException if there is an error 099 * creating this object. 100 */ 101 102 public FSAuthnRequestEnvelope(Element root) throws FSMsgException { 103 if (root == null) { 104 SAMLUtils.debug.message( 105 "FSAuthnRequestEnvelope.parseXML: null input."); 106 throw new FSMsgException("nullInput",null); 107 } 108 String tag = null; 109 if (((tag = root.getLocalName()) == null) || 110 (!tag.equals(IFSConstants.AUTHN_REQUEST_ENVELOPE))) { 111 FSUtils.debug.message( 112 "FSAuthnRequestEnvelope.parseXML: wrong input."); 113 throw new FSMsgException("wrongInput",null); 114 } 115 String ns = root.getNamespaceURI(); 116 if (ns == null) { 117 FSUtils.debug.error("FSAuthnRequestEnvelope(Element):" 118 + " No namespace"); 119 throw new FSMsgException("wrongInput", null); 120 } 121 122 if (ns.equals(IFSConstants.FF_12_XML_NS)) { 123 minorVersion = IFSConstants.FF_12_PROTOCOL_MINOR_VERSION; 124 } 125 126 NodeList nl = root.getChildNodes(); 127 Node child; 128 String childName; 129 int length = nl.getLength(); 130 for (int i = 0; i < length; i++) { 131 child = nl.item(i); 132 if ((childName = child.getLocalName()) != null) { 133 if (childName.equals( 134 IFSConstants.ASSERTION_CONSUMER_SERVICE_URL)) { 135 assertionConsumerServiceURL = 136 XMLUtils.getElementValue((Element) child); 137 } else if (childName.equals(IFSConstants.IDP_LIST)) { 138 idpList = new FSIDPList((Element) child); 139 } else if (childName.equals(IFSConstants.AUTHN_REQUEST)) { 140 authnRequest = new FSAuthnRequest((Element) child); 141 } else if (childName.equals(IFSConstants.PROVIDER_ID)) { 142 providerID = XMLUtils.getElementValue((Element) child); 143 } else if (childName.equals(IFSConstants.PROVIDER_NAME)) { 144 providerName = XMLUtils.getElementValue((Element) child); 145 } else if (childName.equals(IFSConstants.IS_PASSIVE)) { 146 String strIsPassive = 147 XMLUtils.getElementValue((Element) child); 148 boolean isPassive = false; 149 if (strIsPassive != null && 150 strIsPassive.equals(IFSConstants.TRUE)) { 151 isPassive = true; 152 } 153 } 154 } 155 } 156 } 157 158 /** 159 * Returns the value of <code>MinorVersion</code> property. 160 * 161 * @return the value of <code>MinorVersion</code> property. 162 */ 163 public int getMinorVersion() { 164 return minorVersion; 165 } 166 167 /** 168 * Sets the value of <code>MinorVersion</code> property. 169 * 170 * @param minorVersion the value of <code>MinorVersion</code> property. 171 * @see #setMinorVersion(int) 172 */ 173 174 public void setMinorVersion(int minorVersion) { 175 this.minorVersion = minorVersion; 176 } 177 178 /** 179 * Returns the request as an XML Document String 180 * based on the Liberty Request schema. 181 * 182 * @return XML String representing the request. 183 * @throws <code>FSMsgException</code> if there is an error. 184 */ 185 186 public String toXMLString() throws FSMsgException { 187 return toXMLString(true, true); 188 } 189 190 /** 191 * Creates a String representation of the <lib:AuthnRequest> element. 192 * @param includeNS : Determines whether or not the namespace qualifier 193 * is prepended to the Element when converted 194 * @param declareNS : Determines whether or not the namespace is declared 195 * within the Element. 196 * @return String containing the valid XML for this element. 197 * @throws FSMsgException if there is an error. 198 */ 199 200 public String toXMLString(boolean includeNS,boolean declareNS) 201 throws FSMsgException { 202 return toXMLString(includeNS, declareNS, false); 203 } 204 205 /** 206 * Creates a String representation of the <lib:AuthnRequest> element. 207 * 208 * @param includeNS Determines whether or not the namespace qualifier 209 * is prepended to the Element when converted 210 * @param declareNS Determines whether or not the namespace is declared 211 * within the Element. 212 * @param includeHeader Determines whether the output include the xml 213 * declaration header. 214 * @return A string containing the valid XML for this element. 215 * @throws <code>FSMsgException</code> if there is an error. 216 */ 217 public String toXMLString(boolean includeNS, 218 boolean declareNS, 219 boolean includeHeader) throws FSMsgException { 220 221 StringBuffer xml = new StringBuffer(300); 222 if (includeHeader) { 223 xml.append(IFSConstants.XML_PREFIX) 224 .append(IFSConstants.DEFAULT_ENCODING) 225 .append(IFSConstants.QUOTE) 226 .append(IFSConstants.SPACE) 227 .append(IFSConstants.QUESTION_MARK) 228 .append(IFSConstants.RIGHT_ANGLE) 229 .append(IFSConstants.NL); 230 } 231 String prefix = ""; 232 String uri = ""; 233 if (includeNS) { 234 prefix = IFSConstants.LIB_PREFIX; 235 } 236 if (declareNS) { 237 if(minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) { 238 uri = IFSConstants.LIB_12_NAMESPACE_STRING; 239 } else { 240 uri = IFSConstants.LIB_NAMESPACE_STRING; 241 } 242 } 243 244 xml.append(IFSConstants.LEFT_ANGLE) 245 .append(prefix) 246 .append(IFSConstants.AUTHN_REQUEST_ENVELOPE) 247 .append(uri) 248 .append(IFSConstants.RIGHT_ANGLE); 249 250 if (authnRequest != null){ 251 xml.append(authnRequest.toXMLString()); 252 } 253 254 if (providerID != null && providerID.length() != 0){ 255 xml.append(IFSConstants.LEFT_ANGLE) 256 .append(prefix) 257 .append(IFSConstants.PROVIDER_ID) 258 .append(uri) 259 .append(IFSConstants.RIGHT_ANGLE) 260 .append(providerID) 261 .append(IFSConstants.START_END_ELEMENT) 262 .append(prefix) 263 .append(IFSConstants.PROVIDER_ID) 264 .append(IFSConstants.RIGHT_ANGLE); 265 } 266 267 if (providerName != null && providerName.length() != 0){ 268 xml.append(IFSConstants.LEFT_ANGLE) 269 .append(prefix) 270 .append(IFSConstants.PROVIDER_NAME) 271 .append(uri) 272 .append(IFSConstants.RIGHT_ANGLE) 273 .append(providerName) 274 .append(IFSConstants.START_END_ELEMENT) 275 .append(prefix) 276 .append("ProviderName") 277 .append(IFSConstants.PROVIDER_NAME) 278 .append(IFSConstants.RIGHT_ANGLE); 279 } 280 281 if (assertionConsumerServiceURL != null && 282 assertionConsumerServiceURL.length() != 0) { 283 xml.append(IFSConstants.LEFT_ANGLE) 284 .append(prefix) 285 .append(IFSConstants.ASSERTION_CONSUMER_SERVICE_URL) 286 .append(uri) 287 .append(IFSConstants.RIGHT_ANGLE) 288 .append(assertionConsumerServiceURL) 289 .append(IFSConstants.START_END_ELEMENT) 290 .append(prefix) 291 .append(IFSConstants.ASSERTION_CONSUMER_SERVICE_URL) 292 .append(IFSConstants.RIGHT_ANGLE); 293 } 294 295 if (idpList != null){ 296 xml.append(idpList.toXMLString()); 297 } 298 299 String strIsPassive = IFSConstants.FALSE; 300 if (isPassive) { 301 strIsPassive = IFSConstants.TRUE; 302 } 303 304 xml.append(IFSConstants.LEFT_ANGLE) 305 .append(prefix) 306 .append(IFSConstants.IS_PASSIVE) 307 .append(IFSConstants.RIGHT_ANGLE) 308 .append(strIsPassive) 309 .append(IFSConstants.START_END_ELEMENT) 310 .append(prefix) 311 .append(IFSConstants.IS_PASSIVE) 312 .append(IFSConstants.RIGHT_ANGLE); 313 314 //Other elements needs to be handled here 315 316 xml.append(IFSConstants.START_END_ELEMENT) 317 .append(prefix) 318 .append(IFSConstants.AUTHN_REQUEST_ENVELOPE) 319 .append(IFSConstants.RIGHT_ANGLE); 320 321 return xml.toString(); 322 } 323 324 /** 325 * Returns the <code>FSAuthnRequestEnvelope</code> object. 326 * 327 * @param xml the XML string to create this object from 328 * @return <code>FSAuthnRequestEnvelope</code> object. 329 * @throws FSMsgException if there is 330 * error creating the object. 331 */ 332 333 public static FSAuthnRequestEnvelope parseXML(String xml) 334 throws FSMsgException { 335 Document doc = XMLUtils.toDOMDocument(xml, FSUtils.debug); 336 if (doc == null) { 337 if (FSUtils.debug.messageEnabled()) { 338 FSUtils.debug.message( 339 "FSAuthnRequestEnvelope.parseXML:Error " 340 + "while parsing input xml string"); 341 } 342 throw new FSMsgException("parseError",null); 343 } 344 Element root = doc.getDocumentElement(); 345 return new FSAuthnRequestEnvelope(root); 346 } 347 348 /** 349 * Returns the value of <code>AssertionConsumerServiceURL</code> attribute. 350 * 351 * @return the value of <code>AssertionConsumerServiceURL</code> attribute. 352 * @see #setAssertionConsumerServiceURL(String) 353 */ 354 public String getAssertionConsumerServiceURL() { 355 return assertionConsumerServiceURL; 356 } 357 358 /** 359 * Sets the value of <code>AssertionConsumerServiceURL</code> attribute. 360 * 361 * @param assertionConsumerURL the value of 362 * <code>AssertionConsumerServiceURL</code> attribute. 363 * @see #getAssertionConsumerServiceURL 364 */ 365 366 public void setAssertionConsumerServiceURL(String assertionConsumerURL) { 367 this.assertionConsumerServiceURL = assertionConsumerURL; 368 } 369 370 /** 371 * Returns the <code>FSAuthnRequest</code> object. 372 * 373 * @return the <code>FSAuthnRequest</code> object. 374 * @see #setAuthnRequest(FSAuthnRequest) 375 */ 376 public FSAuthnRequest getAuthnRequest() { 377 return authnRequest; 378 } 379 380 /** 381 * Sets the <code>FSAuthnRequest</code> object. 382 * 383 * @param authnRequest the <code>FSAuthnRequest</code> object. 384 * @see #getAuthnRequest 385 */ 386 public void setAuthnRequest(FSAuthnRequest authnRequest) { 387 this.authnRequest = authnRequest; 388 } 389 390 /** 391 * Returns the <code>FSIDPList</code> object. 392 * 393 * return the <code>FSIDPList</code> object. 394 * @see #setIDPList(FSIDPList) 395 */ 396 public FSIDPList getIDPList() { 397 return idpList; 398 } 399 400 /** 401 * Sets the <code>FSIDPList</code> object. 402 * 403 * @param idpList the <code>FSIDPList</code> object. 404 * @see #getIDPList 405 */ 406 public void setIDPList(FSIDPList idpList) { 407 this.idpList = idpList; 408 } 409 410 /** 411 * Returns a list of elements. 412 * 413 * @return list of elements. 414 * @see #setOtherElements(List) 415 */ 416 public List getOtherElements() { 417 return otherElements; 418 } 419 420 /** 421 * Sets a list of elements. 422 * 423 * @param otherElements a list of elements. 424 * @see #getOtherElements 425 */ 426 public void setOtherElements(List otherElements) { 427 this.otherElements = otherElements; 428 } 429 430 /** 431 * Returns <code>FSAuthnRequestEnvelope</code> object. The object 432 * is created by parsing an Base64 encode authentication 433 * request String. 434 * 435 * @param encodedReq the encoded string. 436 * @throws <code>FSMsgException</code> if there is an error 437 * creating <code>FSAuthnRequestEnvelope</code> object. 438 */ 439 440 public static FSAuthnRequestEnvelope parseBASE64EncodedString( 441 String encodedReq) throws FSMsgException { 442 if (encodedReq != null) { 443 String decodedAuthnReq = new String(Base64.decode(encodedReq)); 444 if (FSUtils.debug.messageEnabled()) { 445 FSUtils.debug.message("FSAuthnRequestEnvelope." 446 + "parseBASE64EncodedString: decoded input string: \n" 447 + decodedAuthnReq); 448 } 449 return parseXML(decodedAuthnReq); 450 } else { 451 if (FSUtils.debug.messageEnabled()) { 452 FSUtils.debug.message( 453 "FSAuthnRequestEnvelope.parseBASE64EncodedString: null " 454 + " String passed in as argument."); 455 } 456 throw new FSMsgException("nullInput",null); 457 } 458 } 459 460 /** 461 * Returns a Base64 Encoded Authentication Request String. 462 * 463 * @return a Base64 Encoded Authentication Request String. 464 * @throws FSMsgException if there is an error encoding 465 * the string. 466 */ 467 public String toBASE64EncodedString() throws FSMsgException { 468 if((assertionConsumerServiceURL == null) || 469 (assertionConsumerServiceURL.length() == 0)) { 470 FSUtils.debug.error("FSAuthnRequestEnvelope.toBASE64EncodedString:" 471 + "assertionConsumerServiceURL is null in the " 472 + "FSAuthnRequestEnvelope"); 473 throw new FSMsgException( 474 "noAssertionConsumerServiceURLElement",null); 475 } 476 if (authnRequest == null){ 477 FSUtils.debug.error("FSAuthnRequestEnvelope.toBASE64EncodedString:" 478 + "authnRequest is null in the FSAuthnRequestEnvelope"); 479 throw new FSMsgException("noAuthnRequestElement",null); 480 } 481 return Base64.encode(this.toXMLString().getBytes()); 482 } 483}