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.DocumentException;
41 import org.collectionspace.services.common.document.DocumentNotFoundException;
42 import org.collectionspace.services.common.document.JaxbUtils;
43 import org.collectionspace.services.common.security.SecurityUtils;
44 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class TokenStorageClient {
51 static private final Logger logger = LoggerFactory.getLogger(TokenStorageClient.class);
54 * create user with given userId and password
59 static public Token create(String accountCsid, String tenantId, BigInteger expireSeconds) {
60 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
61 Token token = new Token();
64 EntityManager em = emf.createEntityManager();
66 token.setId(UUID.randomUUID().toString());
67 token.setAccountCsid(accountCsid);
68 token.setTenantId(tenantId);
69 token.setExpireSeconds(expireSeconds);
70 token.setEnabled(true);
71 token.setCreatedAtItem(new Date());
73 em.getTransaction().begin();
75 em.getTransaction().commit();
79 JpaStorageUtils.releaseEntityManagerFactory(emf);
87 * Get token for given ID
88 * @param em EntityManager
91 static public Token get(String id) throws DocumentNotFoundException {
92 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
93 Token tokenFound = null;
96 EntityManager em = emf.createEntityManager();
97 tokenFound = get(em, id);
100 JpaStorageUtils.releaseEntityManagerFactory(emf);
108 * Update a token for given an id
112 static public void update(String id, boolean enabledFlag) throws DocumentNotFoundException {
113 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
114 EntityManager em = null;
116 Token tokenFound = null;
118 em = emf.createEntityManager();
119 tokenFound = get(em, id);
120 if (tokenFound != null) {
121 em.getTransaction().begin();
122 tokenFound.setEnabled(enabledFlag);
123 tokenFound.setUpdatedAtItem(new Date());
124 if (logger.isDebugEnabled()) {
125 logger.debug("Updated token=" + JaxbUtils.toString(tokenFound, Token.class));
127 em.getTransaction().commit();
129 String msg = String.format("Could not find token with id='%s'", id);
130 throw new DocumentNotFoundException(msg);
133 if (em != null && em.isOpen()) {
137 JpaStorageUtils.releaseEntityManagerFactory(emf);
142 public static Token get(EntityManager em, String id) throws DocumentNotFoundException {
143 Token tokenFound = null;
145 em.getTransaction().begin();
146 tokenFound = em.find(Token.class, id);
147 em.getTransaction().commit();
148 if (tokenFound == null) {
149 String msg = "Could not find token with ID=" + id;
151 throw new DocumentNotFoundException(msg);
158 * Deletes the token with given id
160 * @throws Exception if user for given userId not found
162 static public void delete(String id) throws DocumentNotFoundException {
163 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
166 EntityManager em = emf.createEntityManager();
168 StringBuilder tokenDelStr = new StringBuilder("DELETE FROM ");
169 tokenDelStr.append(Token.class.getCanonicalName());
170 tokenDelStr.append(" WHERE id = :id");
172 Query tokenDel = em.createQuery(tokenDelStr.toString());
173 tokenDel.setParameter("id", id);
174 int tokenDelCount = tokenDel.executeUpdate();
175 if (tokenDelCount != 1) {
176 String msg = "Could not find token with id=" + id;
178 throw new DocumentNotFoundException(msg);
182 JpaStorageUtils.releaseEntityManagerFactory(emf);
187 private String getEncPassword(String userId, byte[] password) throws BadRequestException {
188 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
189 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
191 SecurityUtils.validatePassword(new String(password));
192 } catch (Exception e) {
193 throw new BadRequestException(e.getMessage());
195 String secEncPasswd = SecurityUtils.createPasswordHash(
196 userId, new String(password));