/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */

// https://issues.jboss.org/browse/TEIID-2507
// https://issues.redhat.com/browse/TEIID-3663
package org.teiid.runtime;

import java.security.Principal;
import java.util.List;
import javax.security.auth.login.LoginException;

import org.teiid.security.Credentials;
import org.teiid.security.GSSResult;
import org.teiid.security.SecurityHelper;
import org.teiid.security.TeiidLoginContext;
import org.wildfly.security.auth.principal.NamePrincipal;
import org.wildfly.security.auth.realm.SimpleMapBackedSecurityRealm;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityIdentity;

/**
 * A {@link SecurityHelper} that does nothing and always assumes that thread
 * has the proper security context.
 */
public class DoNothingSecurityHelper implements SecurityHelper {

	private SecurityDomain domain = SecurityDomain.builder()
			.addRealm("Simple", new SimpleMapBackedSecurityRealm()).build()
			.setDefaultRealmName("Simple")
			.build();

	@Override
	public SecurityDomain getSecurityDomainContext(String securityDomain) {
		return domain;
	}

	@Override
	public boolean sameSubject(String securityDomain, SecurityIdentity subject) {
		return true;
	}

	@Override
	public SecurityIdentity getSecurityIdentityInContext(String securityDomain) {
		return domain.getCurrentSecurityIdentity();
	}

	@Override
    public TeiidLoginContext authenticate(String securityDomain, String baseUserName, Credentials credentials, String applicationName)
            throws LoginException {
		return new TeiidLoginContext(baseUserName+"@"+securityDomain, createAdHocIdentity(securityDomain, baseUserName, null), securityDomain);
	}

	@Override
	public SecurityIdentity createAdHocIdentity(String securityDomain, Principal principal, List<String> roles) {
		return domain.createAdHocIdentity(principal);
	}

	@Override
	public SecurityIdentity createAdHocIdentity(String securityDomain, String username, List<String> roles) {
		return createAdHocIdentity(securityDomain, new NamePrincipal(username), roles);
    }

    @Override
    public GSSResult neogitiateGssLogin(String securityDomain, byte[] serviceTicket, String krbSecurityFactory) throws LoginException {
		return null;
	}
}