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 * 041 * @author <a href="mailto:[email protected]">Wojciech Sulejman</a> 042 */ 043public class UserInfo { 044 045 private Map<CNField, String> userFields; 046 047 private X500Principal x500Principal; 048 049 /** 050 * The fields provided as a parameter to the constructor. Must be a full set of all the fields as present in the 051 * CNField enum. 052 * 053 * @param userDNFields 054 * @throws CertException 055 */ 056 public UserInfo(Map<CNField, String> userDNFields) throws CertException { 057 verify(userDNFields); 058 this.userFields = userDNFields; 059 try { 060 x500Principal = new X500Principal(getDN(userDNFields)); 061 } catch (IllegalArgumentException e) { 062 throw new CertException("User data might have an incorrect format" + e); 063 } 064 } 065 066 /** 067 * Verifies that all required X500 Principal field values have been set on this object 068 * 069 * @param userFields 070 * @throws CertException 071 */ 072 public void verify(Map<CNField, String> userFields) throws CertException { 073 for (CNField key : CNField.values()) { 074 if (null == userFields.get(key)) { 075 throw new CertException("UserInfo X500 value missing for:" + key.name()); 076 } 077 } 078 } 079 080 /** 081 * Returns a formatted DN string 082 * 083 * @param userFields 084 * @return 085 */ 086 public String getDN(Map<CNField, String> userFields) { 087 String dN = "C=" + userFields.get(CNField.C) + ", O=" + userFields.get(CNField.O) + ", OU=" 088 + userFields.get(CNField.OU) + ", CN=" + userFields.get(CNField.CN); 089 return dN; 090 } 091 092 public Map<CNField, String> getUserFields() { 093 return userFields; 094 } 095 096 public X500Principal getX500Principal() { 097 return x500Principal; 098 } 099 100 public String toString() { 101 return this.getUserFields().get(CNField.UserID) + " " + this.getUserFields().get(CNField.CN); 102 } 103 104}