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.enduser.panels.captcha;
20  
21  import java.security.SecureRandom;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.text.RandomStringGenerator;
24  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
25  import org.apache.wicket.ajax.AjaxRequestTarget;
26  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
27  import org.apache.wicket.extensions.markup.html.captcha.CaptchaImageResource;
28  import org.apache.wicket.markup.html.image.Image;
29  import org.apache.wicket.markup.html.panel.Panel;
30  import org.apache.wicket.model.Model;
31  
32  public class CaptchaPanel<T> extends Panel {
33  
34      private static final long serialVersionUID = -450657681453274465L;
35  
36      private static final SecureRandom RANDOM = new SecureRandom();
37  
38      private static final RandomStringGenerator RANDOM_LETTERS = new RandomStringGenerator.Builder().
39              usingRandom(RANDOM::nextInt).
40              withinRange('a', 'z').
41              build();
42  
43      private final Model<String> captchaText = new Model<>();
44  
45      private final CaptchaImageResource captchaImageResource;
46  
47      public CaptchaPanel(final String id) {
48          super(id);
49  
50          captchaImageResource = new CaptchaImageResource() {
51  
52              private static final long serialVersionUID = 2436829189992948005L;
53  
54              @Override
55              protected byte[] render() {
56                  getChallengeIdModel().setObject(RANDOM_LETTERS.generate(6));
57                  return super.render();
58              }
59          };
60          Image captchaImage = new Image("image", captchaImageResource);
61          captchaImage.setOutputMarkupId(true);
62          add(captchaImage);
63  
64          add(new AjaxButton("reloadButton") {
65  
66              private static final long serialVersionUID = -957948639666058749L;
67  
68              @Override
69              protected void onSubmit(final AjaxRequestTarget target) {
70                  captchaImageResource.invalidate();
71                  target.add(captchaImage);
72              }
73  
74          }.setDefaultFormProcessing(false));
75  
76          add(new AjaxTextFieldPanel("captcha", "captcha", captchaText).
77                  hideLabel().
78                  setOutputMarkupId(true).
79                  setOutputMarkupPlaceholderTag(true));
80      }
81  
82      public boolean check() {
83          boolean check = StringUtils.isBlank(captchaText.getObject())
84                  || StringUtils.isBlank(captchaImageResource.getChallengeId())
85                  ? false
86                  : captchaText.getObject().equals(captchaImageResource.getChallengeId());
87  
88          captchaImageResource.invalidate();
89          return check;
90      }
91  }