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.document.TransactionException;
44 import org.collectionspace.services.common.security.SecurityUtils;
45 import org.collectionspace.services.common.storage.TransactionContext;
46 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
47 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class TokenStorageClient {
54 static private final Logger logger = LoggerFactory.getLogger(TokenStorageClient.class);
57 * create user with given userId and password
62 static public Token create(String accountCsid, String tenantId, BigInteger expireSeconds) {
63 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
64 Token token = new Token();
67 EntityManager em = emf.createEntityManager();
69 token.setId(UUID.randomUUID().toString());
70 token.setAccountCsid(accountCsid);
71 token.setTenantId(tenantId);
72 token.setExpireSeconds(expireSeconds);
73 token.setEnabled(true);
74 token.setCreatedAtItem(new Date());
76 em.getTransaction().begin();
78 em.getTransaction().commit();
82 JpaStorageUtils.releaseEntityManagerFactory(emf);
90 * Update a token for given an id
93 * @throws TransactionException
95 static public void update(TransactionContext transactionContext, String id, boolean enabledFlag) throws DocumentNotFoundException, TransactionException {
96 Token tokenFound = null;
98 tokenFound = get((JPATransactionContext)transactionContext, id);
99 if (tokenFound != null) {
100 tokenFound.setEnabled(enabledFlag);
101 tokenFound.setUpdatedAtItem(new Date());
102 if (logger.isDebugEnabled()) {
103 logger.debug("Updated token=" + JaxbUtils.toString(tokenFound, Token.class));
106 String msg = String.format("Could not find token with id='%s'", id);
107 throw new DocumentNotFoundException(msg);
112 * Get token for given ID
113 * @param em EntityManager
116 public static Token get(JPATransactionContext jpaTransactionContext, String id) throws DocumentNotFoundException, TransactionException {
117 Token tokenFound = null;
119 tokenFound = (Token) jpaTransactionContext.find(Token.class, id);
120 if (tokenFound == null) {
121 String msg = "Could not find token with ID=" + id;
123 throw new DocumentNotFoundException(msg);
129 static public Token get(String id) throws DocumentNotFoundException {
130 Token tokenFound = null;
131 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
134 EntityManager em = emf.createEntityManager();
135 tokenFound = (Token) em.find(Token.class, id);
136 if (tokenFound == null) {
137 String msg = "Could not find token with ID=" + id;
139 throw new DocumentNotFoundException(msg);
143 JpaStorageUtils.releaseEntityManagerFactory(emf);
151 * Deletes the token with given id
153 * @throws Exception if user for given userId not found
155 static public void delete(String id) throws DocumentNotFoundException {
156 EntityManagerFactory emf = JpaStorageUtils.getEntityManagerFactory();
159 EntityManager em = emf.createEntityManager();
161 StringBuilder tokenDelStr = new StringBuilder("DELETE FROM ");
162 tokenDelStr.append(Token.class.getCanonicalName());
163 tokenDelStr.append(" WHERE id = :id");
165 Query tokenDel = em.createQuery(tokenDelStr.toString());
166 tokenDel.setParameter("id", id);
167 int tokenDelCount = tokenDel.executeUpdate();
168 if (tokenDelCount != 1) {
169 String msg = "Could not find token with id=" + id;
171 throw new DocumentNotFoundException(msg);
175 JpaStorageUtils.releaseEntityManagerFactory(emf);
180 private String getEncPassword(String userId, byte[] password) throws BadRequestException {
181 //jaxb unmarshaller already unmarshal xs:base64Binary, no need to b64 decode
182 //byte[] bpass = Base64.decodeBase64(accountReceived.getPassword());
184 SecurityUtils.validatePassword(new String(password));
185 } catch (Exception e) {
186 throw new BadRequestException(e.getMessage());
188 String secEncPasswd = SecurityUtils.createPasswordHash(
189 userId, new String(password), null);