1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic.scim;
20
21 import java.time.OffsetDateTime;
22 import java.util.Base64;
23 import javax.ws.rs.core.MediaType;
24 import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
25 import org.apache.syncope.common.lib.scim.SCIMConf;
26 import org.apache.syncope.common.lib.scim.SCIMGeneralConf;
27 import org.apache.syncope.common.lib.scim.types.SCIMEntitlement;
28 import org.apache.syncope.common.lib.to.PlainSchemaTO;
29 import org.apache.syncope.common.lib.types.AttrSchemaType;
30 import org.apache.syncope.common.lib.types.SchemaType;
31 import org.apache.syncope.core.logic.SchemaLogic;
32 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
33 import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
34 import org.apache.syncope.core.spring.security.AuthContextUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.security.access.prepost.PreAuthorize;
38
39 public class SCIMConfManager {
40
41 protected static final Logger LOG = LoggerFactory.getLogger(SCIMConfManager.class);
42
43 protected final ConfParamOps confParamOps;
44
45 protected final SchemaLogic schemaLogic;
46
47 public SCIMConfManager(final ConfParamOps confParamOps, final SchemaLogic schemaLogic) {
48 this.confParamOps = confParamOps;
49 this.schemaLogic = schemaLogic;
50 }
51
52 @PreAuthorize("hasRole('" + SCIMEntitlement.SCIM_CONF_GET + "')")
53 public SCIMConf get() {
54 SCIMConf conf = null;
55 String confString = confParamOps.get(AuthContextUtils.getDomain(), SCIMConf.KEY, null, String.class);
56 if (confString != null) {
57 try {
58 conf = POJOHelper.deserialize(new String(Base64.getDecoder().decode(confString)), SCIMConf.class);
59 } catch (Exception e) {
60 LOG.error("Could not deserialize, reverting to default", e);
61 }
62 }
63 if (conf == null) {
64 conf = new SCIMConf();
65 set(conf);
66 }
67
68 return conf;
69 }
70
71 @PreAuthorize("hasRole('" + SCIMEntitlement.SCIM_CONF_SET + "')")
72 public void set(final SCIMConf conf) {
73 try {
74 schemaLogic.read(SchemaType.PLAIN, SCIMConf.KEY);
75 } catch (NotFoundException e) {
76 PlainSchemaTO scimConf = new PlainSchemaTO();
77 scimConf.setKey(SCIMConf.KEY);
78 scimConf.setType(AttrSchemaType.Binary);
79 scimConf.setMimeType(MediaType.APPLICATION_JSON);
80 schemaLogic.create(SchemaType.PLAIN, scimConf);
81 }
82 conf.setGeneralConf(new SCIMGeneralConf());
83 conf.getGeneralConf().setLastChangeDate(OffsetDateTime.now());
84
85 confParamOps.set(AuthContextUtils.getDomain(),
86 SCIMConf.KEY, Base64.getEncoder().encodeToString(POJOHelper.serialize(conf).getBytes()));
87 }
88 }