View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }