1 | package gov.hhs.fha.nhinc.token;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.FileReader;
|
---|
5 | import java.io.FileWriter;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.io.RandomAccessFile;
|
---|
9 | import java.util.Properties;
|
---|
10 | import java.util.PropertyResourceBundle;
|
---|
11 | import org.apache.commons.logging.Log;
|
---|
12 | import org.apache.commons.logging.LogFactory;
|
---|
13 | import gov.hhs.fha.nhinc.common.nhinccommon.AssertionType;
|
---|
14 | import gov.hhs.fha.nhinc.common.nhinccommon.CeType;
|
---|
15 | import gov.hhs.fha.nhinc.common.nhinccommon.HomeCommunityType;
|
---|
16 | import gov.hhs.fha.nhinc.common.nhinccommon.PersonNameType;
|
---|
17 | import gov.hhs.fha.nhinc.common.nhinccommon.UserType;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This class is used to store and retrieve the information for the Saml token
|
---|
21 | * from a file
|
---|
22 | *
|
---|
23 | * @author Victoria Vickers
|
---|
24 | * @author Neil Webb
|
---|
25 | */
|
---|
26 | public class InternalTokenMgr
|
---|
27 | {
|
---|
28 | private static Log log = LogFactory.getLog(InternalTokenMgr.class);
|
---|
29 | public static final String propFileName = "token";
|
---|
30 | public static final String commonNameAttrName = "CommonName";
|
---|
31 | public static final String orgUnitAttrName = "OrganizationalUnit";
|
---|
32 | public static final String otherNameAttrName = "OtherName";
|
---|
33 | public static final String locationCityAttrName = "LocationCity";
|
---|
34 | public static final String locationStateAttrName = "LocationState";
|
---|
35 | public static final String locationCountryAttrName = "LocationCountry";
|
---|
36 | public static final String userIdAttrName = "UserId";
|
---|
37 | public static final String userNameAttrName = "UserName";
|
---|
38 | public static final String userFirstNameAttrName = "UserFirstName";
|
---|
39 | public static final String userMiddleNameAttrName = "UserMiddleName";
|
---|
40 | public static final String userLastNameAttrName = "UserLastName";
|
---|
41 | public static final String userOrgAttrName = "UserOrganization";
|
---|
42 | public static final String userRoleCodeAttrName = "UserRoleCode";
|
---|
43 | public static final String userRoleCodeSystemAttrName = "UserRoleCodeSystem";
|
---|
44 | public static final String userRoleCodeSystemNameAttrName = "UserRoleCodeSystemName";
|
---|
45 | public static final String userRoleDisplayAttrName = "UserRoleDisplayName";
|
---|
46 | public static final String purposeCodeAttrName = "PurposeForUseRoleCode";
|
---|
47 | public static final String purposeCodeSystemAttrName = "PurposeForUseCodeSystem";
|
---|
48 | public static final String purposeCodeSystemNameAttrName = "PurposeForUseCodeSystemName";
|
---|
49 | public static final String purposeDisplayAttrName = "PurposeForUseDisplayName";
|
---|
50 | public static final String actionAttrName = "Action";
|
---|
51 | public static final String resourceAttrName = "Resource";
|
---|
52 | public static final String signDateAttrName = "SignDate";
|
---|
53 | public static final String expireDateAttrName = "ExpirationDate";
|
---|
54 | public static final String claimRefAttrName = "ContentReference";
|
---|
55 | public static final String claimFormTypeAttrName = "ContentType";
|
---|
56 | public static final String claimFormAttrName = "Content";
|
---|
57 | public static final String storeFileName = "tokenAttrStoreFile";
|
---|
58 | public static final String dumpFileName = "tokenAttrDumpFile";
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This method accesses the file specified to hold the information extracted
|
---|
62 | * from the SAML Token and using the key / value properties therein creates
|
---|
63 | * a new Assertion object.
|
---|
64 | * @return The new Assertion object containing the extracted token information
|
---|
65 | */
|
---|
66 | AssertionType retrieveInfoOperation()
|
---|
67 | {
|
---|
68 | log.debug("Enter retrieveInfoOperation");
|
---|
69 |
|
---|
70 | AssertionType assertOut = new AssertionType();
|
---|
71 | CeType purposeCoded = new CeType();
|
---|
72 | UserType user = new UserType();
|
---|
73 | PersonNameType userPerson = new PersonNameType();
|
---|
74 | CeType userRole = new CeType();
|
---|
75 | HomeCommunityType userHc = new HomeCommunityType();
|
---|
76 | user.setPersonName(userPerson);
|
---|
77 | user.setOrg(userHc);
|
---|
78 | user.setRoleCoded(userRole);
|
---|
79 | assertOut.setUserInfo(user);
|
---|
80 | assertOut.setPurposeOfDisclosureCoded(purposeCoded);
|
---|
81 |
|
---|
82 | BufferedReader reader = null;
|
---|
83 | String fileName = null;
|
---|
84 | try
|
---|
85 | {
|
---|
86 | PropertyResourceBundle prop = (PropertyResourceBundle) PropertyResourceBundle.getBundle(propFileName);
|
---|
87 | fileName = prop.getString(dumpFileName);
|
---|
88 |
|
---|
89 | reader = new BufferedReader(new FileReader(fileName));
|
---|
90 |
|
---|
91 | Properties storedProps = new Properties();
|
---|
92 | storedProps.load(reader);
|
---|
93 |
|
---|
94 | userPerson.setGivenName(storedProps.getProperty(userFirstNameAttrName));
|
---|
95 | userPerson.setFamilyName(storedProps.getProperty(userLastNameAttrName));
|
---|
96 | userPerson.setSecondNameOrInitials(storedProps.getProperty(userMiddleNameAttrName));
|
---|
97 | userHc.setName(storedProps.getProperty(userOrgAttrName));
|
---|
98 | user.setUserName(storedProps.getProperty(userNameAttrName));
|
---|
99 | userRole.setCode(storedProps.getProperty(userRoleCodeAttrName));
|
---|
100 | userRole.setCodeSystem(storedProps.getProperty(userRoleCodeSystemAttrName));
|
---|
101 | userRole.setCodeSystemName(storedProps.getProperty(userRoleCodeSystemNameAttrName));
|
---|
102 | userRole.setDisplayName(storedProps.getProperty(userRoleDisplayAttrName));
|
---|
103 |
|
---|
104 | purposeCoded.setCode(storedProps.getProperty(purposeCodeAttrName));
|
---|
105 | purposeCoded.setCodeSystem(storedProps.getProperty(purposeCodeSystemAttrName));
|
---|
106 | purposeCoded.setCodeSystemName(storedProps.getProperty(purposeCodeSystemNameAttrName));
|
---|
107 | purposeCoded.setDisplayName(storedProps.getProperty(purposeDisplayAttrName));
|
---|
108 |
|
---|
109 | assertOut.setDateOfSignature(storedProps.getProperty(signDateAttrName));
|
---|
110 | assertOut.setExpirationDate(storedProps.getProperty(expireDateAttrName));
|
---|
111 | assertOut.setClaimFormRef(storedProps.getProperty(claimRefAttrName));
|
---|
112 |
|
---|
113 | String strForm = storedProps.getProperty(claimFormAttrName);
|
---|
114 | if (strForm != null && !strForm.isEmpty())
|
---|
115 | {
|
---|
116 | byte[] formRaw = strForm.getBytes();
|
---|
117 | assertOut.setClaimFormRaw(formRaw);
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 | catch (IOException ex)
|
---|
122 | {
|
---|
123 | log.error("retrieveInfoOperation " + ex.getMessage());
|
---|
124 | }
|
---|
125 | finally
|
---|
126 | {
|
---|
127 | try
|
---|
128 | {
|
---|
129 | if (reader != null)
|
---|
130 | {
|
---|
131 | reader.close();
|
---|
132 | }
|
---|
133 | }
|
---|
134 | catch (IOException iOException)
|
---|
135 | {
|
---|
136 | log.error("retrieveInfoOperation " + iOException.getMessage());
|
---|
137 | }
|
---|
138 | }
|
---|
139 | log.debug("Exit retrieveInfoOperation");
|
---|
140 | return assertOut;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * This method creates the file specified to hold the Assertion information
|
---|
145 | * for use in creating the SAML Token, and stores all available information
|
---|
146 | * as key / value pairs.
|
---|
147 | * @param assertIn The Assertion object containing assertion information,
|
---|
148 | * user information, and home community information
|
---|
149 | * @param actionName The action associated with the desired operation is set
|
---|
150 | * by the bpel and is defined to be one of: subjectDiscovery,
|
---|
151 | * retrieveDocuments, queryDocuments, queryAuditLog, notify, subscribe, or
|
---|
152 | * unsubscribe
|
---|
153 | * @param resourceURI The URI to the service endpoint being invoked as set
|
---|
154 | * by the bpel
|
---|
155 | */
|
---|
156 | void storeInfoOperation(AssertionType assertIn, String actionName, String resourceURI)
|
---|
157 | {
|
---|
158 | log.debug("InternalTokenMgr.storeInfoOperation() -- Begin");
|
---|
159 | RandomAccessFile raFile = null;
|
---|
160 | PrintWriter writeOut = null;
|
---|
161 |
|
---|
162 | try
|
---|
163 | {
|
---|
164 | PropertyResourceBundle prop = (PropertyResourceBundle) PropertyResourceBundle.getBundle(propFileName);
|
---|
165 | String fileName = prop.getString(storeFileName);
|
---|
166 |
|
---|
167 | raFile = new RandomAccessFile(fileName, "rw");
|
---|
168 | raFile.setLength(0);
|
---|
169 | log.debug("Create: " + fileName);
|
---|
170 |
|
---|
171 | //do writing
|
---|
172 | writeOut = new PrintWriter(new FileWriter(fileName));
|
---|
173 | if (actionName != null && !actionName.isEmpty())
|
---|
174 | {
|
---|
175 | writeOut.println(actionAttrName + "=" + actionName);
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (resourceURI != null && !resourceURI.isEmpty())
|
---|
179 | {
|
---|
180 | writeOut.println(resourceAttrName + "=" + resourceURI);
|
---|
181 | }
|
---|
182 | if (assertIn != null)
|
---|
183 | {
|
---|
184 | String purposeCode = "=TREATMENT";
|
---|
185 | String purposeCodeSystem = "=2.16.840.1.113883.3.18.7.1";
|
---|
186 | String purposeCodeSystemName = "=nhin-purpose";
|
---|
187 | String purposeDisplay = "=" + assertIn.getPurposeOfDisclosure();
|
---|
188 | if (assertIn.getPurposeOfDisclosureCoded() != null)
|
---|
189 | {
|
---|
190 | purposeCode = "=" + assertIn.getPurposeOfDisclosureCoded().getCode();
|
---|
191 | purposeCodeSystem = "=" + assertIn.getPurposeOfDisclosureCoded().getCodeSystem();
|
---|
192 | purposeCodeSystemName = "=" + assertIn.getPurposeOfDisclosureCoded().getCodeSystemName();
|
---|
193 | purposeDisplay = "=" + assertIn.getPurposeOfDisclosureCoded().getDisplayName();
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | log.warn("InternalTokenMgr.storeInfoOperation assertion.PurposeOfDisclosureCoded is null - PurposeOfDisclosure element has been deprecated");
|
---|
198 | }
|
---|
199 | writeOut.println(purposeCodeAttrName + purposeCode);
|
---|
200 | writeOut.println(purposeCodeSystemAttrName + purposeCodeSystem);
|
---|
201 | writeOut.println(purposeCodeSystemNameAttrName + purposeCodeSystemName);
|
---|
202 | writeOut.println(purposeDisplayAttrName + purposeDisplay);
|
---|
203 |
|
---|
204 | if (assertIn.getUserInfo() != null)
|
---|
205 | {
|
---|
206 | if (assertIn.getUserInfo().getPersonName() != null)
|
---|
207 | {
|
---|
208 | writeOut.println(userFirstNameAttrName + "=" + assertIn.getUserInfo().getPersonName().getGivenName());
|
---|
209 | writeOut.println(userMiddleNameAttrName + "=" + assertIn.getUserInfo().getPersonName().getSecondNameOrInitials());
|
---|
210 | writeOut.println(userLastNameAttrName + "=" + assertIn.getUserInfo().getPersonName().getFamilyName());
|
---|
211 | }
|
---|
212 | writeOut.println(userNameAttrName + "=" + assertIn.getUserInfo().getUserName());
|
---|
213 | if (assertIn.getUserInfo().getOrg() != null)
|
---|
214 | {
|
---|
215 | writeOut.println(userOrgAttrName + "=" + assertIn.getUserInfo().getOrg().getName());
|
---|
216 | }
|
---|
217 |
|
---|
218 | String userCode = "=112247003";
|
---|
219 | String userCodeSystem = "=2.16.840.1.113883.6.96";
|
---|
220 | String userCodeSystemName = "=SNOMED_CT";
|
---|
221 | String userDisplay = "=" + assertIn.getUserInfo().getRole();
|
---|
222 | if (assertIn.getUserInfo().getRoleCoded() != null)
|
---|
223 | {
|
---|
224 | userCode = "=" + assertIn.getUserInfo().getRoleCoded().getCode();
|
---|
225 | userCodeSystem = "=" + assertIn.getUserInfo().getRoleCoded().getCodeSystem();
|
---|
226 | userCodeSystemName = "=" + assertIn.getUserInfo().getRoleCoded().getCodeSystemName();
|
---|
227 | userDisplay = "=" + assertIn.getUserInfo().getRoleCoded().getDisplayName();
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | log.warn("InternalTokenMgr.storeInfoOperation assertion.userInfo.RoleCoded is null - User role element has been deprecated");
|
---|
232 | }
|
---|
233 | writeOut.println(userRoleCodeAttrName + userCode);
|
---|
234 | writeOut.println(userRoleCodeSystemAttrName + userCodeSystem);
|
---|
235 | writeOut.println(userRoleCodeSystemNameAttrName + userCodeSystemName);
|
---|
236 | writeOut.println(userRoleDisplayAttrName + userDisplay);
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | log.info("InternalTokenMgr.storeInfoOperation assertion.user is null - No assertion.user data stored");
|
---|
241 | }
|
---|
242 |
|
---|
243 | // For use in the Evidence
|
---|
244 | writeOut.println(expireDateAttrName + "=" + assertIn.getExpirationDate());
|
---|
245 | writeOut.println(signDateAttrName + "=" + assertIn.getDateOfSignature());
|
---|
246 | writeOut.println(claimRefAttrName + "=" + assertIn.getClaimFormRef());
|
---|
247 |
|
---|
248 | String strForm = "";
|
---|
249 | byte[] rawForm = assertIn.getClaimFormRaw();
|
---|
250 | if (rawForm != null && rawForm.length > 0)
|
---|
251 | {
|
---|
252 | strForm = new String(rawForm);
|
---|
253 | }
|
---|
254 | writeOut.println(claimFormAttrName + "=" + strForm);
|
---|
255 |
|
---|
256 | }
|
---|
257 | else
|
---|
258 | {
|
---|
259 | log.info("InternalTokenMgr.storeInfoOperation assertion input parameter is null - No assertion data stored");
|
---|
260 | }
|
---|
261 | }
|
---|
262 | catch (IOException ex)
|
---|
263 | {
|
---|
264 | log.error("storeInfoOperation " + ex.getMessage());
|
---|
265 | } //File closure guaranteed in a finally
|
---|
266 | finally
|
---|
267 | {
|
---|
268 | try
|
---|
269 | {
|
---|
270 | if (raFile != null)
|
---|
271 | {
|
---|
272 | raFile.close();
|
---|
273 | }
|
---|
274 | if (writeOut != null)
|
---|
275 | {
|
---|
276 | writeOut.close();
|
---|
277 | }
|
---|
278 | }
|
---|
279 | catch (IOException iOException)
|
---|
280 | {
|
---|
281 | log.error("storeInfoOperation " + iOException.getMessage());
|
---|
282 | }
|
---|
283 | }
|
---|
284 | log.debug("InternalTokenMgr.storeInfoOperation() -- End");
|
---|
285 | }
|
---|
286 | }
|
---|