1 package org.collectionspace.authentication.jackson2;
3 import java.io.IOException;
6 import org.collectionspace.authentication.CSpaceTenant;
7 import org.collectionspace.authentication.spring.Saml2AuthenticatedCSpaceUser;
8 import org.springframework.security.core.GrantedAuthority;
9 import org.springframework.security.core.authority.SimpleGrantedAuthority;
10 import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
12 import com.fasterxml.jackson.core.JsonParser;
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.fasterxml.jackson.core.type.TypeReference;
15 import com.fasterxml.jackson.databind.DeserializationContext;
16 import com.fasterxml.jackson.databind.JsonDeserializer;
17 import com.fasterxml.jackson.databind.JsonNode;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.databind.node.MissingNode;
21 public class Saml2AuthenticatedCSpaceUserDeserializer extends JsonDeserializer<Saml2AuthenticatedCSpaceUser> {
22 private static final TypeReference<Set<SimpleGrantedAuthority>> SIMPLE_GRANTED_AUTHORITY_SET = new TypeReference<Set<SimpleGrantedAuthority>>() {
25 private static final TypeReference<Set<CSpaceTenant>> CSPACE_TENANT_SET = new TypeReference<Set<CSpaceTenant>>() {
29 public Saml2AuthenticatedCSpaceUser deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
30 ObjectMapper mapper = (ObjectMapper) parser.getCodec();
31 JsonNode jsonNode = mapper.readTree(parser);
33 Set<? extends GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"), SIMPLE_GRANTED_AUTHORITY_SET);
34 Set<CSpaceTenant> tenants = mapper.convertValue(jsonNode.get("tenants"), CSPACE_TENANT_SET);
36 Saml2AuthenticatedPrincipal principal = mapper.convertValue(readJsonNode(jsonNode, "principal"), Saml2AuthenticatedPrincipal.class);
37 JsonNode passwordNode = readJsonNode(jsonNode, "password");
38 String username = readJsonNode(jsonNode, "username").asText();
39 String password = passwordNode.asText("");
40 boolean requireSSO = readJsonNode(jsonNode, "requireSSO").asBoolean();
41 String salt = readJsonNode(jsonNode, "salt").asText();
43 Saml2AuthenticatedCSpaceUser result = new Saml2AuthenticatedCSpaceUser(principal, username, password, salt, requireSSO, tenants, authorities);
45 if (passwordNode.asText(null) == null) {
46 result.eraseCredentials();
52 private JsonNode readJsonNode(JsonNode jsonNode, String field) {
53 return jsonNode.has(field) ? jsonNode.get(field) : MissingNode.getInstance();