1 package org.collectionspace.authentication.jackson2;
3 import java.io.IOException;
6 import org.collectionspace.authentication.CSpaceTenant;
7 import org.collectionspace.authentication.CSpaceUser;
8 import org.springframework.security.core.GrantedAuthority;
9 import org.springframework.security.core.authority.SimpleGrantedAuthority;
11 import com.fasterxml.jackson.core.JsonParser;
12 import com.fasterxml.jackson.core.JsonProcessingException;
13 import com.fasterxml.jackson.core.type.TypeReference;
14 import com.fasterxml.jackson.databind.DeserializationContext;
15 import com.fasterxml.jackson.databind.JsonDeserializer;
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import com.fasterxml.jackson.databind.node.MissingNode;
20 public class CSpaceUserDeserializer extends JsonDeserializer<CSpaceUser> {
21 private static final TypeReference<Set<SimpleGrantedAuthority>> SIMPLE_GRANTED_AUTHORITY_SET = new TypeReference<Set<SimpleGrantedAuthority>>() {
24 private static final TypeReference<Set<CSpaceTenant>> CSPACE_TENANT_SET = new TypeReference<Set<CSpaceTenant>>() {
28 public CSpaceUser deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
29 ObjectMapper mapper = (ObjectMapper) parser.getCodec();
30 JsonNode jsonNode = mapper.readTree(parser);
32 Set<? extends GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"), SIMPLE_GRANTED_AUTHORITY_SET);
33 Set<CSpaceTenant> tenants = mapper.convertValue(jsonNode.get("tenants"), CSPACE_TENANT_SET);
35 JsonNode passwordNode = readJsonNode(jsonNode, "password");
36 String username = readJsonNode(jsonNode, "username").asText();
37 String password = passwordNode.asText("");
38 String salt = readJsonNode(jsonNode, "salt").asText();
40 CSpaceUser result = new CSpaceUser(username, password, salt, tenants, authorities);
42 if (passwordNode.asText(null) == null) {
43 result.eraseCredentials();
49 private JsonNode readJsonNode(JsonNode jsonNode, String field) {
50 return jsonNode.has(field) ? jsonNode.get(field) : MissingNode.getInstance();