View Javadoc
1   package fr.paris.lutece.plugins.identitystore.modules.fccertifier.service.certifier;
2   
3   import fr.paris.lutece.plugins.identitystore.service.certifier.IGenerateAutomaticCertifierAttribute;
4   import fr.paris.lutece.plugins.identitystore.v2.web.rs.dto.IdentityDto;
5   import fr.paris.lutece.portal.service.util.AppLogService;
6   import fr.paris.lutece.util.fckey.FCKeyException;
7   import fr.paris.lutece.util.fckey.FCKeyService;
8   
9   /**
10   * The Class AutomaticFcKeyGenerator.
11   */
12  public class AutomaticFcKeyGenerator implements IGenerateAutomaticCertifierAttribute {
13  
14  	
15  	private static final String FIELD_SEPARATOR="|";
16  	private static final String CRYPTO_ALGO ="SHA-256";
17  	
18  	/** The str gender attribute. */
19  	private String _strGenderAttribute;
20  
21  	/** The str first name attribute. */
22  	private String _strFirstNameAttribute;
23  
24  	/** The str family name attribute. */
25  	private String _strFamilyNameAttribute;
26  
27  	/** The str birthplace attribute. */
28  	private String _strBirthplaceAttribute;
29  
30  	/** The str birthdate attribute. */
31  	private String _strBirthdateAttribute;
32  
33  	/** The str birthcountry attribute. */
34  	private String _strBirthcountryAttribute;
35  
36  	/**
37  	 * Gets the gender attribute.
38  	 *
39  	 * @return the gender attribute
40  	 */
41  	public String getGenderAttribute() {
42  		return _strGenderAttribute;
43  	}
44  
45  	/**
46  	 * Sets the gender attribute.
47  	 *
48  	 * @param _strGenderAttribute the new gender attribute
49  	 */
50  	public void setGenderAttribute(String _strGenderAttribute) {
51  		this._strGenderAttribute = _strGenderAttribute;
52  	}
53  
54  	/**
55  	 * Sets the first name attribute.
56  	 *
57  	 * @param _strFirstNameAttribute the new first name attribute
58  	 */
59  	public void setFirstNameAttribute(String _strFirstNameAttribute) {
60  		this._strFirstNameAttribute = _strFirstNameAttribute;
61  	}
62  
63  	/**
64  	 * Sets the family name attribute.
65  	 *
66  	 * @param _strFamilyNameAttribute the new family name attribute
67  	 */
68  	public void setFamilyNameAttribute(String _strFamilyNameAttribute) {
69  		this._strFamilyNameAttribute = _strFamilyNameAttribute;
70  	}
71  
72  	/**
73  	 * Sets the birthplace attribute.
74  	 *
75  	 * @param _strBirthplaceAttribute the new birthplace attribute
76  	 */
77  	public void setBirthplaceAttribute(String _strBirthplaceAttribute) {
78  		this._strBirthplaceAttribute = _strBirthplaceAttribute;
79  	}
80  
81  	/**
82  	 * Sets the birthdate attribute.
83  	 *
84  	 * @param _strBirthdateAttribute the new birthdate attribute
85  	 */
86  	public void setBirthdateAttribute(String _strBirthdateAttribute) {
87  		this._strBirthdateAttribute = _strBirthdateAttribute;
88  	}
89  
90  	/**
91  	 * Sets the birthcountry attribute.
92  	 *
93  	 * @param _strBirthcountryAttribute the new birthcountry attribute
94  	 */
95  	public void setBirthcountryAttribute(String _strBirthcountryAttribute) {
96  		this._strBirthcountryAttribute = _strBirthcountryAttribute;
97  	}
98  	
99  	
100 	
101 	/**
102      * {@inheritDoc}
103      */
104     @Override
105 	public boolean mustBeGenerated(IdentityDto identityDTO, String strCertifierCode) {
106 
107 		if (containsAttributeCertificated(identityDTO, _strGenderAttribute, strCertifierCode)
108 				&& containsAttributeCertificated(identityDTO, _strFirstNameAttribute, strCertifierCode)
109 				&& containsAttributeCertificated(identityDTO, _strFamilyNameAttribute, strCertifierCode)
110 				&& containsAttributeCertificated(identityDTO, _strBirthplaceAttribute, strCertifierCode)
111 				&& containsAttributeCertificated(identityDTO, _strBirthcountryAttribute, strCertifierCode)
112 				&& containsAttributeCertificated(identityDTO, _strBirthdateAttribute, strCertifierCode)) {
113 
114 			try {
115 				FCKeyService.validateKeyInformations(identityDTO.getAttributes().get(_strGenderAttribute).getValue(),
116 						identityDTO.getAttributes().get(_strFirstNameAttribute).getValue(),
117 						identityDTO.getAttributes().get(_strFamilyNameAttribute).getValue(),
118 						identityDTO.getAttributes().get(_strBirthplaceAttribute).getValue(),
119 						identityDTO.getAttributes().get(_strBirthcountryAttribute).getValue(),
120 						identityDTO.getAttributes().get(_strBirthdateAttribute).getValue());
121 				return true;
122 			} catch (FCKeyException e) {
123 				AppLogService.error("the FC key can not be generated for customer id "+ identityDTO.getCustomerId(), e);
124 			}
125 
126 		}
127 		return false;
128 
129 	}
130 
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136 	public String getValue(IdentityDto identityDTO) {
137 
138 		
139 		String strFcKey = "";
140 		try {
141 		    strFcKey=FCKeyService.getKey(identityDTO.getAttributes().get(_strGenderAttribute).getValue(),
142 					identityDTO.getAttributes().get(_strFirstNameAttribute).getValue(),
143 					identityDTO.getAttributes().get(_strFamilyNameAttribute).getValue(),
144 					identityDTO.getAttributes().get(_strBirthplaceAttribute).getValue(),
145 					identityDTO.getAttributes().get(_strBirthcountryAttribute).getValue(),
146 					identityDTO.getAttributes().get(_strBirthdateAttribute).getValue());
147 
148 		} catch (FCKeyException e) {
149 			AppLogService.error("An error appear during FC key generation for customer id "+identityDTO.getCustomerId(),e);
150 		}
151 
152 		return strFcKey;
153 
154 		
155 
156 		
157 
158 	}
159 
160 	
161 	
162 	
163 	private boolean containsAttributeCertificated(IdentityDto identityDto, String strAtrributeKey,
164 			String strFcCertifierCode) {
165 
166 		return identityDto.getAttributes().containsKey(strAtrributeKey)
167 				&& identityDto.getAttributes().get(strAtrributeKey).getCertificate() != null && strFcCertifierCode
168 						.equals(identityDto.getAttributes().get(strAtrributeKey).getCertificate().getCertifierCode());
169 
170 	}
171 
172 }