001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *    Wojciech Sulejman
018 */
019package org.nuxeo.ecm.platform.signature.api.user;
020
021import java.util.Map;
022
023import javax.security.auth.x500.X500Principal;
024
025import org.nuxeo.ecm.platform.signature.api.exception.CertException;
026
027/**
028 * Carries user information encoded inside an x509Name.
029 * <p>
030 * This class is a DTO which exposes an X500 Principal view. It is used to pass user information between application
031 * layers.
032 * <p>
033 * Verifies that all required tokens are present.
034 * <p>
035 * Required tokens:
036 * <ul>
037 * <li>user identifier (commonName field)
038 * <li>user X500Principal: commonName CN, organizationalUnitName OU, organizationName O, countryName C
039 * <li>user email (emailAddress)
040 * </ul>
041 *
042 * @author <a href="mailto:ws@nuxeo.com">Wojciech Sulejman</a>
043 */
044public class UserInfo {
045
046    private Map<CNField, String> userFields;
047
048    private X500Principal x500Principal;
049
050    /**
051     * The fields provided as a parameter to the constructor. Must be a full set of all the fields as present in the
052     * CNField enum.
053     */
054    public UserInfo(Map<CNField, String> userDNFields) throws CertException {
055        verify(userDNFields);
056        this.userFields = userDNFields;
057        try {
058            x500Principal = new X500Principal(getDN(userDNFields));
059        } catch (IllegalArgumentException e) {
060            throw new CertException("User data might have an incorrect format" + e);
061        }
062    }
063
064    /**
065     * Verifies that all required X500 Principal field values have been set on this object
066     */
067    public void verify(Map<CNField, String> userFields) throws CertException {
068        for (CNField key : CNField.values()) {
069            if (null == userFields.get(key)) {
070                throw new CertException("UserInfo X500 value missing for:" + key.name());
071            }
072        }
073    }
074
075    /**
076     * Returns a formatted DN string
077     */
078    public String getDN(Map<CNField, String> userFields) {
079        String dN = "C=" + userFields.get(CNField.C) + ", O=" + userFields.get(CNField.O) + ", OU="
080                + userFields.get(CNField.OU) + ", CN=" + userFields.get(CNField.CN);
081        return dN;
082    }
083
084    public Map<CNField, String> getUserFields() {
085        return userFields;
086    }
087
088    public X500Principal getX500Principal() {
089        return x500Principal;
090    }
091
092    @Override
093    public String toString() {
094        return this.getUserFields().get(CNField.UserID) + " " + this.getUserFields().get(CNField.CN);
095    }
096
097}