1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
41
42 public final class SetGoogleAuthenticatorCredentialClickListener implements ClickListener {
43
44
45
46 private static final String PROBLEM_DISPLAYING_QR_CODE = "Problem displaying qr code";
47
48
49 private static final int WINDOW_POSITION = 100;
50
51
52 private static final String GOOGLE_AUTHENTICATOR_QR_CODE = "Google Authenticator QR code";
53
54
55 private static final String MODAL_WINDOW_SIZE = "200px";
56
57
58 private static final String QR_CODE_IMAGE_SIZE = "175px";
59
60
61 private static final String QR_CODE = "QR code";
62
63
64 private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR_SESSIONID = "Problem enable google authenticator, sessionid{}";
65
66
67 private static final String PROBLEM_ENABLE_GOOGLE_AUTHENTICATOR = "Problem enable google authenticator";
68
69
70 private static final String ERROR_MESSAGE = "Error message";
71
72
73 private static final long serialVersionUID = 1L;
74
75
76 private static final Logger LOGGER = LoggerFactory.getLogger(SetGoogleAuthenticatorCredentialClickListener.class);
77
78
79 private final SetGoogleAuthenticatorCredentialRequest googleAuthRequest;
80
81
82
83
84
85
86
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 }