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

org.jivesoftware.util.cert.CNCertificateIdentityMapping Maven / Gradle / Ivy

The newest version!
package org.jivesoftware.util.cert;

import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Certificate identity mapping that uses the CommonName as the
 * identity credentials
 * 
 * @author Victor Hong
 *
 */
public class CNCertificateIdentityMapping implements CertificateIdentityMapping {

    private static Pattern cnPattern = Pattern.compile("(?i)(cn=)([^,]*)");
    
    /**
     * Maps certificate CommonName as identity credentials
     * 
     * @param certificate
     * @return A List of names.
     */
    @Override
    public List mapIdentity(X509Certificate certificate) {
        String name = certificate.getSubjectDN().getName();
        Matcher matcher = cnPattern.matcher(name);
        // Create an array with the detected identities
        List names = new ArrayList<>();
        while (matcher.find()) {
            names.add(matcher.group(2));
        }
        
        return names;
    }

    /**
     * Returns the short name of mapping
     * 
     * @return The short name of the mapping
     */
    @Override
    public String name() {
        return "Common Name Mapping";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy