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: Transform.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $ 026 * Portions Copyrighted 2014 ForgeRock AS. 027 */ 028 029 030package com.sun.identity.liberty.ws.authnsvc.protocol; 031 032import java.util.ArrayList; 033import java.util.HashMap; 034import java.util.Iterator; 035import java.util.List; 036import java.util.Map; 037import java.util.StringTokenizer; 038 039import org.w3c.dom.Document; 040import org.w3c.dom.Element; 041import org.w3c.dom.Node; 042import org.w3c.dom.NodeList; 043 044import com.sun.identity.shared.xml.XMLUtils; 045import com.sun.identity.shared.configuration.SystemPropertiesManager; 046import com.sun.identity.liberty.ws.authnsvc.AuthnSvcConstants; 047import com.sun.identity.liberty.ws.authnsvc.AuthnSvcException; 048import com.sun.identity.liberty.ws.authnsvc.AuthnSvcUtils; 049 050/** 051 * The <code>Transform</code> class represents 'Transform' element in 052 * 'PasswordTransforms' element defined in Authentication Service schema. 053 * 054 * @supported.all.api 055 * @deprecated since 12.0.0 056 */ 057@Deprecated 058public abstract class Transform { 059 060 /** 061 * Truncation Transform name. 062 */ 063 public static final String TRUNCATION_URI = "urn:liberty:sa:pw:truncate"; 064 065 /** 066 * Lowercase Transform name. 067 */ 068 public static final String LOWERCASE_URI = "urn:liberty:sa:pw:lowercase"; 069 070 /** 071 * Uppercase Transform name. 072 */ 073 public static final String UPPERCASE_URI = "urn:liberty:sa:pw:uppercase"; 074 075 /** 076 * Select Transform name. 077 */ 078 public static final String SELECT_URI = "urn:liberty:sa:pw:select"; 079 080 private static final String TRANSFORM_CLASSES = 081 "com.sun.identity.liberty.ws.authnsvc.transformClasses"; 082 083 private static Map transformClasses = new HashMap(); 084 085 protected String name = null; 086 protected String id = null; 087 protected List parameters = null; 088 089 static { 090 String tmpstr = SystemPropertiesManager.get(TRANSFORM_CLASSES); 091 if (tmpstr != null && tmpstr.length() > 0) { 092 StringTokenizer stz = new StringTokenizer(tmpstr, ","); 093 while(stz.hasMoreTokens()) { 094 String token = stz.nextToken().trim(); 095 int index = token.indexOf('|'); 096 if (index != -1 && index != 0 && index != token.length() - 1) { 097 String name = token.substring(0, index); 098 String className = token.substring(index + 1); 099 if (AuthnSvcUtils.debug.messageEnabled()) { 100 AuthnSvcUtils.debug.message( 101 "Transform.static: add " + token); 102 } 103 transformClasses.put(name, className); 104 } else { 105 if (AuthnSvcUtils.debug.warningEnabled()) { 106 AuthnSvcUtils.debug.warning( 107 "Transform.static: Invalid syntax " + 108 "for Transform Classes List: " + 109 token); 110 } 111 } 112 } 113 } 114 115 } 116 117 static Transform getTransform(Element element) throws AuthnSvcException { 118 119 String name = XMLUtils.getNodeAttributeValue(element, 120 AuthnSvcConstants.ATTR_NAME); 121 if (name == null || name.length() == 0) { 122 throw new AuthnSvcException("missingNameTF"); 123 } 124 125 Transform tf = null; 126 String className = (String)transformClasses.get(name); 127 if (className != null) { 128 try { 129 tf = (Transform)Class.forName(className).newInstance(); 130 } catch (Throwable t) { 131 if (AuthnSvcUtils.debug.warningEnabled()) { 132 AuthnSvcUtils.debug.warning( 133 "Transform.getTransform class = " + className, t); 134 } 135 136 transformClasses.remove(name); 137 } 138 } 139 140 if (tf == null) { 141 if (name.equals(TRUNCATION_URI)) { 142 143 tf = new TruncationTransform(); 144 } else if (name.equals(LOWERCASE_URI)) { 145 146 tf = new LowercaseTransform(); 147 } else if (name.equals(UPPERCASE_URI)) { 148 149 tf = new UppercaseTransform(); 150 } else if (name.equals(SELECT_URI)) { 151 152 tf = new SelectTransform(); 153 } else { 154 155 tf = new GenericTransform(name); 156 } 157 } 158 159 String id = XMLUtils.getNodeAttributeValue(element, 160 AuthnSvcConstants.ATTR_id); 161 tf.setId(id); 162 163 NodeList nl = element.getChildNodes(); 164 int length = nl.getLength(); 165 166 List parameters = null; 167 for(int i = 0; i < length; i++) { 168 Node child = nl.item(i); 169 if (child.getNodeType() == Node.ELEMENT_NODE) { 170 Element childElement = (Element)child; 171 String localName = childElement.getLocalName(); 172 String namespaceURI = childElement.getNamespaceURI(); 173 174 if (AuthnSvcConstants.NS_AUTHN_SVC.equals(namespaceURI) && 175 AuthnSvcConstants.TAG_PARAMETER.equals(localName)) { 176 177 Parameter parameter = new Parameter(childElement); 178 if (parameters == null) { 179 parameters = new ArrayList(); 180 } 181 parameters.add(parameter); 182 } else { 183 throw new AuthnSvcException("invalidChildTF"); 184 } 185 } 186 } 187 188 tf.setParameters(parameters); 189 190 return tf; 191 } 192 193 194 /** 195 * Transforms password. 196 * @param password original password 197 * @return transformed password 198 */ 199 public abstract String transform(String password); 200 201 /** 202 * Returns value of 'name' attribute. 203 * @return value of 'name' attribute 204 */ 205 public String getName() 206 { 207 return name; 208 } 209 210 /** 211 * Returns value of 'id' attribute. 212 * @return value of 'id' attribute 213 * @see #setId(String) 214 */ 215 public String getId() 216 { 217 return id; 218 } 219 220 /** 221 * Returns a list of 'Parameter' child element. 222 * @return a list of 'Parameter' child element 223 * @see #setParameters(List) 224 */ 225 public List getParameters() 226 { 227 return parameters; 228 } 229 230 /** 231 * Sets value of 'id' attribute. 232 * @param id value of 'id' attribute 233 * @see #getId() 234 */ 235 public void setId(String id) 236 { 237 this.id = id; 238 } 239 240 /** 241 * Sets a list of 'Parameter' child element. 242 * @param parameters a list of 'Parameter' child element 243 * @see #getParameters() 244 */ 245 public void setParameters(List parameters) 246 { 247 this.parameters = parameters; 248 } 249 250 /** 251 * Converts this to <code>org.w3c.dom.Element</code> and add to 252 * parent PasswordTransforms Element. 253 * @param ptE parent PasswordTransforms Element 254 * @exception AuthnSvcException if there is 'name' attribute is empty 255 */ 256 void addToParent(Element ptE) throws AuthnSvcException 257 { 258 if (name == null || name.length() == 0) { 259 throw new AuthnSvcException("missingNameTF"); 260 } 261 262 Document doc = ptE.getOwnerDocument(); 263 Element tfE = doc.createElementNS( 264 AuthnSvcConstants.NS_AUTHN_SVC, 265 AuthnSvcConstants.PTAG_TRANSFORM); 266 ptE.appendChild(tfE); 267 268 tfE.setAttributeNS(null, AuthnSvcConstants.ATTR_NAME, name); 269 270 if (id != null) { 271 tfE.setAttributeNS(null, AuthnSvcConstants.ATTR_id, id); 272 } 273 274 if (parameters != null && !parameters.isEmpty()) { 275 for(Iterator iter = parameters.iterator(); iter.hasNext(); ) { 276 Parameter parameter = (Parameter)iter.next(); 277 parameter.addToParent(tfE); 278 } 279 } 280 281 } 282}