org.apigw.authserver.x509.ClientX509PrincipalExtractor.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.x509.ClientX509PrincipalExtractor.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW 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 APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */
package org.apigw.authserver.x509;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.web.authentication.preauth.x509.X509PrincipalExtractor;

import javax.security.auth.x500.X500Principal;
import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Extracts data from a X509 certificate and checks client details service for
 * a corresponding certified client.
 * 
 * @author Albert rwall
 * @author Peter Merikan
 * @author Martin Samuelsson
 *
 */
public class ClientX509PrincipalExtractor implements X509PrincipalExtractor {

    private static final Logger log = LoggerFactory.getLogger(ClientX509PrincipalExtractor.class);

    @Override
    public X509ClientPrincipal extractPrincipal(X509Certificate cert) {

        String subjectDN = cert.getSubjectX500Principal().getName(X500Principal.RFC1779);
        String issuerDN = cert.getIssuerX500Principal().getName(X500Principal.RFC1779);

        X509ClientPrincipal x509ClientPrincipal = new X509ClientPrincipal(subjectDN, issuerDN);
        log.trace("created principal: {}", x509ClientPrincipal);

        return x509ClientPrincipal;

    }

}