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

org.modeshape.jcr.security.AuthenticationProviders Maven / Gradle / Ivy

There is a newer version: 5.4.1.Final
Show newest version
/*
 * ModeShape (http://www.modeshape.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modeshape.jcr.security;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.jcr.Credentials;
import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.ExecutionContext;
import org.modeshape.jcr.JcrI18n;

/**
 * An implementation of {@link AuthenticationProvider} that represents an ordered list of other {@link AuthenticationProvider}
 * implementations.
 */
public class AuthenticationProviders implements AuthenticationProvider {

    private final List providers;

    public AuthenticationProviders() {
        this.providers = new CopyOnWriteArrayList();
    }

    public AuthenticationProviders( List providers ) {
        this.providers = new CopyOnWriteArrayList(providers);
    }

    public AuthenticationProviders( AuthenticationProvider... providers ) {
        this.providers = new CopyOnWriteArrayList(providers);
    }

    @Override
    public ExecutionContext authenticate( Credentials credentials,
                                          String repositoryName,
                                          String workspaceName,
                                          ExecutionContext repositoryContext,
                                          Map sessionAttributes ) {
        ExecutionContext result = null;
        for (AuthenticationProvider provider : providers) {
            try {
                // The session attributes from prior, failed authenticators should be cleared ...
                sessionAttributes.clear();
                result = provider.authenticate(credentials, repositoryName, workspaceName, repositoryContext, sessionAttributes);
                if (result != null) return result;
            } catch (Exception e) {
                Logger.getLogger(AuthenticationProviders.class).error(e,
                                                                      JcrI18n.errorInAuthenticationProvider,
                                                                      provider.getClass().getName(),
                                                                      repositoryName,
                                                                      e.getMessage());
            }
        }
        return null;
    }

    public AuthenticationProviders with( AuthenticationProvider provider ) {
        List providers = new CopyOnWriteArrayList(this.providers);
        providers.add(provider);
        return new AuthenticationProviders(providers);
    }

    public static AuthenticationProviders with( AuthenticationProvider... providers ) {
        List providerList = new CopyOnWriteArrayList();
        for (AuthenticationProvider provider : providers) {
            if (provider != null) providerList.add(provider);
        }
        return new AuthenticationProviders(providerList);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy