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: FSFederationTerminationNotification.java,v 1.3 2008/06/25 05:46:44 qcheng Exp $ 026 * Portions Copyrighted 2014 ForgeRock AS 027 */ 028 029package com.sun.identity.federation.message; 030 031 032import com.sun.identity.federation.common.FSUtils; 033import com.sun.identity.federation.common.IFSConstants; 034import com.sun.identity.federation.message.common.FSMsgException; 035import com.sun.identity.saml.assertion.NameIdentifier; 036import com.sun.identity.saml.common.SAMLUtils; 037import com.sun.identity.saml.common.SAMLConstants; 038import com.sun.identity.saml.common.SAMLException; 039import com.sun.identity.saml.common.SAMLResponderException; 040import com.sun.identity.saml.protocol.AbstractRequest; 041import com.sun.identity.saml.xmlsig.XMLSignatureManager; 042import com.sun.identity.shared.encode.Base64; 043import com.sun.identity.shared.DateUtils; 044import com.sun.identity.shared.encode.URLEncDec; 045import com.sun.identity.shared.xml.XMLUtils; 046import java.text.ParseException; 047import java.util.ArrayList; 048import java.util.Collections; 049import java.util.Date; 050import java.util.Iterator; 051import java.util.List; 052import javax.servlet.http.HttpServletRequest; 053import org.w3c.dom.Element; 054import org.w3c.dom.Document; 055import org.w3c.dom.Node; 056import org.w3c.dom.NodeList; 057 058/** 059 * This class has methods for creating object and message for 060 * Federation Termination. 061 * 062 * @supported.all.api 063 * @deprecated since 12.0.0 064 */ 065@Deprecated 066 067public class FSFederationTerminationNotification extends AbstractRequest { 068 private String providerId; 069 private NameIdentifier nameIdentifier; 070 protected String xmlString; 071 protected String signatureString; 072 protected String id; 073 private String relayState; 074 075 /** 076 * Default Constructor. 077 */ 078 public FSFederationTerminationNotification() { 079 try { 080 setIssueInstant(new Date()); 081 providerId = new String(); 082 nameIdentifier = new NameIdentifier("Test", "Test"); 083 } catch(SAMLException e){ 084 if (FSUtils.debug.messageEnabled()) { 085 FSUtils.debug.message( 086 "FSFederationTerminationNotification.constructor:", e); 087 } 088 } 089 090 } 091 092 /** 093 * Creates <code>FSFederationTerminationNotification<object> from 094 * a Document Element. 095 * 096 * @param root the Document Element. 097 * @throws FSMsgException if there is an error creating 098 * this object. 099 */ 100 public FSFederationTerminationNotification(Element root) 101 throws FSMsgException { 102 String tag = null; 103 if (root == null) { 104 FSUtils.debug.message( 105 "FSFederationTerminationNotification(Element):null input."); 106 throw new FSMsgException("nullInput",null); 107 } 108 if (((tag = root.getLocalName()) == null) || 109 (!tag.equals("FederationTerminationNotification"))) { 110 FSUtils.debug.message( 111 "FSFederationTerminationNotification(Element):wrong input"); 112 throw new FSMsgException("wrongInput",null); 113 } 114 // Attribute IssueInstant 115 String instantString = root.getAttribute(IFSConstants.ISSUE_INSTANT); 116 if ((instantString == null) || (instantString.length() == 0)) { 117 FSUtils.debug.message("FederationTerminationNotification(Element): " 118 + "missing IssueInstant"); 119 String[] args = { IFSConstants.ISSUE_INSTANT }; 120 throw new FSMsgException("missingAttribute",args); 121 } else { 122 try { 123 issueInstant = DateUtils.stringToDate(instantString); 124 } catch (ParseException e) { 125 if (FSUtils.debug.messageEnabled()) { 126 FSUtils.debug.message("FederationTerminationNotification " 127 + " (Element): could not parse IssueInstant", e); 128 } 129 throw new FSMsgException("wrongInput", null); 130 } 131 } 132 int length = 0; 133 id = root.getAttribute(IFSConstants.ID); 134 requestID = root.getAttribute(IFSConstants.REQUEST_ID); 135 parseMajorVersion(root.getAttribute(IFSConstants.MAJOR_VERSION)); 136 parseMinorVersion(root.getAttribute(IFSConstants.MINOR_VERSION)); 137 NodeList contentnl = root.getChildNodes(); 138 Node child; 139 String nodeName; 140 length = contentnl.getLength(); 141 for (int i = 0; i < length; i++) { 142 child = contentnl.item(i); 143 if ((nodeName = child.getLocalName()) != null) { 144 if (nodeName.equals(IFSConstants.RESPONDWITH)) { 145 if (respondWiths == Collections.EMPTY_LIST) { 146 respondWiths = new ArrayList(); 147 } 148 respondWiths.add(XMLUtils.getElementValue((Element) child)); 149 } else if (nodeName.equals(IFSConstants.SIGNATURE)) { 150 } else if (nodeName.equals(IFSConstants.PROVIDER_ID)) { 151 if (providerId != null) { 152 if (FSUtils.debug.messageEnabled()) { 153 FSUtils.debug.message( 154 "FSFederationTerminationNotification(" + 155 "Element : should contain only one " + 156 "ProviderID."); 157 } 158 throw new FSMsgException("wrongInput",null); 159 } 160 providerId = XMLUtils.getElementValue((Element) child); 161 } else if (nodeName.equals(IFSConstants.NAME_IDENTIFIER)) { 162 try{ 163 this.nameIdentifier = 164 new NameIdentifier((Element)child); 165 } catch(SAMLException ex){ 166 if (FSUtils.debug.messageEnabled()) { 167 FSUtils.debug.message( 168 "FSFederationTerminationNotification " 169 + "(Element): SAMLException " 170 + "while constructing nameidentifier"); 171 } 172 throw new FSMsgException("nameIdentifierCreateError", 173 null); 174 } 175 } else if (nodeName.equals(IFSConstants.RELAY_STATE)){ 176 if (relayState != null) { 177 if (FSUtils.debug.messageEnabled()) { 178 FSUtils.debug.message( 179 "FSFederationTerminationNotification " 180 + "(Element) :should contain only one " 181 + "relayState."); 182 } 183 throw new FSMsgException("wrongInput",null); 184 } 185 relayState = XMLUtils.getElementValue((Element) child); 186 } else { 187 if (FSUtils.debug.messageEnabled()) { 188 FSUtils.debug.message( 189 "FSFederationTerminationNotification(Element): " 190 + " invalid node" + nodeName); 191 } 192 throw new FSMsgException("wrongInput",null); 193 } 194 } 195 } 196 197 //check for signature 198 List signs = XMLUtils.getElementsByTagNameNS1(root, 199 SAMLConstants.XMLSIG_NAMESPACE_URI, 200 SAMLConstants.XMLSIG_ELEMENT_NAME); 201 int signsSize = signs.size(); 202 if (signsSize == 1) { 203 Element elem = (Element)signs.get(0); 204 setSignature(elem); 205 xmlString = XMLUtils.print(root); 206 signed = true; 207 } else if (signsSize != 0) { 208 if (FSUtils.debug.messageEnabled()) { 209 FSUtils.debug.message( 210 "FSFederationTerminationNotification(Element):" 211 + "included more than one Signature element."); 212 } 213 throw new FSMsgException("moreElement",null); 214 } 215 } 216 217 /** 218 * Creates <code>FSFederationTerminationNotification</code> object. 219 * 220 * @param requestId the request identifier. 221 * @param providerID the provider identifier. 222 * @param nameId the <code>NameIdentifier</code> object. 223 * @throws FSMsgException if there is an error creating 224 * this object. 225 */ 226 public FSFederationTerminationNotification(String requestId, 227 String providerID,NameIdentifier nameId) throws FSMsgException { 228 int length = 0; 229 int i = 0; 230 setIssueInstant(new Date()); 231 if ((respondWiths != null) && 232 (respondWiths != Collections.EMPTY_LIST)) { 233 length = respondWiths.size(); 234 for (i = 0; i < length; i++) { 235 Object temp = respondWiths.get(i); 236 if (!(temp instanceof String)) { 237 if (FSUtils.debug.messageEnabled()) { 238 FSUtils.debug.message( 239 "FSFederationTerminationNotification:" 240 + "wrong input for RespondWith"); 241 } 242 throw new FSMsgException("wrongInput",null); 243 } 244 } 245 this.respondWiths = respondWiths; 246 } 247 248 if ((requestId != null) && (requestId.length() != 0)) { 249 requestID = requestId; 250 } else { 251 // random generate one 252 requestID = SAMLUtils.generateID(); 253 if (requestID == null) { 254 FSUtils.debug.error("FSFederationTerminationNotification: " 255 + "couldn't generate RequestID."); 256 throw new FSMsgException("errorGenerateID",null); 257 } 258 } 259 this.providerId = providerID; 260 this.nameIdentifier = nameId; 261 } 262 263 /** 264 * Returns the string representation of this object. 265 * This method translates the response to an XML document string based on 266 * the Response schema described above. 267 * 268 * @return An XML String representing the response. NOTE: this is a 269 * complete SAML response xml string with ResponseID, 270 * MajorVersion, etc. 271 * @throws FSMsgException if there is an error converting 272 * this object ot a string. 273 */ 274 public String toXMLString(boolean includeNS, boolean declareNS) 275 throws FSMsgException { 276 return toXMLString(includeNS, declareNS, false); 277 } 278 279 280 /** 281 * Returns a String representation of the <samlp:Response> element. 282 * 283 * @param includeNS Determines whether or not the namespace qualifier 284 * is prepended to the Element when converted 285 * @param declareNS Determines whether or not the namespace is declared 286 * within the Element. 287 * @param includeHeader Determines whether the output include the xml 288 * declaration header. 289 * @return a string containing the valid XML for this element 290 * @throws FSMsgException if there is an error converting 291 * this object ot a string. 292 */ 293 public String toXMLString(boolean includeNS, boolean declareNS, 294 boolean includeHeader) throws FSMsgException { 295 if((providerId == null) || (providerId.length() == 0)){ 296 FSUtils.debug.error( 297 "FSFederationTerminationNotification.toXMLString" 298 + ": providerId is null in the request with requestId:" 299 + requestID); 300 String[] args = { requestID }; 301 throw new FSMsgException("nullProviderIdWRequestId" ,args); 302 } 303 if ((requestID == null) || (requestID.length() == 0)){ 304 requestID = SAMLUtils.generateID(); 305 if (requestID == null) { 306 FSUtils.debug.error("FSFederationTerminationNotification." 307 + "toXMLString: couldn't generate RequestID."); 308 throw new FSMsgException("errorGenerateID",null); 309 } 310 } 311 312 StringBuffer xml = new StringBuffer(300); 313 if (includeHeader) { 314 xml.append(IFSConstants.XML_PREFIX) 315 .append(IFSConstants.QUOTE) 316 .append(IFSConstants.SPACE) 317 .append(IFSConstants.QUESTION_MARK) 318 .append(IFSConstants.RIGHT_ANGLE); 319 } 320 String prefix = ""; 321 String uriSAML = ""; 322 String uri = ""; 323 if (includeNS) { 324 prefix = IFSConstants.LIB_PREFIX; 325 } 326 if (declareNS) { 327 uri = IFSConstants.LIB_NAMESPACE_STRING; 328 if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) { 329 uri = IFSConstants.LIB_12_NAMESPACE_STRING; 330 } 331 uriSAML = IFSConstants.assertionDeclareStr; 332 } 333 334 String instantString = DateUtils.toUTCDateFormat(issueInstant); 335 336 if(requestID != null){ 337 xml.append(IFSConstants.LEFT_ANGLE) 338 .append(prefix) 339 .append(IFSConstants.FEDERATION_TERMINATION_NOTICFICATION) 340 .append(uri) 341 .append(uriSAML) 342 .append(IFSConstants.SPACE); 343 344 if (minorVersion == IFSConstants.FF_11_PROTOCOL_MINOR_VERSION && 345 id != null && !(id.length() == 0)) { 346 xml.append(IFSConstants.SPACE) 347 .append("id") 348 .append(IFSConstants.EQUAL_TO) 349 .append(IFSConstants.QUOTE) 350 .append(id) 351 .append(IFSConstants.QUOTE) 352 .append(IFSConstants.SPACE); 353 } 354 xml.append(IFSConstants.REQUEST_ID) 355 .append(IFSConstants.EQUAL_TO) 356 .append(IFSConstants.QUOTE) 357 .append(requestID) 358 .append(IFSConstants.QUOTE) 359 .append(IFSConstants.SPACE) 360 .append(IFSConstants.MAJOR_VERSION) 361 .append(IFSConstants.EQUAL_TO) 362 .append(IFSConstants.QUOTE) 363 .append(majorVersion) 364 .append(IFSConstants.QUOTE) 365 .append(IFSConstants.SPACE) 366 .append(IFSConstants.MINOR_VERSION) 367 .append(IFSConstants.EQUAL_TO) 368 .append(IFSConstants.QUOTE) 369 .append(minorVersion) 370 .append(IFSConstants.QUOTE) 371 .append(IFSConstants.SPACE) 372 .append(IFSConstants.ISSUE_INSTANT) 373 .append(IFSConstants.EQUAL_TO) 374 .append(IFSConstants.QUOTE) 375 .append(instantString) 376 .append(IFSConstants.QUOTE) 377 .append(IFSConstants.RIGHT_ANGLE); 378 379 if ((respondWiths != null) && 380 (respondWiths != Collections.EMPTY_LIST)) { 381 Iterator i = respondWiths.iterator(); 382 while (i.hasNext()) { 383 xml.append(IFSConstants.LEFT_ANGLE) 384 .append(prefix) 385 .append(IFSConstants.RESPONDWITH) 386 .append(IFSConstants.RIGHT_ANGLE) 387 .append((String) i.next()) 388 .append(IFSConstants.START_END_ELEMENT) 389 .append(prefix) 390 .append(IFSConstants.RESPONDWITH) 391 .append(IFSConstants.LEFT_ANGLE); 392 } 393 } 394 395 if (signed) { 396 if (signatureString != null) { 397 xml.append(signatureString); 398 } else if (signature != null) { 399 signatureString = XMLUtils.print(signature); 400 xml.append(signatureString); 401 } 402 } 403 404 xml.append(IFSConstants.LEFT_ANGLE) 405 .append(prefix) 406 .append(IFSConstants.PROVIDER_ID) 407 .append(uri) 408 .append(IFSConstants.RIGHT_ANGLE) 409 .append(providerId) 410 .append(IFSConstants.START_END_ELEMENT) 411 .append(prefix) 412 .append(IFSConstants.PROVIDER_ID) 413 .append(IFSConstants.RIGHT_ANGLE); 414 415 if (nameIdentifier != null) { 416 xml.append(nameIdentifier.toString()); 417 } 418 419 if (relayState != null) { 420 xml.append(IFSConstants.LEFT_ANGLE) 421 .append(prefix) 422 .append(IFSConstants.RELAY_STATE) 423 .append(uri) 424 .append(IFSConstants.RIGHT_ANGLE) 425 .append(providerId) 426 .append(IFSConstants.START_END_ELEMENT) 427 .append(prefix) 428 .append(IFSConstants.RELAY_STATE) 429 .append(IFSConstants.RIGHT_ANGLE); 430 } 431 432 xml.append(IFSConstants.START_END_ELEMENT) 433 .append(prefix) 434 .append(IFSConstants.FEDERATION_TERMINATION_NOTICFICATION) 435 .append(IFSConstants.RIGHT_ANGLE); 436 } else { 437 if (FSUtils.debug.messageEnabled()) { 438 FSUtils.debug.message("FSFederationTerminationNotification." 439 + "toString: requestID is null "); 440 } 441 throw new FSMsgException("nullRequestID",null); 442 } 443 return xml.toString(); 444 } 445 446 /** 447 * Returns the string representation of this object. 448 * This method translates the response to an XML document string. 449 * 450 * @return An XML String representing the response. NOTE: this is a 451 * complete SAML response xml string with ResponseID, 452 * MajorVersion, etc. 453 */ 454 public String toXMLString() throws FSMsgException { 455 return toXMLString(true, true); 456 } 457 /** 458 * Returns the <code>FSAuthnRequest</code> object. 459 * 460 * @param xml the XML string to be parsed. 461 * @return <code>FSAuthnRequest</code> object created from the XML string. 462 * @throws FSMsgException if there is 463 * error creating the object. 464 */ 465 public static FSFederationTerminationNotification parseXML(String xml) 466 throws FSMsgException { 467 Document doc = XMLUtils.toDOMDocument(xml, FSUtils.debug); 468 if (doc == null) { 469 if (FSUtils.debug.messageEnabled()) { 470 FSUtils.debug.message( 471 "FSFederationTerminationNotification.parseXML:Error " 472 + "while parsing input xml string"); 473 } 474 throw new FSMsgException("parseError",null); 475 } 476 Element root = doc.getDocumentElement(); 477 return new FSFederationTerminationNotification(root); 478 } 479 480 /** 481 * Returns the value of <code>id</code> attribute. 482 * 483 * @return the value of <code>id</code> attribute. 484 * @see #setID(String) 485 */ 486 public String getID() { 487 return id; 488 } 489 490 /** 491 * Sets the value of <code>id</code> attribute. 492 * 493 * @param id the value of <code>id</code> attribute. 494 * @see #getID() 495 */ 496 public void setID(String id){ 497 this.id = id; 498 } 499 500 /** 501 * Set the value of <code>RelayState</code> attribute. 502 * 503 * @param relayState the value of <code>RelayState</code> attribute. 504 * @see #getRelayState() 505 */ 506 public void setRelayState(String relayState){ 507 this.relayState = relayState; 508 } 509 510 /** 511 * Returns the value of <code>RelayState</code> attribute. 512 * 513 * @return the value of <code>RelayState</code> attribute. 514 * @see #setRelayState(String) 515 */ 516 public String getRelayState() { 517 return relayState; 518 } 519 520 /** 521 * Returns the value of <code>MinorVersion</code> attribute. 522 * 523 * @return the value of <code>MinorVersion</code> attribute. 524 * @see #setMinorVersion(int) 525 */ 526 public int getMinorVersion() { 527 return minorVersion; 528 } 529 530 /** 531 * Sets the value of <code>MinorVersion</code> attribute. 532 * 533 * @param version the value of <code>MinorVersion</code> attribute. 534 * @see #getMinorVersion() 535 */ 536 public void setMinorVersion(int version) { 537 minorVersion = version; 538 } 539 /** 540 * Returns the value of <code>ProviderID</code> attribute. 541 * 542 * @return the value of <code>ProviderID</code> attribute. 543 * @see #setProviderId(String). 544 */ 545 public String getProviderId() { 546 return providerId; 547 } 548 549 /** 550 * Sets the value of <code>ProviderID</code> attribute. 551 * 552 * @param providerID the value of <code>ProviderID</code> attribute. 553 * @see #getProviderId() 554 */ 555 public void setProviderId(String providerID) { 556 this.providerId = providerID; 557 } 558 559 /** 560 * Returns the <code>NameIdentifier</code> object. 561 * 562 * @return the <code>NameIdentifier</code> object. 563 */ 564 public NameIdentifier getNameIdentifier() { 565 return nameIdentifier; 566 } 567 568 /** 569 * Sets the <code>NameIdentifier</code> object. 570 * 571 * @param nameId the <code>NameIdentifier</code> object. 572 */ 573 public void setNameIdentifier(NameIdentifier nameId) { 574 this.nameIdentifier = nameId; 575 } 576 577 /** 578 * Returns an <code>URL</code> encoded query string. 579 * 580 * @return a <code>URL</code> encoded query string. 581 * @throws FSMsgException if there is an error. 582 */ 583 public String toURLEncodedQueryString() throws FSMsgException { 584 if((providerId == null) || (providerId.length() == 0)) { 585 FSUtils.debug.error("FSFederationTerminationNotification." 586 + "toURLEncodedQueryString: providerId is null in the " 587 + "request with requestId: " + requestID); 588 String[] args = { requestID }; 589 throw new FSMsgException("nullProviderIdWRequestId",args); 590 } 591 if ((requestID == null) || (requestID.length() == 0)) { 592 requestID = SAMLUtils.generateID(); 593 if (requestID == null) { 594 FSUtils.debug.error("FSFederationTerminationNotification." 595 + "toURLEncodedQueryString: couldn't generate " 596 + "RequestID."); 597 throw new FSMsgException("errorGenerateID",null); 598 } 599 } 600 StringBuffer urlEncodedAuthnReq = new StringBuffer(300); 601 urlEncodedAuthnReq.append(IFSConstants.REQUEST_ID) 602 .append(IFSConstants.EQUAL_TO) 603 .append(URLEncDec.encode(requestID)) 604 .append(IFSConstants.AMPERSAND); 605 urlEncodedAuthnReq.append(IFSConstants.MAJOR_VERSION) 606 .append(IFSConstants.EQUAL_TO) 607 .append(majorVersion).append(IFSConstants.AMPERSAND); 608 urlEncodedAuthnReq.append(IFSConstants.MINOR_VERSION) 609 .append(IFSConstants.EQUAL_TO) 610 .append(minorVersion).append(IFSConstants.AMPERSAND); 611 612 if(issueInstant != null){ 613 urlEncodedAuthnReq.append(IFSConstants.ISSUE_INSTANT) 614 .append(IFSConstants.EQUAL_TO) 615 .append(URLEncDec.encode(DateUtils.toUTCDateFormat(issueInstant))) 616 .append(IFSConstants.AMPERSAND); 617 } else { 618 FSUtils.debug.error("FSFederationTerminationNotification." 619 + "toURLEncodedQueryString: issueInstant missing"); 620 String[] args = { IFSConstants.ISSUE_INSTANT }; 621 throw new FSMsgException("missingAttribute",args); 622 } 623 if (providerId != null && providerId.length() != 0) { 624 urlEncodedAuthnReq.append(IFSConstants.PROVIDER_ID) 625 .append(IFSConstants.EQUAL_TO) 626 .append(URLEncDec.encode(providerId)) 627 .append(IFSConstants.AMPERSAND); 628 } 629 630 if (nameIdentifier != null) { 631 if (nameIdentifier.getName() != null && 632 nameIdentifier.getName().length() != 0) { 633 urlEncodedAuthnReq.append(IFSConstants.NAME) 634 .append(IFSConstants.EQUAL_TO) 635 .append(URLEncDec.encode( 636 nameIdentifier.getName())) 637 .append(IFSConstants.AMPERSAND) 638 .append(IFSConstants.NAME_IDENTIFIER) 639 .append(IFSConstants.EQUAL_TO) 640 .append(URLEncDec.encode( 641 nameIdentifier.getName())) 642 .append(IFSConstants.AMPERSAND); 643 } 644 if (nameIdentifier.getNameQualifier() != null && 645 nameIdentifier.getNameQualifier().length() != 0) { 646 urlEncodedAuthnReq.append(IFSConstants.NAME_QUALIFIER) 647 .append(IFSConstants.EQUAL_TO) 648 .append(URLEncDec.encode( 649 nameIdentifier.getNameQualifier())) 650 .append(IFSConstants.AMPERSAND); 651 } 652 if (nameIdentifier.getFormat() != null && 653 nameIdentifier.getFormat().length() != 0) { 654 urlEncodedAuthnReq.append(IFSConstants.NAME_FORMAT) 655 .append(IFSConstants.EQUAL_TO) 656 .append(URLEncDec.encode( 657 nameIdentifier.getFormat())) 658 .append(IFSConstants.AMPERSAND); 659 } 660 } 661 662 if (relayState != null) { 663 urlEncodedAuthnReq.append(IFSConstants.RELAY_STATE) 664 .append(IFSConstants.EQUAL_TO) 665 .append(URLEncDec.encode(relayState)) 666 .append(IFSConstants.AMPERSAND); 667 } 668 return urlEncodedAuthnReq.toString(); 669 } 670 671 /** 672 * Returns a <code>Base64</code> encoded string representing this 673 * object. 674 * 675 * @return a <code>Base64</code> encoded string representing this 676 * object. 677 * @throws FSMsgException if there is an error creating 678 * a <code>Base64</code> encoded string. 679 */ 680 public String toBASE64EncodedString() throws FSMsgException { 681 if((providerId == null) || (providerId.length() == 0)){ 682 FSUtils.debug.error("FSFederationTerminationNotification." 683 + "toURLEncodedQueryString: providerId is null in the " 684 + "request with requestId:" + requestID); 685 String[] args = { requestID }; 686 throw new FSMsgException("nullProviderIdWRequestId",args); 687 } 688 if ((requestID == null) || (requestID.length() == 0)) { 689 requestID = SAMLUtils.generateID(); 690 if (requestID == null) { 691 FSUtils.debug.error("FSFederationTerminationNotification." 692 + "toURLEncodedQueryString: couldn't generate " 693 + "RequestID."); 694 throw new FSMsgException("errorGenerateID",null); 695 } 696 } 697 return Base64.encode(this.toXMLString().getBytes()); 698 } 699 700 701 /** 702 * Returns <code>FSFederationTerminationNotification</code> object. The 703 * object is creating by parsing the <code>HttpServletRequest</code> 704 * object. 705 * 706 * @param request the <code>HttpServletRequest</code> object. 707 * @throws FSMsgException if there is an error 708 * creating <code>FSFederationTerminationNotification</code> object. 709 */ 710 public static FSFederationTerminationNotification parseURLEncodedRequest( 711 HttpServletRequest request 712 ) throws FSMsgException, SAMLException { 713 FSFederationTerminationNotification 714 retFederationTerminationNotification = 715 new FSFederationTerminationNotification(); 716 try{ 717 FSUtils.debug.message("checking minor version"); 718 retFederationTerminationNotification.majorVersion = 719 Integer.parseInt( 720 request.getParameter(IFSConstants.MAJOR_VERSION)); 721 retFederationTerminationNotification.minorVersion = 722 Integer.parseInt(request.getParameter( 723 IFSConstants.MINOR_VERSION)); 724 } catch(NumberFormatException ex){ 725 throw new FSMsgException("invalidNumber",null); 726 } 727 728 String requestID = request.getParameter(IFSConstants.REQUEST_ID); 729 if (request != null) { 730 retFederationTerminationNotification.requestID = requestID; 731 } else { 732 String[] args = { IFSConstants.REQUEST_ID }; 733 throw new FSMsgException("missingAttribute",args); 734 } 735 736 String instantString = request.getParameter(IFSConstants.ISSUE_INSTANT); 737 if (instantString == null || 738 instantString.length() == 0) { 739 String[] args = { IFSConstants.ISSUE_INSTANT }; 740 throw new FSMsgException("missingAttribute",args); 741 } 742 try{ 743 retFederationTerminationNotification.issueInstant = 744 DateUtils.stringToDate(instantString); 745 } catch (ParseException e){ 746 throw new FSMsgException("parseError",null); 747 } 748 749 String providerID = request.getParameter(IFSConstants.PROVIDER_ID); 750 if (providerID != null){ 751 retFederationTerminationNotification.providerId = providerID; 752 } else { 753 throw new FSMsgException("missingElement",null); 754 } 755 756 String nameFormat = request.getParameter(IFSConstants.NAME_FORMAT); 757 758 String nameQualifier = 759 request.getParameter(IFSConstants.NAME_QUALIFIER); 760 761 762 String name = request.getParameter("Name"); 763 if (name == null) { 764 throw new FSMsgException("missingNameIdentifier",null); 765 } 766 767 String relayState = request.getParameter(IFSConstants.RELAY_STATE); 768 if (relayState != null) { 769 retFederationTerminationNotification.relayState = relayState; 770 } 771 772 retFederationTerminationNotification.nameIdentifier = 773 new NameIdentifier(name, nameQualifier, nameFormat); 774 775 FSUtils.debug.message("Returning Termination Object"); 776 return retFederationTerminationNotification; 777 } 778 779 /** 780 * Sets the <code>MajorVersion</code> by parsing the version string. 781 * 782 * @param majorVer a String representing the <code>MajorVersion</code> to 783 * be set. 784 * @throws FSMsgException when the version mismatches. 785 */ 786 private void parseMajorVersion(String majorVer) throws FSMsgException { 787 try { 788 majorVersion = Integer.parseInt(majorVer); 789 } catch (NumberFormatException e) { 790 if (FSUtils.debug.messageEnabled()) { 791 FSUtils.debug.message( 792 "FSFederationTerminationNotification(Element): " 793 + "invalid MajorVersion", e); 794 } 795 throw new FSMsgException("wrongInput",null); 796 } 797 798 if (majorVersion != SAMLConstants.PROTOCOL_MAJOR_VERSION) { 799 if (majorVersion > SAMLConstants.PROTOCOL_MAJOR_VERSION) { 800 if (FSUtils.debug.messageEnabled()) { 801 FSUtils.debug.message( 802 "FSFederationTerminationNotification(Element): " 803 + "MajorVersion of the " 804 + "FederationTerminationNotification is too high."); 805 } 806 throw new FSMsgException("requestVersionTooHigh",null); 807 } else { 808 if (FSUtils.debug.messageEnabled()) { 809 FSUtils.debug.message( 810 "FSFederationTerminationNotification(Element): " 811 + "MajorVersion of the " 812 + "FederationTerminationNotification is too low."); 813 } 814 throw new FSMsgException("requestVersionTooLow",null); 815 } 816 } 817 } 818 819 /** 820 * Sets the <code>MinorVersion</code> by parsing the version string. 821 * 822 * @param minorVer a String representing the <code>MinorVersion</code> to 823 * be set. 824 * @throws SAMLException when the version mismatchs. 825 */ 826 private void parseMinorVersion(String minorVer) throws FSMsgException { 827 try { 828 minorVersion = Integer.parseInt(minorVer); 829 } catch (NumberFormatException e) { 830 if (FSUtils.debug.messageEnabled()) { 831 FSUtils.debug.message( 832 "FSFederationTerminationNotification(Element): " 833 + "invalid MinorVersion", e); 834 } 835 throw new FSMsgException("wrongInput",null); 836 } 837 838 if (minorVersion != IFSConstants.FF_12_PROTOCOL_MINOR_VERSION && 839 minorVersion != IFSConstants.FF_11_PROTOCOL_MINOR_VERSION) { 840 if (minorVersion > IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) { 841 FSUtils.debug.error("FSFedTerminationNot(Element):" 842 + " MinorVersion of the Response is too high."); 843 throw new FSMsgException("responseVersionTooHigh",null); 844 } else { 845 FSUtils.debug.error("FSFedTerminationNot(Element): " 846 + " MinorVersion of the Response is too low:" 847 + minorVersion); 848 throw new FSMsgException("responseVersionTooLow",null); 849 } 850 } 851 } 852 853 /** 854 * Unsupported operation. 855 */ 856 public void signXML() throws SAMLException { 857 throw new SAMLException(FSUtils.BUNDLE_NAME, 858 "unsupportedOperation",null); 859 } 860 861 /** 862 * Signs the <code>FSFederationTerminationNotification</code>. 863 * object 864 * 865 * @param certAlias the Certificate Alias 866 * @throws SAMLException if 867 * <code>FSFederationTerminationNotification</code> 868 * cannot be signed. 869 */ 870 public void signXML(String certAlias) throws SAMLException { 871 FSUtils.debug.message( 872 "FSFederationTerminationNotification.signXML: Called"); 873 if (signed) { 874 if (FSUtils.debug.messageEnabled()) { 875 FSUtils.debug.message( 876 "FSFederationTerminationNotification.signXML: " 877 + "the assertion is already signed."); 878 } 879 throw new SAMLResponderException(FSUtils.BUNDLE_NAME, 880 "alreadySigned",null); 881 } 882 if (certAlias == null || certAlias.length() == 0) { 883 throw new SAMLResponderException( 884 FSUtils.BUNDLE_NAME,"cannotFindCertAlias",null); 885 } 886 try{ 887 XMLSignatureManager manager = XMLSignatureManager.getInstance(); 888 if (minorVersion == IFSConstants.FF_11_PROTOCOL_MINOR_VERSION) { 889 signatureString = manager.signXML(this.toXMLString(true, true), 890 certAlias, (String) null, IFSConstants.ID, 891 this.id, false); 892 } else 893 if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) { 894 signatureString = 895 manager.signXML(this.toXMLString(true, true), 896 certAlias, (String) null, 897 IFSConstants.REQUEST_ID, 898 this.getRequestID(), false); 899 } else { 900 if (FSUtils.debug.messageEnabled()) { 901 FSUtils.debug.message("invalid minor version."); 902 } 903 } 904 905 signature = 906 XMLUtils.toDOMDocument(signatureString, FSUtils.debug) 907 .getDocumentElement(); 908 909 signed = true; 910 xmlString = this.toXMLString(true, true); 911 } catch(Exception e){ 912 throw new SAMLResponderException(FSUtils.BUNDLE_NAME, 913 "signFailed",null); 914 } 915 } 916 917 918 /** 919 * Sets the <code>Element</code> signature. 920 * 921 * @param elem the <code>Element</code> object 922 * @return true if signature is set otherwise false 923 */ 924 public boolean setSignature(Element elem) { 925 signatureString = XMLUtils.print(elem); 926 return super.setSignature(elem); 927 } 928}
Copyright © 2010-2017, ForgeRock All Rights Reserved.