SetGoogleAuthenticatorCredentialClickListener.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.pageclicklistener;

  20. import java.net.URI;
  21. import java.net.URISyntaxException;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
  25. import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialRequest;
  26. import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialResponse;
  27. import com.vaadin.ui.Button.ClickEvent;
  28. import com.vaadin.ui.Button.ClickListener;
  29. import com.vaadin.ui.Notification;
  30. import com.vaadin.ui.UI;
  31. import com.vaadin.ui.VerticalLayout;
  32. import com.vaadin.ui.Window;

  33. import fi.jasoft.qrcode.QRCode;

  34. /**
  35.  * The Class SetGoogleAuthenticatorCredentialClickListener.
  36.  *
  37.  * @see SetGoogleAuthenticatorCredentialClickEvent
  38.  */
  39. public final class SetGoogleAuthenticatorCredentialClickListener extends AbstractClickListener implements ClickListener {


  40.     /** The Constant ERROR_MESSAGE. */
  41.     private static final String ERROR_MESSAGE = "Error message";

  42.     /** The Constant GOOGLE_AUTHENTICATOR_QR_CODE. */
  43.     private static final String GOOGLE_AUTHENTICATOR_QR_CODE = "Google Authenticator QR code";

  44.     /** The Constant LOGGER. */
  45.     private static final Logger LOGGER = LoggerFactory.getLogger(SetGoogleAuthenticatorCredentialClickListener.class);

  46.     /** The Constant MODAL_WINDOW_SIZE. */
  47.     private static final String MODAL_WINDOW_SIZE = "400px";

  48.     /** The Constant PROBLEM_DISPLAYING_QR_CODE. */
  49.     private static final String PROBLEM_DISPLAYING_QR_CODE = "Problem displaying qr code";

  50.     /** The Constant PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR. */
  51.     private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR = "Problem enable google authenticator";

  52.     /** The Constant PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID. */
  53.     private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID = "Problem enable google authenticator, sessionid{}";

  54.     /** The Constant QR_CODE. */
  55.     private static final String QR_CODE = "QR code";

  56.     /** The Constant QR_CODE_IMAGE_SIZE. */
  57.     private static final String QR_CODE_IMAGE_SIZE = "175px";

  58.     /** The Constant serialVersionUID. */
  59.     private static final long serialVersionUID = 1L;

  60.     /** The Constant WINDOW_POSITION. */
  61.     private static final int WINDOW_POSITION = 100;

  62.     /** The google auth request. */
  63.     private final SetGoogleAuthenticatorCredentialRequest googleAuthRequest;

  64.     /**
  65.      * Instantiates a new sets the google authenticator credential click
  66.      * listener.
  67.      *
  68.      * @param googleAuthRequest
  69.      *            the google auth request
  70.      */
  71.     public SetGoogleAuthenticatorCredentialClickListener(final SetGoogleAuthenticatorCredentialRequest googleAuthRequest) {
  72.         this.googleAuthRequest = googleAuthRequest;
  73.     }

  74.     @Override
  75.     public void buttonClick(final ClickEvent event) {
  76.         final SetGoogleAuthenticatorCredentialResponse response = (SetGoogleAuthenticatorCredentialResponse) getApplicationManager().service(googleAuthRequest);

  77.         if (ServiceResult.SUCCESS == response.getResult()) {

  78.             try {
  79.                 final URI keyUri = new URI(response.getOtpAuthTotpURL());
  80.                 final QRCode qrCode = new QRCode(QR_CODE, keyUri.toASCIIString());
  81.                 qrCode.setHeight(QR_CODE_IMAGE_SIZE);
  82.                 qrCode.setWidth(QR_CODE_IMAGE_SIZE);

  83.                 final Window mywindow = new Window(GOOGLE_AUTHENTICATOR_QR_CODE);
  84.                 mywindow.setHeight(MODAL_WINDOW_SIZE);
  85.                 mywindow.setWidth(MODAL_WINDOW_SIZE);

  86.                 mywindow.setPositionX(WINDOW_POSITION);
  87.                 mywindow.setPositionY(WINDOW_POSITION);

  88.                 final VerticalLayout panelContent = new VerticalLayout();

  89.                 mywindow.setContent(panelContent);
  90.                 panelContent.addComponent(qrCode);

  91.                 mywindow.setModal(true);
  92.                 UI.getCurrent().addWindow(mywindow);

  93.             } catch (final URISyntaxException e) {
  94.                 LOGGER.warn(PROBLEM_DISPLAYING_QR_CODE,e);
  95.                 showNotification(PROBLEM_DISPLAYING_QR_CODE,
  96.                           ERROR_MESSAGE,
  97.                           Notification.Type.WARNING_MESSAGE);
  98.             }

  99.         } else {
  100.             showNotification(PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR,
  101.                       ERROR_MESSAGE,
  102.                       Notification.Type.WARNING_MESSAGE);
  103.             LOGGER.info(PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID,googleAuthRequest.getSessionId());
  104.         }
  105.     }
  106. }