<commit_before>/*
 * Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors.
 *
 * 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.switchyard.security.karaf.provider;

import org.apache.karaf.jaas.boot.ProxyLoginModule;
import org.switchyard.ServiceSecurity;
import org.switchyard.security.context.SecurityContext;
import org.switchyard.security.provider.DefaultSecurityProvider;

/**
 * KarafSecurityProvider.
 *
 * @author <NAME> &lt;<a href="mailto:<EMAIL>"><EMAIL></a>&gt; &copy; 2014 Red Hat Inc.
 */
public class KarafSecurityProvider extends DefaultSecurityProvider {

    static {
        // Here to trigger fallback usage of a different SecurityProvider (or DefaultSecurityProvider),
        // if the org.apache.karaf.jaas:org.apache.karaf.jaas.modules dependency is not available.
        new ProxyLoginModule();
    }

    /**
     * Constructs a new KarafSecurityProvider.
     */
    public KarafSecurityProvider() {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void populate(ServiceSecurity serviceSecurity, SecurityContext securityContext) {
        super.populate(serviceSecurity, securityContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear(ServiceSecurity serviceSecurity, SecurityContext securityContext) {
        super.clear(serviceSecurity, securityContext);
    }

}
<commit_msg>SWITCHYARD-2547 demo-policy-security-basic - Required policy "authorization" is not provided on karaf<commit_after>/*
 * Copyright 2015 JBoss Inc
 *
 * 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.switchyard.security.karaf.provider;

import java.security.Principal;
import java.util.Set;

import javax.security.auth.Subject;

import org.apache.karaf.jaas.boot.ProxyLoginModule;
import org.apache.karaf.jaas.boot.principal.GroupPrincipal;
import org.switchyard.ServiceSecurity;
import org.switchyard.security.context.SecurityContext;
import org.switchyard.security.provider.DefaultSecurityProvider;

// TODO: Auto-generated Javadoc
/**
 * KarafSecurityProvider.
 *
 * @author <NAME> &lt;<a href="mailto:<EMAIL>"><EMAIL></a>&gt; &copy; 2014 Red Hat Inc.
 */
public class KarafSecurityProvider extends DefaultSecurityProvider {

    static {
        // Here to trigger fallback usage of a different SecurityProvider (or DefaultSecurityProvider),
        // if the org.apache.karaf.jaas:org.apache.karaf.jaas.modules dependency is not available.
        new ProxyLoginModule();
    }

    /**
     * Constructs a new KarafSecurityProvider.
     */
    public KarafSecurityProvider() {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void populate(ServiceSecurity serviceSecurity, SecurityContext securityContext) {
        super.populate(serviceSecurity, securityContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear(ServiceSecurity serviceSecurity, SecurityContext securityContext) {
        super.clear(serviceSecurity, securityContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean checkRolesAllowed(ServiceSecurity serviceSecurity, SecurityContext securityContext) {
        Set<String> rolesAllowed = serviceSecurity.getRolesAllowed();
        if (rolesAllowed.isEmpty()) {
            return true;
        }
        String securityDomain = serviceSecurity.getSecurityDomain();
        for (String roleName : rolesAllowed) {
            boolean isInRole = this.isCallerInRole(securityContext, roleName, securityDomain);
            if (isInRole) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks if is caller in role.
     *
     * @param securityContext
     *            the security context
     * @param roleName
     *            the role name
     * @param securityDomain
     *            the security domain
     * @return true, if is caller in role
     */
    public boolean isCallerInRole(SecurityContext securityContext, String roleName, String securityDomain) {
        Subject subject = securityContext.getSubject(securityDomain, false);
        if (subject != null) {
            for (Principal principal : subject.getPrincipals()) {
                if (principal instanceof GroupPrincipal) {
                    if (principal.getName().equalsIgnoreCase(roleName)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

{'fraction_non_alphanumeric': 0.09036472509526401, 'fraction_numerical': 0.005080747595717656, 'mean_word_length': 2.8954063604240283, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5400968', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1408, 'n_words': 429}