View Javadoc
1   /*
2    * Copyright 2014 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  
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
28  import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialRequest;
29  import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialResponse;
30  import com.vaadin.ui.Button.ClickEvent;
31  import com.vaadin.ui.Button.ClickListener;
32  import com.vaadin.ui.Notification;
33  import com.vaadin.ui.UI;
34  import com.vaadin.ui.VerticalLayout;
35  import com.vaadin.ui.Window;
36  
37  import fi.jasoft.qrcode.QRCode;
38  
39  /**
40   * The Class SetGoogleAuthenticatorCredentialClickListener.
41   */
42  public final class SetGoogleAuthenticatorCredentialClickListener implements ClickListener {
43  
44  
45  	/** The Constant PROBLEM_DISPLAYING_QR_CODE. */
46  	private static final String PROBLEM_DISPLAYING_QR_CODE = "Problem displaying qr code";
47  
48  	/** The Constant WINDOW_POSITION. */
49  	private static final int WINDOW_POSITION = 100;
50  
51  	/** The Constant GOOGLE_AUTHENTICATOR_QR_CODE. */
52  	private static final String GOOGLE_AUTHENTICATOR_QR_CODE = "Google Authenticator QR code";
53  
54  	/** The Constant MODAL_WINDOW_SIZE. */
55  	private static final String MODAL_WINDOW_SIZE = "200px";
56  
57  	/** The Constant QR_CODE_IMAGE_SIZE. */
58  	private static final String QR_CODE_IMAGE_SIZE = "175px";
59  
60  	/** The Constant QR_CODE. */
61  	private static final String QR_CODE = "QR code";
62  
63  	/** The Constant PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID. */
64  	private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID = "Problem enable google authenticator, sessionid{}";
65  
66  	/** The Constant PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR. */
67  	private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR = "Problem enable google authenticator";
68  
69  	/** The Constant ERROR_MESSAGE. */
70  	private static final String ERROR_MESSAGE = "Error message";
71  
72  	/** The Constant serialVersionUID. */
73  	private static final long serialVersionUID = 1L;
74  
75  	/** The Constant LOGGER. */
76  	private static final Logger LOGGER = LoggerFactory.getLogger(SetGoogleAuthenticatorCredentialClickListener.class);
77  
78  	/** The google auth request. */
79  	private final SetGoogleAuthenticatorCredentialRequest googleAuthRequest;
80  
81  	/**
82  	 * Instantiates a new sets the google authenticator credential click
83  	 * listener.
84  	 *
85  	 * @param googleAuthRequest
86  	 *            the google auth request
87  	 */
88  	public SetGoogleAuthenticatorCredentialClickListener(final SetGoogleAuthenticatorCredentialRequest googleAuthRequest) {
89  		this.googleAuthRequest = googleAuthRequest;
90  	}
91  
92  	@Override
93  	public void buttonClick(final ClickEvent event) {
94  		final SetGoogleAuthenticatorCredentialResponse response = (SetGoogleAuthenticatorCredentialResponse) ApplicationMangerAccess.getApplicationManager().service(googleAuthRequest);
95  
96  		if (ServiceResult.SUCCESS == response.getResult()) {
97  
98  			try {
99  				final URI keyUri = new URI(response.getOtpAuthTotpURL());
100 				final QRCode qrCode = new QRCode(QR_CODE, keyUri.toASCIIString());
101 				qrCode.setHeight(QR_CODE_IMAGE_SIZE);
102 				qrCode.setWidth(QR_CODE_IMAGE_SIZE);
103 
104 				final Window mywindow = new Window(GOOGLE_AUTHENTICATOR_QR_CODE);
105 				mywindow.setHeight(MODAL_WINDOW_SIZE);
106 				mywindow.setWidth(MODAL_WINDOW_SIZE);
107 
108 				mywindow.setPositionX(WINDOW_POSITION);
109 				mywindow.setPositionY(WINDOW_POSITION);
110 
111 				final VerticalLayout panelContent = new VerticalLayout();
112 
113 				mywindow.setContent(panelContent);
114 				panelContent.addComponent(qrCode);
115 
116 				mywindow.setModal(true);
117 				UI.getCurrent().addWindow(mywindow);
118 
119 			} catch (final URISyntaxException e) {
120 				LOGGER.warn(PROBLEM_DISPLAYING_QR_CODE,e);
121 				Notification.show(PROBLEM_DISPLAYING_QR_CODE,
122 		                  ERROR_MESSAGE,
123 		                  Notification.Type.WARNING_MESSAGE);
124 			}
125 
126 		} else {
127 			Notification.show(PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR,
128 	                  ERROR_MESSAGE,
129 	                  Notification.Type.WARNING_MESSAGE);
130 			LOGGER.info(PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID,googleAuthRequest.getSessionId());
131 		}
132 	}
133 }