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 userFound.setPasswd(getEncPassword(userId, password, userFound.getSalt()));
119 userFound.setUpdatedAtItem(new Date());
120 if (logger.isDebugEnabled()) {
121 logger.debug("updated user=" + JaxbUtils.toString(userFound, User.class));
123 jpaTransactionContext.persist(userFound);
128 * delete deletes user with given userId
129 * @param em entity manager
131 * @throws Exception if user for given userId not found
133 public void delete(JPATransactionContext jpaTransactionContext, String userId)
134 throws DocumentNotFoundException, Exception {
135 //if userid gives any indication about the id provider, it should
136 //be used to avoid the following approach
137 StringBuilder usrDelStr = new StringBuilder("DELETE FROM ");
138 usrDelStr.append(User.class.getCanonicalName());
139 usrDelStr.append(" WHERE username = :username");
140 //TODO: add tenant id
141 Query usrDel = jpaTransactionContext.createQuery(usrDelStr.toString());
142 usrDel.setParameter("username", userId);
143 int usrDelCount = usrDel.executeUpdate();
144 if (usrDelCount != 1) {
145 String msg = "could not find user with username=" + userId;
147 throw new DocumentNotFoundException(msg);
151 private String getEncPassword(String userId, byte[] password, String salt) throws BadRequestException {
152 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
153 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
155 SecurityUtils.validatePassword(new String(password));
156 } catch (Exception e) {
157 throw new BadRequestException(e.getMessage());
159 String secEncPasswd = SecurityUtils.createPasswordHash(
160 userId, new String(password), salt);