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.math.BigInteger;
31 import java.util.Date;
32 import java.util.UUID;
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityManagerFactory;
36 import javax.persistence.Query;
38 import org.collectionspace.services.authentication.Token;
39 import org.collectionspace.services.common.document.BadRequestException;
40 import org.collectionspace.services.common.document.DocumentNotFoundException;
41 import org.collectionspace.services.common.document.JaxbUtils;
42 import org.collectionspace.services.common.security.SecurityUtils;
43 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class TokenStorageClient {
50 static private final Logger logger = LoggerFactory.getLogger(TokenStorageClient.class);
53 * create user with given userId and password
58 static public Token create(String accountCsid, String tenantId, BigInteger expireSeconds) {
59 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
60 Token token = new Token();
63 EntityManager em = emf.createEntityManager();
65 token.setId(UUID.randomUUID().toString());
66 token.setAccountCsid(accountCsid);
67 token.setTenantId(tenantId);
68 token.setExpireSeconds(expireSeconds);
69 token.setEnabled(true);
70 token.setCreatedAtItem(new Date());
72 em.getTransaction().begin();
74 em.getTransaction().commit();
78 JpaStorageUtils.releaseEntityManagerFactory(emf);
86 * Get token for given ID
87 * @param em EntityManager
90 static public Token get(String id) throws DocumentNotFoundException {
91 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
92 Token tokenFound = null;
95 EntityManager em = emf.createEntityManager();
96 em.getTransaction().begin();
97 tokenFound = em.find(Token.class, id);
98 em.getTransaction().commit();
99 if (tokenFound == null) {
100 String msg = "Could not find token with ID=" + id;
102 throw new DocumentNotFoundException(msg);
106 JpaStorageUtils.releaseEntityManagerFactory(emf);
114 * Update a token for given an id
118 static public void update(String id, boolean enabledFlag) throws DocumentNotFoundException {
119 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
120 Token tokenFound = null;
123 EntityManager em = emf.createEntityManager();
124 tokenFound = get(id);
126 tokenFound.setEnabled(enabledFlag);
127 tokenFound.setUpdatedAtItem(new Date());
128 if (logger.isDebugEnabled()) {
129 logger.debug("Updated token=" + JaxbUtils.toString(tokenFound, Token.class));
131 em.persist(tokenFound);
135 JpaStorageUtils.releaseEntityManagerFactory(emf);
141 * Deletes the token with given id
143 * @throws Exception if user for given userId not found
145 static public void delete(String id) throws DocumentNotFoundException {
146 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
149 EntityManager em = emf.createEntityManager();
151 StringBuilder tokenDelStr = new StringBuilder("DELETE FROM ");
152 tokenDelStr.append(Token.class.getCanonicalName());
153 tokenDelStr.append(" WHERE id = :id");
155 Query tokenDel = em.createQuery(tokenDelStr.toString());
156 tokenDel.setParameter("id", id);
157 int tokenDelCount = tokenDel.executeUpdate();
158 if (tokenDelCount != 1) {
159 String msg = "Could not find token with id=" + id;
161 throw new DocumentNotFoundException(msg);
165 JpaStorageUtils.releaseEntityManagerFactory(emf);
170 private String getEncPassword(String userId, byte[] password) throws BadRequestException {
171 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
172 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
174 SecurityUtils.validatePassword(new String(password));
175 } catch (Exception e) {
176 throw new BadRequestException(e.getMessage());
178 String secEncPasswd = SecurityUtils.createPasswordHash(
179 userId, new String(password));