<commit_before>/*
 * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
 * distributed with this file and available online at
 * http://www.ja-sig.org/products/cas/overview/license/
 */
package org.jasig.cas.adaptors.x509.authentication.principal;

import javax.validation.constraints.NotNull;
import java.security.cert.X509Certificate;

import java.util.Arrays;
import java.util.Comparator;

/**
 * The following class is deprecated in favor of
 * {@link X509CertificateCredentialsToSubjectPrinciplalResolver}.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @version $Revision$ $Date$
 * @since 3.0.4
 */
@Deprecated
public final class X509CertificateCredentialsToIdentifierPrincipalResolver extends
    AbstractX509CertificateCredentialsToPrincipalResolver {

    private static final String DEFAULT_IDENTIFIER = "$OU $CN";

    private static final String ENTRIES_DELIMITER = ",";

    private static final String NAME_VALUE_PAIR_DELIMITER = "=";

    /** The identifier meta data */
    @NotNull
    private String identifier = DEFAULT_IDENTIFIER;

    protected String resolvePrincipalInternal(
        final X509Certificate certificate) {
        String username = this.identifier;
        
        if (log.isInfoEnabled()) {
            log.info("Creating principal for: " + certificate.getSubjectDN().getName());
        }

        final String[] entries = certificate.getSubjectDN().getName().split(ENTRIES_DELIMITER);

        //[fix by Barry Silk]
        // Make sure entries are sorted by length, in descending order
        // This is to prevent a substition of a shorter length descriptor
        // e.g., $CN must get replaced prior to $C
        Arrays.sort(entries, new LengthComparator());
        
        for (final String val : entries) {
            final String[] nameValuePair = val.split(NAME_VALUE_PAIR_DELIMITER);
            final String name = nameValuePair[0].trim();
            final String value = nameValuePair[1];

            if (log.isDebugEnabled()) {
                log.debug("Parsed " + name + " - " + value);
            }

            username = username.replaceAll("\\$" + name, value);
        }
        
        if (this.identifier.equals(username)) {
            return null;
        }

        return username;
    }
    
    public void setIdentifier(final String identifier) {
        this.identifier = identifier;
    }
    
    //[fix by <NAME>, see above]
    class LengthComparator implements Comparator<String> {
        public int compare(final String s1, final String s2) {
            final String[] nameValuePair1 = s1.split(NAME_VALUE_PAIR_DELIMITER);
            final String name1 = nameValuePair1[0].trim();
            final String[] nameValuePair2 = s2.split(NAME_VALUE_PAIR_DELIMITER);
            final String name2 = nameValuePair2[0].trim();
            final int len1 = name1.length();
            final int len2 = name2.length();
            if (len1 > len2) {
                return -1;
            }
            if (len2 > len1) {
                return 1;
            }
            return 0;
        }
    }

}<commit_msg>CAS-749<commit_after>/*
 * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
 * distributed with this file and available online at
 * http://www.ja-sig.org/products/cas/overview/license/
 */
package org.jasig.cas.adaptors.x509.authentication.principal;

import javax.validation.constraints.NotNull;
import java.security.cert.X509Certificate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The following class is deprecated in favor of
 * {@link X509CertificateCredentialsToSubjectPrinciplalResolver}.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @version $Revision$ $Date$
 * @since 3.0.4
 */
@Deprecated
public final class X509CertificateCredentialsToIdentifierPrincipalResolver extends AbstractX509CertificateCredentialsToPrincipalResolver {

    private static final String DEFAULT_IDENTIFIER = "$OU $CN";

    private final Pattern subjectRegex = Pattern.compile("([A-Z]+)=(?:\"(.+)\"|([\\w ]+))", 74);

    /** The identifier meta data */
    @NotNull
    private String identifier = DEFAULT_IDENTIFIER;

    protected String resolvePrincipalInternal(final X509Certificate certificate) {
        String username = this.identifier;
        
        if (log.isInfoEnabled()) {
            log.info("Creating principal for: " + certificate.getSubjectDN().getName());
        }

        for (final Matcher regexMatcher = this.subjectRegex.matcher(certificate.getSubjectDN().getName()); regexMatcher.find();) {
            final String name = regexMatcher.group(1).trim();
            final String value;

            if(regexMatcher.group(2) != null) {
                value = regexMatcher.group(2);
            } else {
                value = regexMatcher.group(3);
            }

            if (log.isDebugEnabled()) {
                log.debug(String.format("Parsed: %s - %s", name, value));
	        }

            username = username.replaceAll((new StringBuilder("\\$")).append(name).toString(), value);
        }
        
        if (this.identifier.equals(username)) {
            return null;
        }

        return username;
    }
    
    public void setIdentifier(final String identifier) {
        this.identifier = identifier;
    }
}
{'fraction_non_alphanumeric': 0.1085575485799701, 'fraction_numerical': 0.01569506726457399, 'mean_word_length': 2.5615435795076515, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '5050050', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1417, 'n_words': 369}