001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Wojciech Sulejman 016 */ 017package org.nuxeo.ecm.platform.signature.api.user; 018 019import java.util.Map; 020 021import javax.security.auth.x500.X500Principal; 022 023import org.nuxeo.ecm.platform.signature.api.exception.CertException; 024 025/** 026 * Carries user information encoded inside an x509Name. 027 * <p> 028 * This class is a DTO which exposes an X500 Principal view. It is used to pass user information between application 029 * layers. 030 * <p> 031 * Verifies that all required tokens are present. 032 * <p> 033 * Required tokens: 034 * <ul> 035 * <li>user identifier (commonName field) 036 * <li>user X500Principal: commonName CN, organizationalUnitName OU, organizationName O, countryName C 037 * <li>user email (emailAddress) 038 * 039 * @author <a href="mailto:ws@nuxeo.com">Wojciech Sulejman</a> 040 */ 041public class UserInfo { 042 043 private Map<CNField, String> userFields; 044 045 private X500Principal x500Principal; 046 047 /** 048 * The fields provided as a parameter to the constructor. Must be a full set of all the fields as present in the 049 * CNField enum. 050 * 051 * @param userDNFields 052 * @throws CertException 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 * @param userFields 068 * @throws CertException 069 */ 070 public void verify(Map<CNField, String> userFields) throws CertException { 071 for (CNField key : CNField.values()) { 072 if (null == userFields.get(key)) { 073 throw new CertException("UserInfo X500 value missing for:" + key.name()); 074 } 075 } 076 } 077 078 /** 079 * Returns a formatted DN string 080 * 081 * @param userFields 082 * @return 083 */ 084 public String getDN(Map<CNField, String> userFields) { 085 String dN = "C=" + userFields.get(CNField.C) + ", O=" + userFields.get(CNField.O) + ", OU=" 086 + userFields.get(CNField.OU) + ", CN=" + userFields.get(CNField.CN); 087 return dN; 088 } 089 090 public Map<CNField, String> getUserFields() { 091 return userFields; 092 } 093 094 public X500Principal getX500Principal() { 095 return x500Principal; 096 } 097 098 public String toString() { 099 return this.getUserFields().get(CNField.UserID) + " " + this.getUserFields().get(CNField.CN); 100 } 101 102}