001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.identity.profile.api.configurator.impl.dynamic;
043:
044: import java.lang.reflect.Constructor;
045: import java.lang.reflect.Method;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Vector;
052: import org.netbeans.modules.identity.profile.api.configurator.ConfiguratorException;
053: import org.netbeans.modules.identity.profile.api.configurator.SecurityMechanismHelper;
054: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
055: import org.netbeans.modules.identity.profile.api.configurator.spi.ProviderConfig;
056: import org.netbeans.modules.identity.profile.api.configurator.spi.TrustAuthorityConfig;
057:
058: /**
059: * Dynamic implementation class for the ProviderConfig interface.
060: *
061: * Created on June 9, 2006, 7:59 PM
062: *
063: * @author ptliu
064: */
065: public class ProviderConfigImpl implements ProviderConfig {
066: private static final String WSP = "WSP"; //NOI18N
067:
068: private static final String AM_PROVIDER_CONFIG_CLASS = "com.sun.identity.wss.provider.ProviderConfig"; //NOI18N
069:
070: private static final String AM_PASSWORD_CREDENTIAL_CLASS = "com.sun.identity.wss.security.PasswordCredential"; //NOI18N
071:
072: private static final String AM_GET_PROVIDER_METHOD = "getProvider"; //NOI18N
073:
074: private static final String AM_IS_RESPONSE_SIGN_ENABLED_METHOD = "isResponseSignEnabled"; //NOI18N
075:
076: private static final String AM_SET_RESPONSE_SIGN_ENABLED_METHOD = "setResponseSignEnabled"; //NOI18N
077:
078: private static final String AM_GET_KEY_ALIAS_METHOD = "getKeyAlias"; //NOI18N
079:
080: private static final String AM_SET_KEY_ALIAS_METHOD = "setKeyAlias"; //NOI18N
081:
082: private static final String AM_GET_KEY_PASSWORD_METHOD = "getKeyPassword"; //NOI18N
083:
084: private static final String AM_GET_KEY_STORE_FILE_METHOD = "getKeyStoreFile"; //NOI18N
085:
086: private static final String AM_GET_KEY_STORE_PASSWORD_METHOD = "getKeyStorePassword"; //NOI18N
087:
088: private static final String AM_SET_KEY_STORE_METHOD = "setKeyStore"; //NOI18N
089:
090: private static final String AM_GET_PROPERTY_METHOD = "getProperty"; //NOI18N
091:
092: private static final String AM_SET_PROPERTY_METHOD = "setProperty"; //NOI18N
093:
094: private static final String AM_GET_SECURITY_MECHANISMS_METHOD = "getSecurityMechanisms"; //NOI18N
095:
096: private static final String AM_SET_SECURITY_MECHANISMS_METHOD = "setSecurityMechanisms"; //NOI18N
097:
098: private static final String AM_GET_ALL_SUPPORT_SECURITY_MECH_METHOD = "getAllSupportedSecurityMech"; //NOI18N
099:
100: private static final String AM_GET_ALL_MESSAGE_LEVEL_SECURITY_MECH_METHOD = "getAllMessageLevelSecurityMech"; //NOI18N
101:
102: private static final String AM_SAVE_PROVIDER_METHOD = "saveProvider"; //NOI18N
103:
104: private static final String AM_DELETE_PROVIDER_METHOD = "deleteProvider"; //NOI18N
105:
106: private static final String AM_GET_WSP_ENDPOINT_METHOD = "getWSPEndpoint"; //NOI18N
107:
108: private static final String AM_SET_WSP_ENDPOINT_METHOD = "setWSPEndpoint"; //NOI18N
109:
110: private static final String AM_IS_PROVIDER_EXISTS_METHOD = "isProviderExists"; //NOI18N
111:
112: private static final String AM_GET_USERS_METHOD = "getUsers"; //NOI18N
113:
114: private static final String AM_SET_USERS_METHOD = "setUsers"; //NOI18N
115:
116: private static final String AM_GET_USERNAME_METHOD = "getUserName"; //NOI18N
117:
118: private static final String AM_GET_PASSWORD_METHOD = "getPassword"; //NOI18N
119:
120: private static final String AM_SET_SERVICE_TYPE_METHOD = "setServiceType"; //NOI18N
121:
122: private static final String AM_GET_SERVICE_TYPE_METHOD = "getServiceType"; //NOI18N
123:
124: private static final String AM_SET_DEFAULT_KEY_STORE_METHOD = "setDefaultKeyStore"; //NOI18N
125:
126: private static final String AM_USE_DEFAULT_KEY_STORE_METHOD = "useDefaultKeyStore"; //NOI18N
127:
128: private static final String AM_SET_TRUST_AUTHORITY_CONFIG_LIST_METHOD = "setTrustAuthorityConfigList"; //NOI18N
129:
130: private static final String DEFAULT_RELATIVE_KEYSTORE_LOCATION = "/domains/domain1/config/keystore.jks"; //NOI18N
131:
132: private static final String DEFAULT_KEYSTORE_PASSWORD = "adminadmin"; //NOI18N
133:
134: private static final String DEFAULT_KEY_ALIAS = "s1as"; //NOI18N
135:
136: private static final String AM_CONFIG_FILE_RELATIVE_PATH = "/addons/amserver/AMConfig.properties"; //NOI18N
137:
138: private static final String DEFAULT_APPSERVER_LOCATION = "{sjsas.root}"; //NOI18N
139:
140: private Class providerConfigClass;
141: private Class passwordCredentialClass;
142: private Object providerConfig;
143: private String providerName;
144: private String providerType;
145: private ServerProperties properties;
146: private SecurityMechanismHelper secMechHelper;
147:
148: /**
149: * Creates a new instance of ProviderConfigImpl
150: */
151: public ProviderConfigImpl(String providerName, String type,
152: ServerProperties properties) {
153: this .providerName = providerName;
154: this .providerType = type;
155: this .properties = properties;
156: this .secMechHelper = new SecurityMechanismHelper(properties
157: .getProperty(ServerProperties.PROP_ID));
158:
159: //
160: // Initialize providerConfig now so we can report
161: // any exception early on.
162: //
163: getProviderConfig();
164: }
165:
166: Class getProviderConfigClass() {
167: if (providerConfigClass == null) {
168: try {
169: ClassLoader loader = ClassLoaderManager.getDefault()
170: .getClassLoader(properties);
171: providerConfigClass = loader
172: .loadClass(AM_PROVIDER_CONFIG_CLASS);
173: } catch (Exception ex) {
174: throw ConfiguratorException.create(ex);
175: }
176: }
177:
178: return providerConfigClass;
179: }
180:
181: Object getProviderConfig() {
182: if (providerConfig == null) {
183: try {
184: Method method = getProviderConfigClass().getMethod(
185: AM_GET_PROVIDER_METHOD, String.class,
186: String.class);
187: providerConfig = method.invoke(null, providerName,
188: providerType);
189:
190: //System.out.println("providerConfig = " + providerConfig);
191:
192: //
193: // For WSP profiles, we need to create it if it
194: // does not exist and add the appropriate security mechanism
195: // uri.
196: //
197: if (providerType.equals(WSP) && !isProviderExists()) {
198: createProvider();
199: }
200: } catch (Exception ex) {
201: ClassLoaderManager.getDefault().removeClassLoader(
202: properties);
203: throw ConfiguratorException.create(ex);
204: }
205: }
206:
207: return providerConfig;
208: }
209:
210: private void createProvider() {
211: Collection<String> names = new ArrayList<String>();
212: names.add(providerName);
213: Collection<String> uris = secMechHelper
214: .getSecurityMechanismURIsFromNames(names);
215: setSecurityMechanisms(uris);
216: setDefaultKeyStore(true);
217:
218: /*
219: String amConfigFile = properties.getProperty(ServerProperties.PROP_AM_CONFIG_FILE);
220:
221: // Convert to use forward slash.
222: amConfigFile = amConfigFile.replace('\\', '/');
223:
224: String appServerLocation = DEFAULT_APPSERVER_LOCATION;
225:
226: if (properties.isDefault()) {
227: int index = amConfigFile.indexOf(AM_CONFIG_FILE_RELATIVE_PATH);
228:
229: if (index != -1) {
230: appServerLocation = amConfigFile.substring(0, index);
231: }
232: }
233:
234:
235: setKeyStore(appServerLocation + DEFAULT_RELATIVE_KEYSTORE_LOCATION,
236: DEFAULT_KEYSTORE_PASSWORD, null);
237: setKeyAlias(DEFAULT_KEY_ALIAS);
238: */
239:
240: saveProvider();
241:
242: // Need to recreate the providerConfig after saving.
243: providerConfig = null;
244: getProviderConfig();
245: }
246:
247: public boolean isResponseSignEnabled() {
248: try {
249: Method method = getProviderConfigClass().getMethod(
250: AM_IS_RESPONSE_SIGN_ENABLED_METHOD);
251: return ((Boolean) method.invoke(getProviderConfig()))
252: .booleanValue();
253: } catch (Exception ex) {
254: throw ConfiguratorException.create(ex);
255: }
256: }
257:
258: public void setResponseSignEnabled(boolean flag) {
259: try {
260: Method method = getProviderConfigClass().getMethod(
261: AM_SET_RESPONSE_SIGN_ENABLED_METHOD, Boolean.TYPE);
262: method.invoke(getProviderConfig(), flag);
263: } catch (Exception ex) {
264: throw ConfiguratorException.create(ex);
265: }
266: }
267:
268: public String getKeyAlias() {
269: try {
270: Method method = getProviderConfigClass().getMethod(
271: AM_GET_KEY_ALIAS_METHOD);
272: return (String) method.invoke(getProviderConfig());
273: } catch (Exception ex) {
274: throw ConfiguratorException.create(ex);
275: }
276: }
277:
278: public void setKeyAlias(String keyAlias) {
279: try {
280: Method method = getProviderConfigClass().getMethod(
281: AM_SET_KEY_ALIAS_METHOD, String.class);
282: method.invoke(getProviderConfig(), keyAlias);
283: } catch (Exception ex) {
284: throw ConfiguratorException.create(ex);
285: }
286: }
287:
288: public String getKeyPassword() {
289: try {
290: Method method = getProviderConfigClass().getMethod(
291: AM_GET_KEY_PASSWORD_METHOD);
292: return (String) method.invoke(getProviderConfig());
293: } catch (Exception ex) {
294: throw ConfiguratorException.create(ex);
295: }
296: }
297:
298: public String getKeyStoreFile() {
299: try {
300: Method method = getProviderConfigClass().getMethod(
301: AM_GET_KEY_STORE_FILE_METHOD);
302: return (String) method.invoke(getProviderConfig());
303: } catch (Exception ex) {
304: throw ConfiguratorException.create(ex);
305: }
306: }
307:
308: public String getKeyStorePassword() {
309: try {
310: Method method = getProviderConfigClass().getMethod(
311: AM_GET_KEY_STORE_PASSWORD_METHOD);
312: return (String) method.invoke(getProviderConfig());
313: } catch (Exception ex) {
314: throw ConfiguratorException.create(ex);
315: }
316: }
317:
318: public void setKeyStore(String location, String password,
319: String keyPassword) {
320: try {
321: Method method = getProviderConfigClass().getMethod(
322: AM_SET_KEY_STORE_METHOD, String.class,
323: String.class, String.class);
324: method.invoke(getProviderConfig(), location, password,
325: keyPassword);
326: } catch (Exception ex) {
327: //ex.printStackTrace();
328: throw ConfiguratorException.create(ex);
329: }
330: }
331:
332: public Object getProperty(String propName) {
333: try {
334: Method method = getProviderConfigClass().getMethod(
335: AM_GET_PROPERTY_METHOD, String.class);
336: return method.invoke(getProviderConfig(), propName);
337: } catch (Exception ex) {
338: throw ConfiguratorException.create(ex);
339: }
340: }
341:
342: public void setProperty(String propName, Object value) {
343: try {
344: Method method = getProviderConfigClass().getMethod(
345: AM_SET_PROPERTY_METHOD, String.class, Object.class);
346: method.invoke(getProviderConfig(), propName, value);
347: } catch (Exception ex) {
348: throw ConfiguratorException.create(ex);
349: }
350: }
351:
352: public Collection<String> getSecurityMechanisms() {
353: try {
354: Method method = getProviderConfigClass().getMethod(
355: AM_GET_SECURITY_MECHANISMS_METHOD);
356: return (Collection<String>) method
357: .invoke(getProviderConfig());
358: } catch (Exception ex) {
359: throw ConfiguratorException.create(ex);
360: }
361: }
362:
363: public void setSecurityMechanisms(Collection<String> securityMechs) {
364: try {
365: Method method = getProviderConfigClass().getMethod(
366: AM_SET_SECURITY_MECHANISMS_METHOD, List.class);
367: method.invoke(getProviderConfig(), new ArrayList(
368: securityMechs));
369: } catch (Exception ex) {
370: throw ConfiguratorException.create(ex);
371: }
372: }
373:
374: public void saveProvider() {
375: try {
376: Method method = getProviderConfigClass().getMethod(
377: AM_SAVE_PROVIDER_METHOD, getProviderConfigClass());
378: method.invoke(null, getProviderConfig());
379: } catch (Exception ex) {
380: throw ConfiguratorException.create(ex);
381: }
382: }
383:
384: public void deleteProvider() {
385: try {
386: Method method = getProviderConfigClass().getMethod(
387: AM_DELETE_PROVIDER_METHOD, String.class);
388: method.invoke(null, providerName);
389: } catch (Exception ex) {
390: throw ConfiguratorException.create(ex);
391: }
392: }
393:
394: public String getWSPEndpoint() {
395: try {
396: Method method = getProviderConfigClass().getMethod(
397: AM_GET_WSP_ENDPOINT_METHOD);
398: return (String) method.invoke(getProviderConfig());
399: } catch (Exception ex) {
400: throw ConfiguratorException.create(ex);
401: }
402: }
403:
404: public void setWSPEndpoint(String endpoint) {
405: try {
406: Method method = getProviderConfigClass().getMethod(
407: AM_SET_WSP_ENDPOINT_METHOD, String.class);
408: method.invoke(getProviderConfig(), endpoint);
409: } catch (Exception ex) {
410: throw ConfiguratorException.create(ex);
411: }
412: }
413:
414: public boolean isProviderExists() {
415: try {
416: Method method = getProviderConfigClass().getMethod(
417: AM_IS_PROVIDER_EXISTS_METHOD, String.class,
418: String.class);
419: return (Boolean) method.invoke(null, providerName,
420: providerType);
421: } catch (Exception ex) {
422: throw ConfiguratorException.create(ex);
423: }
424:
425: }
426:
427: public ServerProperties getServerProperties(String id) {
428: return properties;
429: }
430:
431: public void setServerProperties(ServerProperties properties) {
432: // TODO: need to reinitialize everything
433: }
434:
435: public String getUserName() {
436: return null;
437: }
438:
439: public void setUserName(String userName) {
440: }
441:
442: public String getPassword() {
443: return null;
444: }
445:
446: public void setPassword(String password) {
447: }
448:
449: public void setUserNamePasswordPairs(
450: Collection<Vector<String>> pairs) {
451: List credentials = convertToPasswordCredentials(pairs);
452:
453: try {
454: Method method = getProviderConfigClass().getMethod(
455: AM_SET_USERS_METHOD, List.class);
456: method.invoke(getProviderConfig(), credentials);
457: } catch (Exception ex) {
458: throw ConfiguratorException.create(ex);
459: }
460: }
461:
462: public Collection<Vector<String>> getUserNamePasswordPairs() {
463: try {
464: Method method = getProviderConfigClass().getMethod(
465: AM_GET_USERS_METHOD);
466: List credentials = (List) method
467: .invoke(getProviderConfig());
468:
469: return convertToUserNamePasswordPairs(credentials);
470: } catch (Exception ex) {
471: throw ConfiguratorException.create(ex);
472: }
473: }
474:
475: private Class getPasswordCredentialClass() {
476: if (passwordCredentialClass == null) {
477: try {
478: ClassLoader loader = ClassLoaderManager.getDefault()
479: .getClassLoader(properties);
480: passwordCredentialClass = loader
481: .loadClass(AM_PASSWORD_CREDENTIAL_CLASS);
482: } catch (Exception ex) {
483: throw ConfiguratorException.create(ex);
484: }
485: }
486: return passwordCredentialClass;
487: }
488:
489: private List convertToPasswordCredentials(
490: Collection<Vector<String>> pairs) {
491: List credentials = new ArrayList();
492:
493: for (Vector<String> pair : pairs) {
494: credentials.add(convertToPasswordCredential(pair));
495: }
496:
497: return credentials;
498: }
499:
500: private Object convertToPasswordCredential(Vector<String> pair) {
501: try {
502: Constructor constructor = getPasswordCredentialClass()
503: .getConstructor(String.class, String.class);
504:
505: String userName = pair.get(0);
506:
507: //
508: // Empty user name exposes a security leak on the UserNameToken
509: // profile in the AM authentication provider. Appending an empty
510: // space to plug the hole.
511: //
512: if (userName == null || userName.length() == 0) {
513: userName = " "; //NOI18N
514: }
515:
516: return constructor.newInstance(userName, pair.get(1));
517: } catch (Exception ex) {
518: throw ConfiguratorException.create(ex);
519: }
520: }
521:
522: private Collection<Vector<String>> convertToUserNamePasswordPairs(
523: List credentials) {
524: Collection pairs = new Vector<Vector<String>>();
525:
526: if (credentials != null) {
527: Iterator iter = credentials.iterator();
528:
529: while (iter.hasNext()) {
530: pairs.add(convertToUserNamePasswordPair(iter.next()));
531: }
532: }
533:
534: return pairs;
535: }
536:
537: private Vector<String> convertToUserNamePasswordPair(
538: Object credential) {
539: try {
540: Method method = getPasswordCredentialClass().getMethod(
541: AM_GET_USERNAME_METHOD);
542: String userName = (String) method.invoke(credential);
543: method = getPasswordCredentialClass().getMethod(
544: AM_GET_PASSWORD_METHOD);
545: String password = (String) method.invoke(credential);
546:
547: Vector<String> pair = new Vector<String>();
548: pair.add(userName);
549: pair.add(password);
550:
551: return pair;
552: } catch (Exception ex) {
553: throw ConfiguratorException.create(ex);
554: }
555: }
556:
557: /**
558: * This is currently not support by amclientsdk.
559: *
560: */
561: public Collection<String> getAllProviderNames() {
562: // simply return an empty list.
563: return Collections.emptyList();
564: }
565:
566: public void setServiceType(String serviceType) {
567: try {
568: Method method = getProviderConfigClass().getMethod(
569: AM_SET_SERVICE_TYPE_METHOD, String.class);
570: method.invoke(getProviderConfig(), serviceType);
571: } catch (Exception ex) {
572: throw ConfiguratorException.create(ex);
573: }
574: }
575:
576: public String getServiceType() {
577: try {
578: Method method = getProviderConfigClass().getMethod(
579: AM_GET_SERVICE_TYPE_METHOD);
580: return (String) method.invoke(getProviderConfig());
581: } catch (Exception ex) {
582: throw ConfiguratorException.create(ex);
583: }
584: }
585:
586: public void setDefaultKeyStore(boolean flag) {
587: try {
588: Method method = getProviderConfigClass().getMethod(
589: AM_SET_DEFAULT_KEY_STORE_METHOD, Boolean.TYPE);
590: method.invoke(getProviderConfig(), flag);
591: } catch (Exception ex) {
592: throw ConfiguratorException.create(ex);
593: }
594:
595: }
596:
597: public boolean useDefaultKeyStore() {
598: try {
599: Method method = getProviderConfigClass().getMethod(
600: AM_USE_DEFAULT_KEY_STORE_METHOD);
601: return ((Boolean) method.invoke(getProviderConfig()))
602: .booleanValue();
603: } catch (Exception ex) {
604: throw ConfiguratorException.create(ex);
605: }
606: }
607:
608: public void setTrustAuthorityConfigList(
609: List<TrustAuthorityConfig> trustAuthConfigs) {
610: List list = new ArrayList();
611:
612: for (TrustAuthorityConfig config : trustAuthConfigs) {
613: TrustAuthorityConfigImpl configImpl = (TrustAuthorityConfigImpl) config;
614: list.add(configImpl.getTrustAuthorityConfig());
615: }
616:
617: try {
618: Method method = getProviderConfigClass().getMethod(
619: AM_SET_TRUST_AUTHORITY_CONFIG_LIST_METHOD);
620: method.invoke(getProviderConfig(), list);
621: } catch (Exception ex) {
622: throw ConfiguratorException.create(ex);
623: }
624: }
625:
626: public void close() {
627:
628: }
629: }
|