All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.gjc.util.SecurityUtils Maven / Gradle / Ivy

There is a newer version: 7.2024.1.Alpha1
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.gjc.util;

import com.sun.enterprise.util.i18n.StringManager;
import com.sun.gjc.common.DataSourceObjectBuilder;
import com.sun.gjc.spi.ConnectionRequestInfoImpl;

import jakarta.resource.ResourceException;
import jakarta.resource.spi.ManagedConnectionFactory;
import jakarta.resource.spi.security.PasswordCredential;
import javax.security.auth.Subject;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

/**
 * SecurityUtils for Generic JDBC Connector.
 *
 * @author Evani Sai Surya Kiran
 * @version 1.0, 02/07/22
 */
public class SecurityUtils {

    static private StringManager sm = StringManager.getManager(
            DataSourceObjectBuilder.class);

    /**
     * This method returns the PasswordCredential object, given
     * the ManagedConnectionFactory, subject and the
     * ConnectionRequestInfo. It first checks if the
     * ConnectionRequestInfo is null or not. If it is not null,
     * it constructs a PasswordCredential object with
     * the user and password fields from the ConnectionRequestInfo and returns this
     * PasswordCredential object. If the ConnectionRequestInfo
     * is null, it retrieves the PasswordCredential objects from
     * the Subject parameter and returns the first
     * PasswordCredential object which contains a
     * ManagedConnectionFactory, instance equivalent
     * to the ManagedConnectionFactory, parameter.
     *
     * @param mcf     ManagedConnectionFactory
     * @param subject Subject
     * @param info    ConnectionRequestInfo
     * @return PasswordCredential
     * @throws ResourceException generic exception if operation fails
     * @throws SecurityException if access to the Subject instance is denied
     */
    public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf,
                                                           final Subject subject, jakarta.resource.spi.ConnectionRequestInfo info) throws ResourceException {

        if (info == null) {
            if (subject == null) {
                return null;
            } else {
                PasswordCredential pc = (PasswordCredential) AccessController.doPrivileged
                        (new PrivilegedAction() {
                            public Object run() {
                                Set passwdCredentialSet = subject.getPrivateCredentials(PasswordCredential.class);
                                Iterator iter = passwdCredentialSet.iterator();
                                while (iter.hasNext()) {
                                    PasswordCredential temp = (PasswordCredential) iter.next();
                                    if (temp.getManagedConnectionFactory().equals(mcf)) {
                                        return temp;
                                    }
                                }
                                return null;
                            }
                        });
                if (pc == null) {
                    String msg = sm.getString("su.no_passwd_cred");
                    throw new jakarta.resource.spi.SecurityException(msg);
                } else {
                    return pc;
                }
            }
        } else {
            ConnectionRequestInfoImpl cxReqInfo = (ConnectionRequestInfoImpl) info;
            PasswordCredential pc = new PasswordCredential(cxReqInfo.getUser(), cxReqInfo.getPassword());
            pc.setManagedConnectionFactory(mcf);
            return pc;
        }
    }

    /**
     * Returns true if two strings are equal; false otherwise
     *
     * @param str1 String
     * @param str2 String
     * @return true    if the two strings are equal
     *         false	otherwise
     */
    static private boolean isEqual(String str1, String str2) {
        if (str1 == null) {
            return (str2 == null);
        } else {
            return str1.equals(str2);
        }
    }

    /**
     * Returns true if two PasswordCredential objects are equal; false otherwise
     *
     * @param pC1 PasswordCredential
     * @param pC2 PasswordCredential
     * @return true    if the two PasswordCredentials are equal
     *         false	otherwise
     */
    static public boolean isPasswordCredentialEqual(PasswordCredential pC1, PasswordCredential pC2) {
        if (pC1 == pC2)
            return true;
        if (pC1 == null || pC2 == null)
            return (pC1 == pC2);
        if (!isEqual(pC1.getUserName(), pC2.getUserName())) {
            return false;
        }
        return Arrays.equals(pC1.getPassword(), pC2.getPassword());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy