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.client.console.wizards;
20  
21  import java.io.Serializable;
22  import java.nio.charset.StandardCharsets;
23  import java.util.Base64;
24  import org.apache.syncope.client.console.rest.SAML2IdPEntityRestClient;
25  import org.apache.syncope.client.console.wicket.markup.html.form.TextEditorPanel;
26  import org.apache.syncope.common.lib.to.SAML2IdPEntityTO;
27  import org.apache.wicket.PageReference;
28  import org.apache.wicket.extensions.wizard.WizardModel;
29  import org.apache.wicket.model.PropertyModel;
30  
31  public class SAML2IdPEntityWizardBuilder extends SAML2EntityWizardBuilder<SAML2IdPEntityTO> {
32  
33      private static final long serialVersionUID = -8013493490328546125L;
34  
35      protected final SAML2IdPEntityRestClient saml2IdPEntityRestClient;
36  
37      public SAML2IdPEntityWizardBuilder(
38              final SAML2IdPEntityTO defaultItem,
39              final SAML2IdPEntityRestClient saml2IdPEntityRestClient,
40              final PageReference pageRef) {
41  
42          super(defaultItem, pageRef);
43          this.saml2IdPEntityRestClient = saml2IdPEntityRestClient;
44      }
45  
46      @Override
47      protected Serializable onApplyInternal(final SAML2IdPEntityTO modelObject) {
48          if (modelObject.getMetadata() != null) {
49              modelObject.setMetadata(Base64.getEncoder().encodeToString(
50                      modelObject.getMetadata().getBytes(StandardCharsets.UTF_8)));
51          }
52          if (modelObject.getSigningCertificate() != null) {
53              modelObject.setSigningCertificate(Base64.getEncoder().encodeToString(
54                      modelObject.getSigningCertificate().getBytes(StandardCharsets.UTF_8)));
55          }
56          if (modelObject.getSigningKey() != null) {
57              modelObject.setSigningKey(Base64.getEncoder().encodeToString(
58                      modelObject.getSigningKey().getBytes(StandardCharsets.UTF_8)));
59          }
60          if (modelObject.getEncryptionCertificate() != null) {
61              modelObject.setEncryptionCertificate(Base64.getEncoder().encodeToString(
62                      modelObject.getEncryptionCertificate().getBytes(StandardCharsets.UTF_8)));
63          }
64          if (modelObject.getEncryptionKey() != null) {
65              modelObject.setEncryptionKey(Base64.getEncoder().encodeToString(
66                      modelObject.getEncryptionKey().getBytes(StandardCharsets.UTF_8)));
67          }
68          saml2IdPEntityRestClient.set(modelObject);
69          return modelObject;
70      }
71  
72      @Override
73      protected WizardModel buildModelSteps(final SAML2IdPEntityTO modelObject, final WizardModel wizardModel) {
74          if (modelObject.getMetadata() != null) {
75              modelObject.setMetadata(new String(Base64.getDecoder().decode(
76                      modelObject.getMetadata()), StandardCharsets.UTF_8));
77          }
78          if (modelObject.getSigningCertificate() != null) {
79              modelObject.setSigningCertificate(new String(Base64.getDecoder().decode(
80                      modelObject.getSigningCertificate()), StandardCharsets.UTF_8));
81          }
82          if (modelObject.getSigningKey() != null) {
83              modelObject.setSigningKey(new String(Base64.getDecoder().decode(
84                      modelObject.getSigningKey()), StandardCharsets.UTF_8));
85          }
86          if (modelObject.getEncryptionCertificate() != null) {
87              modelObject.setEncryptionCertificate(new String(Base64.getDecoder().decode(
88                      modelObject.getEncryptionCertificate()), StandardCharsets.UTF_8));
89          }
90          if (modelObject.getEncryptionKey() != null) {
91              modelObject.setEncryptionKey(new String(Base64.getDecoder().decode(
92                      modelObject.getEncryptionKey()), StandardCharsets.UTF_8));
93          }
94  
95          wizardModel.add(new Metadata(modelObject, pageRef));
96          wizardModel.add(new TextPem(modelObject, "signingCertificate", pageRef));
97          wizardModel.add(new TextPem(modelObject, "signingKey", pageRef));
98          wizardModel.add(new TextPem(modelObject, "encryptionCertificate", pageRef));
99          wizardModel.add(new TextPem(modelObject, "encryptionKey", pageRef));
100         return wizardModel;
101     }
102 
103     protected class TextPem extends Pem {
104 
105         private static final long serialVersionUID = 1L;
106 
107         public TextPem(final SAML2IdPEntityTO entity, final String property, final PageReference pageRef) {
108             super(property);
109 
110             add(new TextEditorPanel(null, new PropertyModel<>(entity, property), false, pageRef));
111         }
112     }
113 }