2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2010 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
25 * To change this template, choose Tools | Templates
26 * and open the template in the editor.
28 package org.collectionspace.services.account.storage.csidp;
30 import java.util.Date;
31 import java.util.UUID;
33 import javax.persistence.Query;
35 import org.collectionspace.services.authentication.User;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.document.BadRequestException;
38 import org.collectionspace.services.common.document.DocumentNotFoundException;
39 import org.collectionspace.services.common.document.JaxbUtils;
40 import org.collectionspace.services.common.document.TransactionException;
41 import org.collectionspace.services.common.security.SecurityUtils;
42 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * UserStorageClient manages persistence for CS IdP
49 * Note: this class is always used by the AccountStorageClient which provides
50 * access to entity manager
53 public class UserStorageClient {
55 private final Logger logger = LoggerFactory.getLogger(UserStorageClient.class);
58 * create user with given userId and password
63 public User create(String userId, byte[] password) throws Exception {
64 User user = new User();
65 user.setUsername(userId);
66 String salt = UUID.randomUUID().toString();
67 user.setPasswd(getEncPassword(userId, password, salt));
69 user.setCreatedAtItem(new Date());
74 * getUser get user for given userId
75 * @param em EntityManager
78 public User get(JPATransactionContext jpaTransactionContext, String userId) throws DocumentNotFoundException {
79 User userFound = (User) jpaTransactionContext.find(User.class, userId);
80 if (userFound == null) {
81 String msg = "Could not find user with userId=" + userId;
83 throw new DocumentNotFoundException(msg);
89 @SuppressWarnings("rawtypes")
90 public User get(ServiceContext ctx, String userId) throws DocumentNotFoundException, TransactionException {
91 User userFound = null;
93 JPATransactionContext jpaConnectionContext = (JPATransactionContext)ctx.openConnection();
95 userFound = (User) jpaConnectionContext.find(User.class, userId);
96 if (userFound == null) {
97 String msg = "could not find user with userId=" + userId;
99 throw new DocumentNotFoundException(msg);
102 ctx.closeConnection();
109 * updateUser for given userId
110 * @param entity manager
114 public void update(JPATransactionContext jpaTransactionContext, String userId, byte[] password)
115 throws DocumentNotFoundException, Exception {
116 User userFound = get(jpaTransactionContext, userId);
117 if (userFound != null) {
118 String salt = UUID.randomUUID().toString();
119 userFound.setPasswd(getEncPassword(userId, password, salt));
120 userFound.setSalt(salt);
121 userFound.setUpdatedAtItem(new Date());
122 if (logger.isDebugEnabled()) {
123 logger.debug("updated user=" + JaxbUtils.toString(userFound, User.class));
125 jpaTransactionContext.persist(userFound);
130 * delete deletes user with given userId
131 * @param em entity manager
133 * @throws Exception if user for given userId not found
135 public void delete(JPATransactionContext jpaTransactionContext, String userId)
136 throws DocumentNotFoundException, Exception {
137 //if userid gives any indication about the id provider, it should
138 //be used to avoid the following approach
139 StringBuilder usrDelStr = new StringBuilder("DELETE FROM ");
140 usrDelStr.append(User.class.getCanonicalName());
141 usrDelStr.append(" WHERE username = :username");
142 //TODO: add tenant id
143 Query usrDel = jpaTransactionContext.createQuery(usrDelStr.toString());
144 usrDel.setParameter("username", userId);
145 int usrDelCount = usrDel.executeUpdate();
146 if (usrDelCount != 1) {
147 String msg = "could not find user with username=" + userId;
149 throw new DocumentNotFoundException(msg);
153 private String getEncPassword(String userId, byte[] password, String salt) throws BadRequestException {
154 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
155 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
157 SecurityUtils.validatePassword(new String(password));
158 } catch (Exception e) {
159 throw new BadRequestException(e.getMessage());
161 String secEncPasswd = SecurityUtils.createPasswordHash(
162 userId, new String(password), salt);