8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: AuthenticationServiceNameProviderImpl.java,v 1.3 2008/06/25 05:44:03 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.sm;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.util.ISAuthConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.security.AdminTokenAction;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.security.AccessController;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A concrete implementation of <code>AuthenticationServiceNameProvider</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * that uses the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.authentication.config.AMAuthenticationManager</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to retrieve the names of authentication module services that are loaded by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * default.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class AuthenticationServiceNameProviderImpl implements
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthenticationServiceNameProvider {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean initialized;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static HashSet authNmodules = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Debug debug = SMSEntry.debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Provides a collection of authentication module service names that are
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * loaded by default. This implementation uses the authentication
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service specific configuration to retrieve the relevant
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * module service name information.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return a <code>Set</code> of authentication module service names.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getAuthenticationServiceNames() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (initialized) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return authNmodules;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOToken token = (SSOToken) AccessController.doPrivileged(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AdminTokenAction.getInstance());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchemaManager scm = new ServiceSchemaManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ISAuthConstants.AUTH_SERVICE_NAME, token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchema schema = scm.getGlobalSchema();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set authenticators = (Set) schema.getAttributeDefaults().get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ISAuthConstants.AUTHENTICATORS);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator it = authenticators.iterator(); it.hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String module = (String) it.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int index = module.lastIndexOf(".");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (index != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module = module.substring(index + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceName = "iPlanetAMAuth" + module + "Service";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if the service name exisits with organization schema
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchemaManager ssm = new ServiceSchemaManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceName, token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ssm.getOrganizationSchema() != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster authNmodules.add(serviceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Try with "sunAMAuth"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceName = "sunAMAuth" + module + "Service";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchemaManager ssm = new ServiceSchemaManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceName, token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ssm.getOrganizationSchema() != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster authNmodules.add(serviceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception ee) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Ignore the Exception and donot add to authmodules
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // 1) Service does not exisit
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // 2) OrganizationSchema does not exisit
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialized = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("AuthenticationServiceNameProviderImpl error", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("AuthenticationServiceNameProviderImpl error", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return authNmodules;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}