[1174] | 1 | |
---|
| 2 | import java.awt.Frame; |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.net.MalformedURLException; |
---|
| 5 | import java.net.URL; |
---|
| 6 | import java.security.GeneralSecurityException; |
---|
| 7 | import java.security.ProviderException; |
---|
| 8 | |
---|
| 9 | import javax.net.ssl.SSLHandshakeException; |
---|
| 10 | import javax.swing.JApplet; |
---|
| 11 | import javax.swing.JOptionPane; |
---|
| 12 | import javax.swing.SwingUtilities; |
---|
| 13 | import org.apache.commons.httpclient.HttpException; |
---|
| 14 | |
---|
| 15 | import netscape.javascript.JSObject; |
---|
| 16 | |
---|
| 17 | import br.gov.serpro.cert.DigitalCertificate; |
---|
| 18 | import br.gov.serpro.setup.Setup; |
---|
| 19 | import br.gov.serpro.ui.DialogBuilder; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * GUI que realiza o login com certificados A1 e A3 ou login com usuário e senha no expresso. |
---|
| 23 | * Esta classe estende a classe JApplet |
---|
| 24 | * @author Mário César Kolling - mario.kolling@serpro.gov.br |
---|
| 25 | */ |
---|
[3633] | 26 | public class LoginApplet extends JApplet { |
---|
[1174] | 27 | |
---|
[3633] | 28 | /** |
---|
| 29 | * Valor gerado aleatoriamente |
---|
| 30 | */ |
---|
| 31 | //TODO: Alterar a cor e fonte dos labels e das caixas de texto e senha. |
---|
| 32 | private static final long serialVersionUID = -6204158613173951516L; |
---|
| 33 | private DigitalCertificate dc; |
---|
[1174] | 34 | private Setup setup; |
---|
| 35 | |
---|
[3633] | 36 | /* (non-Javadoc) |
---|
| 37 | * @see java.applet.Applet#init() |
---|
| 38 | */ |
---|
| 39 | public void init() { |
---|
| 40 | super.init(); |
---|
| 41 | this.setSize(0, 0); |
---|
[1174] | 42 | this.setup = new Setup(this); |
---|
| 43 | this.setup.addLanguageResource("ExpressoCertLoginMessages"); |
---|
[3633] | 44 | } |
---|
[4123] | 45 | |
---|
[4124] | 46 | private boolean parseVercert(String[] answer, String certificate){ |
---|
[4123] | 47 | boolean tryAgain = false; |
---|
| 48 | // Faz o login |
---|
| 49 | if (setup.getParameter("debug").equalsIgnoreCase("true") && answer != null) { |
---|
| 50 | System.out.println("Código de retorno: " + answer[0].trim()); |
---|
| 51 | } |
---|
[1174] | 52 | |
---|
[4123] | 53 | if (answer == null){ // Ação cancelada |
---|
| 54 | tryAgain = false; |
---|
| 55 | String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() |
---|
| 56 | + ":" + this.getCodeBase().getPort() + "/login.php"; |
---|
| 57 | try { |
---|
| 58 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 59 | } catch (MalformedURLException e) { |
---|
| 60 | // TODO Bloco catch gerado automaticamente |
---|
| 61 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 62 | e.printStackTrace(); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | else if (Integer.parseInt(answer[0].trim()) == 0) { |
---|
| 67 | |
---|
| 68 | tryAgain = false; |
---|
| 69 | // Pega usuário e senha de credentials[1] e credentials[2], respectivamente |
---|
| 70 | // adiciona na página e faz o submit. |
---|
| 71 | JSObject document = (JSObject) JSObject.getWindow(this).getMember("document"); |
---|
| 72 | JSObject loginForm = (JSObject) document.getMember("flogin"); |
---|
| 73 | JSObject loginField = (JSObject) loginForm.getMember("user"); |
---|
| 74 | loginField.setMember("value", answer[1].trim()); |
---|
| 75 | |
---|
| 76 | JSObject passwdField = (JSObject) loginForm.getMember("passwd"); |
---|
| 77 | passwdField.setMember("value", answer[2].trim()); |
---|
| 78 | |
---|
[4124] | 79 | JSObject certificateField = (JSObject) loginForm.getMember("certificado"); |
---|
| 80 | certificateField.setMember("value", certificate.trim()); |
---|
| 81 | |
---|
[4123] | 82 | loginForm.call("submit", null); |
---|
| 83 | Thread.yield(); |
---|
| 84 | |
---|
| 85 | } else if (Integer.parseInt(answer[0].trim()) == 6) { |
---|
| 86 | |
---|
| 87 | tryAgain = false; |
---|
| 88 | |
---|
| 89 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 90 | System.out.println("Mensagem de retorno: " + answer[1].trim()); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), answer[1].trim(), this.setup); |
---|
| 94 | |
---|
| 95 | String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() |
---|
| 96 | + ":" + this.getCodeBase().getPort() + "/login.php?cd=98&ts=202"; |
---|
| 97 | try { |
---|
| 98 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 99 | } catch (MalformedURLException e) { |
---|
| 100 | // TODO Bloco catch gerado automaticamente |
---|
| 101 | if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 102 | e.printStackTrace(); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | } else { |
---|
| 106 | tryAgain = true; |
---|
| 107 | dc.destroy(); |
---|
| 108 | System.gc(); |
---|
| 109 | |
---|
| 110 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 111 | System.out.println("Mensagem de retorno: " + answer[1].trim()); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // Mostra mensagem de erro para o usuário |
---|
| 115 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), answer[1].trim(), this.setup); |
---|
| 116 | Thread.yield(); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return tryAgain; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | private boolean parseHandleCertificateResponse(String certificate){ |
---|
| 123 | |
---|
| 124 | // Envia certificado |
---|
| 125 | |
---|
| 126 | JSObject document = (JSObject) JSObject.getWindow(this).getMember("document"); |
---|
| 127 | JSObject certificateForm = (JSObject) document.getMember("certificateForm"); |
---|
| 128 | JSObject certificateField = (JSObject) certificateForm.getMember("certificado"); |
---|
| 129 | certificateField.setMember("value", certificate); |
---|
| 130 | |
---|
| 131 | // submit e cai fora |
---|
| 132 | certificateForm.call("submit", null); |
---|
| 133 | |
---|
| 134 | dc.destroy(); |
---|
| 135 | System.gc(); |
---|
| 136 | Thread.yield(); |
---|
| 137 | |
---|
| 138 | return false; |
---|
| 139 | } |
---|
| 140 | |
---|
[3633] | 141 | /* (non-Javadoc) |
---|
| 142 | * @see java.applet.Applet#start() |
---|
| 143 | */ |
---|
| 144 | @Override |
---|
| 145 | public void start() { |
---|
| 146 | super.start(); |
---|
[1174] | 147 | |
---|
[3633] | 148 | int useCertificate = DigitalCertificate.KEYSTORE_NOT_DETECTED; |
---|
| 149 | boolean tryAgain = true; |
---|
[1174] | 150 | |
---|
[3633] | 151 | do { |
---|
[1174] | 152 | |
---|
[3633] | 153 | // Cria uma instância de DigitalCertificate e a inicializa |
---|
[4123] | 154 | // Aqui pega document base e verifica em que aplicação estamos. |
---|
| 155 | |
---|
[3633] | 156 | this.dc = new DigitalCertificate(this.getDocumentBase(), setup); |
---|
| 157 | useCertificate = dc.init(); |
---|
[1174] | 158 | |
---|
[3633] | 159 | try { |
---|
[4123] | 160 | |
---|
| 161 | String redirect = ""; |
---|
| 162 | |
---|
| 163 | // Testa em qual aplicação estamos. |
---|
| 164 | URL documentURL = this.getDocumentBase(); |
---|
| 165 | |
---|
| 166 | if (documentURL.getPath().matches(".*login.php$")){ |
---|
| 167 | redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() |
---|
| 168 | + ":" + this.getCodeBase().getPort() + "/login.php"; |
---|
| 169 | } |
---|
| 170 | else { |
---|
| 171 | redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() |
---|
| 172 | + ":" + this.getCodeBase().getPort() + "/preferences/index.php"; |
---|
| 173 | } |
---|
| 174 | |
---|
[3633] | 175 | switch (useCertificate) { |
---|
| 176 | case DigitalCertificate.KEYSTORE_DETECTED: |
---|
| 177 | // Mostra PinNeedeDialog. |
---|
| 178 | String pin = DialogBuilder.showPinDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup); |
---|
[4123] | 179 | |
---|
[3633] | 180 | if (pin != null) { |
---|
| 181 | dc.openKeyStore(pin.toCharArray()); |
---|
[4123] | 182 | if (documentURL.getPath().matches(".*login.php$")){ |
---|
| 183 | tryAgain = parseVercert(dc.getCredentials(pin, new URL(this.getCodeBase().getProtocol()+"://" + |
---|
| 184 | this.getCodeBase().getHost() + ":" + this.getCodeBase().getPort() + |
---|
[4124] | 185 | "/security/vercert.php")), dc.getPEMCertificate()); |
---|
[1174] | 186 | } |
---|
[4123] | 187 | else { |
---|
| 188 | tryAgain = parseHandleCertificateResponse(dc.getPEMCertificate()); |
---|
[3633] | 189 | } |
---|
[1174] | 190 | |
---|
[3633] | 191 | } else { |
---|
[1174] | 192 | |
---|
[4123] | 193 | // TODO: Notifica usuário |
---|
[3633] | 194 | tryAgain = false; |
---|
| 195 | try { |
---|
| 196 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 197 | } catch (MalformedURLException e) { |
---|
| 198 | // TODO Bloco catch gerado automaticamente |
---|
[1174] | 199 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 200 | e.printStackTrace(); |
---|
| 201 | } |
---|
[3633] | 202 | } |
---|
| 203 | } |
---|
[1174] | 204 | |
---|
[3633] | 205 | break; |
---|
| 206 | default: |
---|
[1174] | 207 | |
---|
[4123] | 208 | // TODO: notifica usuário que token não foi encontrado |
---|
[3633] | 209 | // ou repositório de chaves públicas não foi configurado. |
---|
| 210 | // Tentar carregar token/keystore novamente? / Logon sem certificado digital? |
---|
[1174] | 211 | |
---|
[3633] | 212 | tryAgain = false; |
---|
[1174] | 213 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 214 | System.out.println("não achou token"); |
---|
| 215 | } |
---|
| 216 | |
---|
[3633] | 217 | dc.destroy(); |
---|
| 218 | System.gc(); |
---|
| 219 | |
---|
| 220 | try { |
---|
| 221 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 222 | } catch (MalformedURLException e) { |
---|
| 223 | // TODO Bloco catch gerado automaticamente |
---|
[1174] | 224 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 225 | e.printStackTrace(); |
---|
| 226 | } |
---|
[3633] | 227 | } |
---|
| 228 | } |
---|
[1174] | 229 | |
---|
[3633] | 230 | } catch (SSLHandshakeException e) { |
---|
[1174] | 231 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 232 | e.printStackTrace(); |
---|
| 233 | } |
---|
[3633] | 234 | dc.destroy(); |
---|
| 235 | System.gc(); |
---|
[1174] | 236 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
[3633] | 237 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet001"), this.setup); |
---|
[1174] | 238 | |
---|
[3633] | 239 | Thread.yield(); |
---|
[1174] | 240 | |
---|
[3633] | 241 | tryAgain = true; |
---|
| 242 | } catch (HttpException e) { |
---|
[1174] | 243 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 244 | e.printStackTrace(); |
---|
| 245 | } |
---|
[3633] | 246 | |
---|
| 247 | tryAgain = true; |
---|
| 248 | Thread.yield(); |
---|
| 249 | } catch (GeneralSecurityException e) { |
---|
[1174] | 250 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 251 | e.printStackTrace(); |
---|
| 252 | } |
---|
[3633] | 253 | |
---|
[1174] | 254 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
[3633] | 255 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet002"), this.setup); |
---|
[1174] | 256 | |
---|
[3633] | 257 | Thread.yield(); |
---|
| 258 | tryAgain = true; |
---|
| 259 | } catch (IOException e) { |
---|
| 260 | dc.destroy(); |
---|
| 261 | System.gc(); |
---|
[1174] | 262 | |
---|
| 263 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 264 | e.printStackTrace(); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | Throwable cause = null; |
---|
[3633] | 268 | if ((cause = e.getCause()) != null) { |
---|
| 269 | if (cause instanceof javax.security.auth.login.LoginException) { |
---|
[1174] | 270 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet003"), this.setup); |
---|
| 271 | } else { |
---|
| 272 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 273 | System.out.println("Exception lançada: " + cause.getClass().getCanonicalName()); |
---|
| 274 | } |
---|
| 275 | } |
---|
[3633] | 276 | } else { |
---|
| 277 | if (e instanceof java.net.ConnectException) { |
---|
[1174] | 278 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
| 279 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet004"), this.setup); |
---|
| 280 | } else { |
---|
| 281 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 282 | System.out.println("Exception lançada: " + e.getClass().getCanonicalName()); |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
[3633] | 287 | Thread.yield(); |
---|
[1174] | 288 | tryAgain = true; |
---|
[3633] | 289 | } catch (ProviderException e) { |
---|
[1174] | 290 | |
---|
[3633] | 291 | dc.destroy(); |
---|
| 292 | System.gc(); |
---|
[1174] | 293 | |
---|
| 294 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 295 | e.printStackTrace(); |
---|
| 296 | } |
---|
| 297 | |
---|
[3633] | 298 | int resultado = DialogBuilder.showConfirmDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
| 299 | //"Deseja tentar carregá-lo novamente?", |
---|
[1174] | 300 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet005"), |
---|
[3633] | 301 | JOptionPane.QUESTION_MESSAGE, |
---|
| 302 | JOptionPane.OK_CANCEL_OPTION, this.setup); |
---|
[1174] | 303 | |
---|
[3633] | 304 | if (resultado == JOptionPane.OK_OPTION) { |
---|
| 305 | tryAgain = true; |
---|
| 306 | } else { |
---|
| 307 | tryAgain = false; |
---|
| 308 | } |
---|
[1174] | 309 | |
---|
[3633] | 310 | Thread.yield(); |
---|
| 311 | } |
---|
[1174] | 312 | |
---|
[3633] | 313 | } while (tryAgain); |
---|
[1174] | 314 | |
---|
[3633] | 315 | } |
---|
[1174] | 316 | |
---|
[3633] | 317 | /** |
---|
| 318 | * Destrói a Applet, executando códigos para desregistrar tokens, keystores, etc. |
---|
| 319 | */ |
---|
| 320 | @Override |
---|
| 321 | public void stop() { |
---|
| 322 | //super.destroy(); |
---|
[1174] | 323 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 324 | System.out.println("Finalizando Applet de Login!"); |
---|
| 325 | } |
---|
| 326 | |
---|
[3633] | 327 | this.dc.destroy(); |
---|
| 328 | this.dc = null; |
---|
| 329 | System.gc(); |
---|
| 330 | } |
---|
[1174] | 331 | } |
---|